File size: 1,368 Bytes
12880e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4ce05b9
12880e4
 
 
 
 
 
 
 
 
 
 
4ce05b9
 
12880e4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { describe, it, expect } from "vitest";
import { mergeInstructions } from "../src/import/dsh-import.js";
import { readFileSync, writeFileSync, mkdtempSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";

function tempFile(): string {
  const dir = mkdtempSync(join(tmpdir(), "agentlens-import-"));
  return join(dir, "AGENTS.md");
}

describe("mergeInstructions", () => {
  it("appends the import section and keeps existing content", () => {
    const file = tempFile();
    writeFileSync(file, "# Existing rules\n\n- Keep this\n", "utf8");
    mergeInstructions(file, "/data/dsh");
    const out = readFileSync(file, "utf8");
    expect(out).toContain("- Keep this");
    expect(out).toContain("agentlens-import:start");
    expect(out).toContain("/data/dsh/persona.md");
  });

  it("is idempotent across repeated runs", () => {
    const file = tempFile();
    writeFileSync(file, "# Existing\n", "utf8");
    mergeInstructions(file, "/data/a");
    mergeInstructions(file, "/data/a");
    mergeInstructions(file, "/data/b");
    const out = readFileSync(file, "utf8");
    // Exactly one section, and it reflects the latest data dir.
    expect((out.match(/agentlens-import:start/g) || []).length).toBe(1);
    expect(out).toContain("/data/b/persona.md");
    expect(out).not.toContain("/data/a/persona.md");
  });
});