|
1 | 1 | import { afterEach, describe, expect, it } from "vitest"; |
2 | | -import { mkdtempSync, readFileSync, existsSync, rmSync } from "node:fs"; |
| 2 | +import { |
| 3 | + mkdtempSync, |
| 4 | + mkdirSync, |
| 5 | + writeFileSync, |
| 6 | + readFileSync, |
| 7 | + existsSync, |
| 8 | + rmSync, |
| 9 | +} from "node:fs"; |
3 | 10 | import { tmpdir } from "node:os"; |
4 | 11 | import { join } from "node:path"; |
5 | 12 | import type { LanguageModelV3Prompt } from "@ai-sdk/provider"; |
@@ -69,6 +76,38 @@ describe("writeSystemRule", () => { |
69 | 76 | ); |
70 | 77 | expect(ignore.match(/opencode\.mdc/g)).toHaveLength(1); |
71 | 78 | }); |
| 79 | + it("overwrites the rule body on rewrite", () => { |
| 80 | + const cwd = tmp(); |
| 81 | + writeSystemRule(cwd, "first"); |
| 82 | + writeSystemRule(cwd, "second"); |
| 83 | + const body = readFileSync( |
| 84 | + join(cwd, ".cursor", "rules", "opencode.mdc"), |
| 85 | + "utf8", |
| 86 | + ); |
| 87 | + expect(body).toBe("---\nalwaysApply: true\n---\n\nsecond\n"); |
| 88 | + expect(body).not.toContain("first"); |
| 89 | + }); |
| 90 | + it("preserves a pre-existing .gitignore and appends the rule", () => { |
| 91 | + const cwd = tmp(); |
| 92 | + const dir = join(cwd, ".cursor", "rules"); |
| 93 | + mkdirSync(dir, { recursive: true }); |
| 94 | + // Pre-existing entry with no trailing newline exercises the prefix path. |
| 95 | + writeFileSync(join(dir, ".gitignore"), "other.txt", "utf8"); |
| 96 | + writeSystemRule(cwd, "x"); |
| 97 | + const ignore = readFileSync(join(dir, ".gitignore"), "utf8"); |
| 98 | + const lines = ignore.split(/\r?\n/); |
| 99 | + expect(lines).toContain("other.txt"); |
| 100 | + expect(lines).toContain("opencode.mdc"); |
| 101 | + }); |
| 102 | + it("does not re-append when the rule is already git-ignored", () => { |
| 103 | + const cwd = tmp(); |
| 104 | + const dir = join(cwd, ".cursor", "rules"); |
| 105 | + mkdirSync(dir, { recursive: true }); |
| 106 | + writeFileSync(join(dir, ".gitignore"), "opencode.mdc\n", "utf8"); |
| 107 | + writeSystemRule(cwd, "x"); |
| 108 | + const ignore = readFileSync(join(dir, ".gitignore"), "utf8"); |
| 109 | + expect(ignore.match(/opencode\.mdc/g)).toHaveLength(1); |
| 110 | + }); |
72 | 111 | }); |
73 | 112 |
|
74 | 113 | describe("removeSystemRule", () => { |
|
0 commit comments