xonecas.io
64875
Mc

mini-coder / commit 02964eb

test: add coverage for parsing and model helpers

xonecas2026-03-08 02964ebdd5b9b42869c65af3933b260b5b9a07f7 parent 86d6784

5 files changed

src/cli/frontmatter.test.ts+48−0
@@ -0,0 +1,48 @@
1+import { describe, expect, test } from "bun:test";
2+import { parseFrontmatter } from "./frontmatter.ts";
3+
4+describe("parseFrontmatter", () => {
5+ test("parses frontmatter with CRLF line endings", () => {
6+ const raw =
7+ "---\r\nname: reviewer\r\ndescription: Reviews code\r\nmodel: openai/gpt-5\r\n---\r\n\r\nBe strict.\r\n";
8+ expect(parseFrontmatter(raw)).toEqual({
9+ meta: {
10+ name: "reviewer",
11+ description: "Reviews code",
12+ model: "openai/gpt-5",
13+ },
14+ body: "Be strict.",
15+ });
16+ });
17+
18+ test("preserves colons inside quoted values", () => {
19+ const raw =
20+ '---\ndescription: "Review: fast"\nmodel: "openai/gpt:mini"\n---\n\nPrompt';
21+ expect(parseFrontmatter(raw)).toEqual({
22+ meta: {
23+ description: "Review: fast",
24+ model: "openai/gpt:mini",
25+ },
26+ body: "Prompt",
27+ });
28+ });
29+
30+ test("ignores unknown frontmatter keys", () => {
31+ const raw = "---\nname: helper\nfoo: bar\nexecution: inline\n---\n\nRun it";
32+ expect(parseFrontmatter(raw)).toEqual({
33+ meta: {
34+ name: "helper",
35+ execution: "inline",
36+ },
37+ body: "Run it",
38+ });
39+ });
40+
41+ test("returns the raw input when the closing fence is missing", () => {
42+ const raw = "---\nname: helper\ndescription: Missing end";
43+ expect(parseFrontmatter(raw)).toEqual({
44+ meta: {},
45+ body: raw,
46+ });
47+ });
48+});
src/cli/image-types.test.ts+55−0
@@ -0,0 +1,55 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdtemp, rm, writeFile } from "node:fs/promises";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+import { isImageFilename, loadImageFile } from "./image-types.ts";
6+
7+let dir: string;
8+
9+beforeEach(async () => {
10+ dir = await mkdtemp(join(tmpdir(), "mc-image-types-test-"));
11+});
12+
13+afterEach(async () => {
14+ await rm(dir, { recursive: true, force: true });
15+});
16+
17+describe("isImageFilename", () => {
18+ test("accepts supported extensions case-insensitively", () => {
19+ expect(isImageFilename("photo.JPG")).toBe(true);
20+ expect(isImageFilename("archive.backup.AvIf")).toBe(true);
21+ });
22+
23+ test("rejects dotfiles and unsupported extensions", () => {
24+ expect(isImageFilename(".png")).toBe(false);
25+ expect(isImageFilename("README")).toBe(false);
26+ expect(isImageFilename("vector.svg")).toBe(false);
27+ });
28+});
29+
30+describe("loadImageFile", () => {
31+ test("loads file data and normalizes jpg media types", async () => {
32+ const bytes = Buffer.from("abc");
33+ const filePath = join(dir, "photo.jpg");
34+ await writeFile(filePath, bytes);
35+
36+ expect(await loadImageFile(filePath)).toEqual({
37+ data: bytes.toString("base64"),
38+ mediaType: "image/jpeg",
39+ });
40+ });
41+
42+ test("preserves supported exotic image extensions", async () => {
43+ const bytes = Buffer.from([0, 1, 2, 3]);
44+ const filePath = join(dir, "capture.heic");
45+ await writeFile(filePath, bytes);
46+
47+ const attachment = await loadImageFile(filePath);
48+ expect(attachment?.data).toBe(bytes.toString("base64"));
49+ expect(attachment?.mediaType).toBe("image/heic");
50+ });
51+
52+ test("returns null for missing files", async () => {
53+ expect(await loadImageFile(join(dir, "missing.png"))).toBeNull();
54+ });
55+});
src/cli/load-markdown-configs.test.ts+105−0
@@ -0,0 +1,105 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+import { loadMarkdownConfigs } from "./load-markdown-configs.ts";
6+
7+let dir: string;
8+
9+beforeEach(() => {
10+ dir = mkdtempSync(join(tmpdir(), "mc-load-markdown-configs-test-"));
11+});
12+
13+afterEach(() => {
14+ rmSync(dir, { recursive: true, force: true });
15+});
16+
17+function writeFileAt(parts: string[], content: string): void {
18+ const filePath = join(dir, ...parts);
19+ mkdirSync(join(filePath, ".."), { recursive: true });
20+ writeFileSync(filePath, content);
21+}
22+
23+function createDirAt(parts: string[]): void {
24+ mkdirSync(join(dir, ...parts), { recursive: true });
25+}
26+
27+function nextName(prefix: string): string {
28+ return `${prefix}-${Math.random().toString(36).slice(2)}`;
29+}
30+
31+describe("loadMarkdownConfigs", () => {
32+ test("ignores local .claude dirs when disabled", () => {
33+ const name = nextName("flat-disabled");
34+ writeFileAt([".claude", "commands", `${name}.md`], "from claude");
35+
36+ const configs = loadMarkdownConfigs({
37+ type: "commands",
38+ strategy: "flat",
39+ cwd: dir,
40+ includeClaudeDirs: false,
41+ mapConfig: ({ body, source }) => ({ body, source }),
42+ });
43+
44+ expect(configs.get(name)).toBeUndefined();
45+ });
46+
47+ test("prefers local .agents over local .claude with the same name", () => {
48+ const name = nextName("flat-precedence");
49+ writeFileAt([".claude", "commands", `${name}.md`], "from claude");
50+ writeFileAt([".agents", "commands", `${name}.md`], "from agents");
51+
52+ const configs = loadMarkdownConfigs({
53+ type: "commands",
54+ strategy: "flat",
55+ cwd: dir,
56+ includeClaudeDirs: true,
57+ mapConfig: ({ body, source }) => ({ body, source }),
58+ });
59+
60+ expect(configs.get(name)).toEqual({ body: "from agents", source: "local" });
61+ });
62+
63+ test("uses frontmatter name for nested configs", () => {
64+ const folderName = nextName("folder");
65+ const configName = nextName("skill");
66+ writeFileAt(
67+ [".agents", "skills", folderName, "SKILL.md"],
68+ `---\nname: ${configName}\ndescription: Test skill\n---\n\nBody`,
69+ );
70+
71+ const configs = loadMarkdownConfigs({
72+ type: "skills",
73+ strategy: "nested",
74+ nestedFileName: "SKILL.md",
75+ cwd: dir,
76+ includeClaudeDirs: false,
77+ mapConfig: ({ name, body, source }) => ({ name, body, source }),
78+ });
79+
80+ expect(configs.has(folderName)).toBe(false);
81+ expect(configs.get(configName)).toEqual({
82+ name: configName,
83+ body: "Body",
84+ source: "local",
85+ });
86+ });
87+
88+ test("skips directory entries that look like markdown files", () => {
89+ const badName = `${nextName("bad")}.md`;
90+ const goodName = nextName("good");
91+ createDirAt([".agents", "commands", badName]);
92+ writeFileAt([".agents", "commands", `${goodName}.md`], "ok");
93+
94+ const configs = loadMarkdownConfigs({
95+ type: "commands",
96+ strategy: "flat",
97+ cwd: dir,
98+ includeClaudeDirs: false,
99+ mapConfig: ({ body }) => body,
100+ });
101+
102+ expect(configs.get(goodName)).toBe("ok");
103+ expect(configs.has(badName.slice(0, -3))).toBe(false);
104+ });
105+});
src/llm-api/model-info.test.ts+63−0
@@ -7,6 +7,7 @@ import {
77 isProviderVisibleInSnapshot,
88 isStaleTimestamp,
99 matchCanonicalModelId,
10+ normalizeModelId,
1011 parseModelsDevCapabilities,
1112 resolveModelInfoFromRows,
1213 } from "./model-info.ts";
@@ -64,6 +65,68 @@ describe("parseModelsDevCapabilities", () => {
6465 expect(byId.get("gpt-5.3-codex")?.context_window).toBe(400_000);
6566 expect(byId.get("gpt-5.3-codex")?.reasoning).toBe(1);
6667 });
68+
69+ test("normalizes repeated models/ prefixes and merges duplicate providers", () => {
70+ const rows = parseModelsDevCapabilities(
71+ {
72+ google: {
73+ models: {
74+ "Gemini-2.5-Pro": {
75+ id: "models/models/Gemini-2.5-Pro",
76+ },
77+ },
78+ },
79+ openai: {
80+ models: {
81+ "gemini-2.5-pro": {
82+ limit: { context: 128.9 },
83+ reasoning: true,
84+ },
85+ },
86+ },
87+ },
88+ 456,
89+ );
90+
91+ expect(rows).toEqual([
92+ {
93+ canonical_model_id: "gemini-2.5-pro",
94+ context_window: 128,
95+ reasoning: 1,
96+ source_provider: "google",
97+ raw_json: JSON.stringify({ id: "models/models/Gemini-2.5-Pro" }),
98+ updated_at: 456,
99+ },
100+ ]);
101+ });
102+
103+ test("sanitizes invalid context window values", () => {
104+ const rows = parseModelsDevCapabilities(
105+ {
106+ openai: {
107+ models: {
108+ negative: { limit: { context: -12.9 } },
109+ fractional: { limit: { context: 4096.9 } },
110+ invalid: { limit: { context: Number.NaN } },
111+ stringy: { limit: { context: "128000" } },
112+ },
113+ },
114+ },
115+ 789,
116+ );
117+
118+ const byId = new Map(rows.map((row) => [row.canonical_model_id, row]));
119+ expect(byId.get("negative")?.context_window).toBe(0);
120+ expect(byId.get("fractional")?.context_window).toBe(4096);
121+ expect(byId.get("invalid")?.context_window).toBeNull();
122+ expect(byId.get("stringy")?.context_window).toBeNull();
123+ });
124+});
125+
126+describe("normalizeModelId", () => {
127+ test("trims, lowercases, and removes repeated models/ prefixes", () => {
128+ expect(normalizeModelId(" MODELS/models/GPT-5.2 ")).toBe("gpt-5.2");
129+ });
67130 });
68131
69132 describe("model match index", () => {
src/tools/hashline.test.ts+45−0
@@ -0,0 +1,45 @@
1+import { describe, expect, test } from "bun:test";
2+import { findLineByHash, formatHashLine, hashLine } from "./hashline.ts";
3+
4+describe("hashLine", () => {
5+ test("returns a stable two-character lowercase hex digest", () => {
6+ const hash = hashLine("hello");
7+ expect(hash).toHaveLength(2);
8+ expect(hash).toMatch(/^[0-9a-f]{2}$/);
9+ expect(hashLine("hello")).toBe(hash);
10+ });
11+});
12+
13+describe("formatHashLine", () => {
14+ test("formats the line number, hash, and content", () => {
15+ expect(formatHashLine(12, "alpha")).toBe(`12:${hashLine("alpha")}| alpha`);
16+ });
17+});
18+
19+describe("findLineByHash", () => {
20+ test("returns the hinted line when it matches exactly", () => {
21+ const lines = ["alpha", "beta", "gamma", "beta"];
22+ expect(findLineByHash(lines, hashLine("beta"), 2)).toBe(2);
23+ });
24+
25+ test("finds a unique nearby match within the scan range", () => {
26+ const lines = ["alpha", "beta", "gamma", "delta"];
27+ expect(findLineByHash(lines, hashLine("gamma"), 2)).toBe(3);
28+ });
29+
30+ test("treats anchor hashes case-insensitively", () => {
31+ const lines = ["alpha", "beta"];
32+ expect(findLineByHash(lines, hashLine("beta").toUpperCase(), 1)).toBe(2);
33+ });
34+
35+ test("returns null when nearby matches are ambiguous", () => {
36+ const lines = ["alpha", "beta", "gamma", "beta"];
37+ expect(findLineByHash(lines, hashLine("beta"), 3)).toBeNull();
38+ });
39+
40+ test("returns null when the only match is outside the scan range", () => {
41+ const lines = Array.from({ length: 25 }, (_, i) => `line ${i + 1}`);
42+ lines[22] = "needle";
43+ expect(findLineByHash(lines, hashLine("needle"), 1)).toBeNull();
44+ });
45+});