|
| 1 | +import { expect, test, describe } from "bun:test" |
| 2 | +import { ConfigMarkdown } from "../../src/config/markdown" |
| 3 | +import path from "path" |
| 4 | +import os from "os" |
| 5 | + |
| 6 | +describe("ConfigMarkdown: duplicate key deduplication", () => { |
| 7 | + test("should keep first occurrence of duplicate top-level keys", () => { |
| 8 | + const content = [ |
| 9 | + "---", |
| 10 | + "name: first", |
| 11 | + "mode: subagent", |
| 12 | + "name: second", |
| 13 | + "---", |
| 14 | + "", |
| 15 | + "Body content", |
| 16 | + ].join("\n") |
| 17 | + |
| 18 | + const sanitized = ConfigMarkdown.fallbackSanitization(content) |
| 19 | + expect(sanitized).toContain("name: first") |
| 20 | + expect(sanitized).not.toContain("name: second") |
| 21 | + }) |
| 22 | + |
| 23 | + test("should deduplicate multiple duplicate keys", () => { |
| 24 | + const content = [ |
| 25 | + "---", |
| 26 | + "pwd: allow", |
| 27 | + "mode: subagent", |
| 28 | + "pwd: deny", |
| 29 | + "description: test", |
| 30 | + "mode: all", |
| 31 | + "---", |
| 32 | + "", |
| 33 | + "Body", |
| 34 | + ].join("\n") |
| 35 | + |
| 36 | + const sanitized = ConfigMarkdown.fallbackSanitization(content) |
| 37 | + expect(sanitized).toContain("pwd: allow") |
| 38 | + expect(sanitized).not.toContain("pwd: deny") |
| 39 | + expect(sanitized).toContain("mode: subagent") |
| 40 | + expect(sanitized).not.toContain("mode: all") |
| 41 | + expect(sanitized).toContain("description: test") |
| 42 | + }) |
| 43 | + |
| 44 | + test("should preserve nested/indented duplicate keys", () => { |
| 45 | + const content = [ |
| 46 | + "---", |
| 47 | + "tools:", |
| 48 | + " write: true", |
| 49 | + " read: true", |
| 50 | + " write: false", |
| 51 | + "mode: subagent", |
| 52 | + "---", |
| 53 | + "", |
| 54 | + "Body", |
| 55 | + ].join("\n") |
| 56 | + |
| 57 | + const sanitized = ConfigMarkdown.fallbackSanitization(content) |
| 58 | + // indented lines are preserved as-is (they're nested keys) |
| 59 | + const indented = sanitized.match(/ write: (true|false)/g) |
| 60 | + expect(indented).toHaveLength(2) |
| 61 | + }) |
| 62 | + |
| 63 | + test("should not affect content without duplicates", () => { |
| 64 | + const content = [ |
| 65 | + "---", |
| 66 | + "name: test", |
| 67 | + "mode: subagent", |
| 68 | + "description: a description", |
| 69 | + "---", |
| 70 | + "", |
| 71 | + "Body content", |
| 72 | + ].join("\n") |
| 73 | + |
| 74 | + const sanitized = ConfigMarkdown.fallbackSanitization(content) |
| 75 | + expect(sanitized).toContain("name: test") |
| 76 | + expect(sanitized).toContain("mode: subagent") |
| 77 | + expect(sanitized).toContain("description: a description") |
| 78 | + expect(sanitized).toContain("Body content") |
| 79 | + }) |
| 80 | + |
| 81 | + test("should return content unchanged when no frontmatter", () => { |
| 82 | + const content = "Just some text without frontmatter" |
| 83 | + const sanitized = ConfigMarkdown.fallbackSanitization(content) |
| 84 | + expect(sanitized).toBe(content) |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe("ConfigMarkdown: parse with duplicate keys", () => { |
| 89 | + test("should parse file with duplicate keys keeping first value", async () => { |
| 90 | + const dir = path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2)) |
| 91 | + const file = path.join(dir, "duplicate-keys.md") |
| 92 | + await Bun.write( |
| 93 | + file, |
| 94 | + [ |
| 95 | + "---", |
| 96 | + "mode: subagent", |
| 97 | + "description: test agent", |
| 98 | + "mode: all", |
| 99 | + "---", |
| 100 | + "", |
| 101 | + "Agent instructions here", |
| 102 | + ].join("\n"), |
| 103 | + ) |
| 104 | + |
| 105 | + const result = await ConfigMarkdown.parse(file) |
| 106 | + expect(result.data.mode).toBe("subagent") |
| 107 | + expect(result.data.description).toBe("test agent") |
| 108 | + expect(result.content).toContain("Agent instructions here") |
| 109 | + }) |
| 110 | + |
| 111 | + test("should parse file with no duplicates normally", async () => { |
| 112 | + const dir = path.join(os.tmpdir(), "opencode-test-" + Math.random().toString(36).slice(2)) |
| 113 | + const file = path.join(dir, "normal.md") |
| 114 | + await Bun.write( |
| 115 | + file, |
| 116 | + [ |
| 117 | + "---", |
| 118 | + "mode: subagent", |
| 119 | + "description: normal agent", |
| 120 | + "---", |
| 121 | + "", |
| 122 | + "Normal content", |
| 123 | + ].join("\n"), |
| 124 | + ) |
| 125 | + |
| 126 | + const result = await ConfigMarkdown.parse(file) |
| 127 | + expect(result.data.mode).toBe("subagent") |
| 128 | + expect(result.data.description).toBe("normal agent") |
| 129 | + expect(result.content).toContain("Normal content") |
| 130 | + }) |
| 131 | +}) |
0 commit comments