Skip to content

Commit ba0c40f

Browse files
committed
test: wildcard matching + config markdown sanitization coverage
Add edge-case tests for Wildcard.match (security-critical permission matching) and ConfigMarkdown.fallbackSanitization/shell() (YAML frontmatter recovery). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> https://claude.ai/code/session_01ArGQ4moupZc4ZY6pwjMVTj
1 parent 3b6d5d4 commit ba0c40f

2 files changed

Lines changed: 158 additions & 1 deletion

File tree

‎packages/opencode/test/config/markdown.test.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,102 @@ Always structure your responses using clear markdown formatting:
211211
})
212212
})
213213

214+
describe("ConfigMarkdown.shell", () => {
215+
test("extracts shell commands from template", () => {
216+
const template = "Run !`git status` and then !`npm test`"
217+
const matches = ConfigMarkdown.shell(template)
218+
expect(matches.length).toBe(2)
219+
expect(matches[0][1]).toBe("git status")
220+
expect(matches[1][1]).toBe("npm test")
221+
})
222+
223+
test("does not match regular backticks without bang", () => {
224+
const template = "Use `git status` command"
225+
const matches = ConfigMarkdown.shell(template)
226+
expect(matches.length).toBe(0)
227+
})
228+
229+
test("does not match empty bang-backtick", () => {
230+
const template = "Empty !`` should not match"
231+
const matches = ConfigMarkdown.shell(template)
232+
expect(matches.length).toBe(0)
233+
})
234+
235+
test("extracts command with pipes and flags", () => {
236+
const template = "Run !`cat file.txt | grep error` to find issues"
237+
const matches = ConfigMarkdown.shell(template)
238+
expect(matches.length).toBe(1)
239+
expect(matches[0][1]).toBe("cat file.txt | grep error")
240+
})
241+
})
242+
243+
describe("ConfigMarkdown.fallbackSanitization", () => {
244+
test("converts value with colon to block scalar", () => {
245+
const input = "---\nurl: https://example.com:8080\n---\nContent"
246+
const result = ConfigMarkdown.fallbackSanitization(input)
247+
expect(result).toContain("url: |-")
248+
expect(result).toContain(" https://example.com:8080")
249+
})
250+
251+
test("preserves already double-quoted values with colons", () => {
252+
const input = '---\nurl: "https://example.com:8080"\n---\nContent'
253+
const result = ConfigMarkdown.fallbackSanitization(input)
254+
expect(result).toContain('"https://example.com:8080"')
255+
expect(result).not.toContain("|-")
256+
})
257+
258+
test("preserves already single-quoted values with colons", () => {
259+
const input = "---\nurl: 'https://example.com:8080'\n---\nContent"
260+
const result = ConfigMarkdown.fallbackSanitization(input)
261+
expect(result).toContain("'https://example.com:8080'")
262+
expect(result).not.toContain("|-")
263+
})
264+
265+
test("passes through content without frontmatter unchanged", () => {
266+
const input = "Just some content"
267+
expect(ConfigMarkdown.fallbackSanitization(input)).toBe(input)
268+
})
269+
270+
test("preserves comments in frontmatter", () => {
271+
const input = "---\n# comment\nname: John\n---\nContent"
272+
const result = ConfigMarkdown.fallbackSanitization(input)
273+
expect(result).toContain("# comment")
274+
expect(result).toContain("name: John")
275+
})
276+
277+
test("preserves indented continuation lines", () => {
278+
const input = "---\nsummary: >\n This is multiline\n content\n---\nBody"
279+
const result = ConfigMarkdown.fallbackSanitization(input)
280+
expect(result).toContain(" This is multiline")
281+
expect(result).toContain(" content")
282+
})
283+
284+
test("preserves block scalar indicators", () => {
285+
const input = "---\nnotes: |\n line1\n line2\n---\nContent"
286+
const result = ConfigMarkdown.fallbackSanitization(input)
287+
expect(result).toContain("notes: |")
288+
})
289+
290+
test("handles empty frontmatter values", () => {
291+
const input = "---\nempty:\n---\nContent"
292+
const result = ConfigMarkdown.fallbackSanitization(input)
293+
expect(result).toContain("empty:")
294+
})
295+
296+
test("does not modify content after frontmatter", () => {
297+
const input = "---\nname: John\n---\nContent with url: http://example.com:3000"
298+
const result = ConfigMarkdown.fallbackSanitization(input)
299+
expect(result).toContain("Content with url: http://example.com:3000")
300+
})
301+
302+
test("handles CRLF line endings in frontmatter regex", () => {
303+
const input = "---\r\nurl: https://example.com:8080\r\n---\r\nContent"
304+
const result = ConfigMarkdown.fallbackSanitization(input)
305+
expect(result).toContain("url: |-")
306+
expect(result).toContain(" https://example.com:8080")
307+
})
308+
})
309+
214310
describe("ConfigMarkdown: frontmatter has weird model id", async () => {
215311
const result = await ConfigMarkdown.parse(import.meta.dir + "/fixtures/weird-model-id.md")
216312

‎packages/opencode/test/util/wildcard.test.ts‎

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { test, expect } from "bun:test"
1+
import { describe, test, expect } from "bun:test"
22
import { Wildcard } from "../../src/util/wildcard"
33

44
test("match handles glob tokens", () => {
@@ -88,3 +88,64 @@ test("match handles case-insensitivity on Windows", () => {
8888
expect(Wildcard.match("/users/test/file", "/Users/test/*")).toBe(false)
8989
}
9090
})
91+
92+
// --- Edge cases found during test-discovery audit ---
93+
94+
describe("Wildcard.match — star crosses path separators", () => {
95+
test("star matches across directory boundaries unlike shell glob", () => {
96+
// Wildcard.match uses .* which crosses /, unlike shell globs where * stops at /
97+
// This is relied on by the permission system: "src/*" must match "src/deep/nested/file.ts"
98+
expect(Wildcard.match("src/deep/nested/file.ts", "src/*")).toBe(true)
99+
expect(Wildcard.match("src/a/b/c/d.ts", "src/*/d.ts")).toBe(true)
100+
})
101+
})
102+
103+
describe("Wildcard.match — special regex characters", () => {
104+
test("dots in pattern are literal, not regex any-char", () => {
105+
expect(Wildcard.match("file.txt", "file.txt")).toBe(true)
106+
expect(Wildcard.match("filextxt", "file.txt")).toBe(false)
107+
})
108+
109+
test("parentheses and pipes in pattern are literal", () => {
110+
expect(Wildcard.match("(a|b)", "(a|b)")).toBe(true)
111+
expect(Wildcard.match("a", "(a|b)")).toBe(false)
112+
})
113+
114+
test("brackets in pattern are literal", () => {
115+
expect(Wildcard.match("[abc]", "[abc]")).toBe(true)
116+
expect(Wildcard.match("a", "[abc]")).toBe(false)
117+
})
118+
119+
test("dollar and caret in pattern are literal", () => {
120+
expect(Wildcard.match("$HOME", "$HOME")).toBe(true)
121+
expect(Wildcard.match("^start", "^start")).toBe(true)
122+
})
123+
})
124+
125+
describe("Wildcard.match — empty and boundary cases", () => {
126+
test("empty pattern matches only empty string", () => {
127+
expect(Wildcard.match("", "")).toBe(true)
128+
expect(Wildcard.match("something", "")).toBe(false)
129+
})
130+
})
131+
132+
describe("Wildcard.allStructured — non-contiguous tail matching", () => {
133+
test("non-contiguous tail tokens match if in correct order", () => {
134+
// matchSequence scans non-contiguously: finds "push" then skips "extra" and finds "--force"
135+
const result = Wildcard.allStructured(
136+
{ head: "git", tail: ["push", "extra", "--force"] },
137+
{ "git push --force": "deny" },
138+
)
139+
expect(result).toBe("deny")
140+
})
141+
142+
test("reversed tail tokens do not match when items exhausted", () => {
143+
// Pattern expects push then --force; tail has them reversed
144+
// "push" found at i=1, but no items remain after for "--force"
145+
const result = Wildcard.allStructured(
146+
{ head: "git", tail: ["--force", "push"] },
147+
{ "git push --force": "deny" },
148+
)
149+
expect(result).toBeUndefined()
150+
})
151+
})

0 commit comments

Comments
 (0)