Skip to content

Commit f25bc07

Browse files
committed
test(provider): cover gitignore-preserve and rule-body rewrite paths
1 parent d771ddd commit f25bc07

1 file changed

Lines changed: 40 additions & 1 deletion

File tree

test/system-rule.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
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";
310
import { tmpdir } from "node:os";
411
import { join } from "node:path";
512
import type { LanguageModelV3Prompt } from "@ai-sdk/provider";
@@ -69,6 +76,38 @@ describe("writeSystemRule", () => {
6976
);
7077
expect(ignore.match(/opencode\.mdc/g)).toHaveLength(1);
7178
});
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+
});
72111
});
73112

74113
describe("removeSystemRule", () => {

0 commit comments

Comments
 (0)