返回 CodeWhale
check-facts.test.ts
根目录 / web / lib / check-facts.test.ts
1 /**
2 * Tests for check-facts.mjs via its re-importable logic.
3 *
4 * We test the diffFacts helper (ported from check-facts.mjs) against
5 * fixture-like objects to prove the stale-version detection works.
6 * End-to-end `node scripts/check-facts.mjs` exit-code tests are not run
7 * from vitest since they depend on the actual workspace file tree.
8 */
9 import { describe, it, expect } from "vitest";
10
11 // --- inline diffFacts (same logic as check-facts.mjs) ----------------
12
13 interface ProviderFact {
14 id: string;
15 label: string;
16 env: string;
17 }
18
19 interface PublishedReleaseFact {
20 tag: string;
21 version: string;
22 publishedAt: string;
23 url: string;
24 }
25
26 interface ModelFact {
27 id: string;
28 provider: string | null;
29 contextWindow: number | null;
30 maxOutput: number | null;
31 reasoning: boolean;
32 addedAt: string | null;
33 }
34
35 interface RepoFacts {
36 [key: string]: unknown;
37 generatedAt: string;
38 sourceRevision: string | null;
39 sourceCommittedAt: string | null;
40 version: string | null;
41 crates: string[];
42 sandboxBackends: string[];
43 providers: ProviderFact[];
44 models: ModelFact[];
45 defaultModel: string | null;
46 nodeEngines: string | null;
47 toolCount: number | null;
48 contributorCount: number;
49 license: string | null;
50 latestPublishedRelease: PublishedReleaseFact | null;
51 }
52
53 function diffFacts(
54 committed: Record<string, unknown>,
55 fresh: Record<string, unknown>,
56 ): Array<{ field: string; committed: unknown; fresh: unknown }> {
57 const checkFields = [
58 "version",
59 "crates",
60 "sandboxBackends",
61 "providers",
62 "models",
63 "defaultModel",
64 "nodeEngines",
65 "toolCount",
66 "contributorCount",
67 "license",
68 "latestPublishedRelease",
69 ];
70 const diffs: Array<{ field: string; committed: unknown; fresh: unknown }> = [];
71 for (const field of checkFields) {
72 const a = JSON.stringify(committed[field] ?? null);
73 const b = JSON.stringify(fresh[field] ?? null);
74 if (a !== b) {
75 diffs.push({ field, committed: committed[field], fresh: fresh[field] });
76 }
77 }
78 return diffs;
79 }
80
81 // --- helpers ---------------------------------------------------------
82
83 function freshFacts(overrides: Partial<RepoFacts> = {}): RepoFacts {
84 return {
85 generatedAt: new Date().toISOString(),
86 sourceRevision: null,
87 sourceCommittedAt: null,
88 version: "0.8.64",
89 crates: ["cli", "config", "tui"],
90 sandboxBackends: [
91 "seatbelt (macOS, when available)",
92 "bubblewrap (Linux, opt-in when installed)",
93 ],
94 providers: [
95 { id: "deepseek", label: "DeepSeek", env: "DEEPSEEK_API_KEY" },
96 { id: "anthropic", label: "Anthropic", env: "ANTHROPIC_API_KEY" },
97 ],
98 models: [
99 {
100 id: "deepseek-v4-pro",
101 provider: "DeepSeek",
102 contextWindow: 1000000,
103 maxOutput: 128000,
104 reasoning: true,
105 addedAt: "2026-07-01",
106 },
107 ],
108 defaultModel: "deepseek-v4-pro",
109 nodeEngines: ">=18",
110 toolCount: 78,
111 contributorCount: 390,
112 license: "MIT",
113 latestPublishedRelease: {
114 tag: "v0.8.63",
115 version: "0.8.63",
116 publishedAt: "2026-06-01T00:00:00Z",
117 url: "https://github.com/codewhale-hq/CodeWhale/releases/tag/v0.8.63",
118 },
119 ...overrides,
120 };
121 }
122
123 // --- tests -----------------------------------------------------------
124
125 describe("diffFacts (check-facts parity)", () => {
126 it("returns empty array when facts match", () => {
127 const committed = freshFacts();
128 const fresh = freshFacts();
129 expect(diffFacts(committed, fresh)).toEqual([]);
130 });
131
132 it("detects stale version (0.8.62 vs 0.8.64)", () => {
133 const committed = freshFacts({ version: "0.8.62" });
134 const fresh = freshFacts({ version: "0.8.64" });
135 const diffs = diffFacts(committed, fresh);
136 expect(diffs).toHaveLength(1);
137 expect(diffs[0]).toEqual({
138 field: "version",
139 committed: "0.8.62",
140 fresh: "0.8.64",
141 });
142 });
143
144 it("detects provider list drift (added Anthropic)", () => {
145 const committed = freshFacts({
146 providers: [{ id: "deepseek", label: "DeepSeek", env: "DEEPSEEK_API_KEY" }],
147 });
148 const fresh = freshFacts({
149 providers: [
150 { id: "deepseek", label: "DeepSeek", env: "DEEPSEEK_API_KEY" },
151 { id: "anthropic", label: "Anthropic", env: "ANTHROPIC_API_KEY" },
152 ],
153 });
154 const diffs = diffFacts(committed, fresh);
155 expect(diffs).toHaveLength(1);
156 expect(diffs[0].field).toBe("providers");
157 });
158
159 it("detects stale default model", () => {
160 const committed = freshFacts({ defaultModel: "deepseek-v3" });
161 const fresh = freshFacts({ defaultModel: "deepseek-v4-pro" });
162 const diffs = diffFacts(committed, fresh);
163 expect(diffs).toHaveLength(1);
164 expect(diffs[0].field).toBe("defaultModel");
165 });
166
167 it("detects tool count drift", () => {
168 const committed = freshFacts({ toolCount: 70 });
169 const fresh = freshFacts({ toolCount: 78 });
170 const diffs = diffFacts(committed, fresh);
171 expect(diffs).toHaveLength(1);
172 expect(diffs[0].field).toBe("toolCount");
173 });
174
175 it("detects multiple field drifts at once", () => {
176 const committed = freshFacts({
177 version: "0.8.62",
178 toolCount: 70,
179 contributorCount: 389,
180 });
181 const fresh = freshFacts({
182 version: "0.8.64",
183 toolCount: 78,
184 });
185 const diffs = diffFacts(committed, fresh);
186 expect(diffs).toHaveLength(3);
187 expect(diffs.map((d) => d.field).sort()).toEqual(["contributorCount", "toolCount", "version"]);
188 });
189
190 it("ignores generatedAt and exact-build provenance changes", () => {
191 const committed = freshFacts({ generatedAt: "old" });
192 const fresh = freshFacts({
193 generatedAt: "new",
194 sourceRevision: "a".repeat(40),
195 sourceCommittedAt: "2026-07-21T22:00:00Z",
196 });
197 expect(diffFacts(committed, fresh)).toEqual([]);
198 });
199
200 it("detects latest-published-release drift", () => {
201 const committed = freshFacts();
202 const fresh = freshFacts({
203 latestPublishedRelease: {
204 tag: "v0.8.64",
205 version: "0.8.64",
206 publishedAt: "2026-06-02T00:00:00Z",
207 url: "https://github.com/codewhale-hq/CodeWhale/releases/tag/v0.8.64",
208 },
209 });
210 const diffs = diffFacts(committed, fresh);
211 expect(diffs).toHaveLength(1);
212 expect(diffs[0].field).toBe("latestPublishedRelease");
213 });
214
215 it("handles null-to-value drift for license", () => {
216 const committed = freshFacts({ license: null });
217 const fresh = freshFacts({ license: "MIT" });
218 const diffs = diffFacts(committed, fresh);
219 expect(diffs).toHaveLength(1);
220 expect(diffs[0].field).toBe("license");
221 });
222
223 it("handles empty arrays in committed vs populated arrays in fresh", () => {
224 const committed = freshFacts({ crates: [], providers: [] });
225 const fresh = freshFacts({
226 crates: ["cli"],
227 providers: [{ id: "deepseek", label: "DeepSeek", env: "DEEPSEEK_API_KEY" }],
228 });
229 const diffs = diffFacts(committed, fresh);
230 expect(diffs).toHaveLength(2);
231 expect(diffs.map((d) => d.field).sort()).toEqual(["crates", "providers"]);
232 });
233 });
234
234 lines TYPESCRIPT