返回 CodeWhale
capabilities.test.ts
根目录 / web / lib / capabilities.test.ts
1 import { readdirSync, readFileSync, statSync } from "node:fs";
2 import { join } from "node:path";
3 import { renderToStaticMarkup } from "react-dom/server";
4 import { afterEach, describe, expect, it, vi } from "vitest";
5 import {
6 RELEASE_BASELINE,
7 baselineFor,
8 capabilityAvailability,
9 capabilityViews,
10 claimAvailability,
11 claimedItems,
12 isFounderHeld,
13 parseReleaseBaseline,
14 releaseView,
15 releasedFeatureNames,
16 runtimeCapabilityContext,
17 type CapabilityContext,
18 type ReleaseBaseline,
19 } from "./capabilities";
20 import { FACTS } from "./facts.generated";
21 import { FEATURE_CATALOGUE, type Feature } from "./features";
22 import { FOUNDER_SWITCHES, FOUNDER_SWITCH_VALUES } from "./founder-switches";
23 import { PRODUCT_COPY } from "./content/product";
24 import ModelsPage from "../app/[locale]/models/page";
25 import { buildLlmsTxt } from "./llms-txt";
26
27 vi.mock("next/navigation", () => ({ redirect: vi.fn(), notFound: vi.fn(), usePathname: () => "/en", useRouter: () => ({}) }));
28
29 const WEB = new URL("..", import.meta.url).pathname;
30
31 const row = (id: string, since: string | null, extra: Partial<Feature> = {}): Feature => ({
32 id, name: `Name ${id}`, summary: `Summary ${id}`, status: "stable", surfaces: { tui: "stable" }, since, flag: null, docs: null, registryLine: 1, ...extra,
33 });
34
35 const baseline = (tag: string, ids: string[], summaries: Record<string, string> = {}): ReleaseBaseline => ({
36 format: "codewhale.release-baseline.v1",
37 tag,
38 version: tag.slice(1),
39 features: ids.map((id) => ({ id, name: `Tagged ${id}`, summary: summaries[id] ?? `Summary ${id}`, status: "stable", since: null })),
40 providers: [{ id: "alpha", label: "Alpha", env: "ALPHA_KEY" }],
41 models: [{ id: "alpha-1", provider: "Alpha", contextWindow: 1000, maxOutput: 100, reasoning: false, addedAt: "2026-01-01" }],
42 toolCount: 7,
43 });
44
45 const at = (tag: string, ids: string[] = [], summaries: Record<string, string> = {}): CapabilityContext => ({
46 release: { tag, version: tag.slice(1) },
47 baseline: baseline("v1.2.0", ids, summaries),
48 });
49
50 afterEach(() => {
51 vi.unstubAllGlobals();
52 vi.restoreAllMocks();
53 });
54
55 describe("a registry row is available only at or below the published release", () => {
56 const fixture = row("fixture-capability", "1.3.0");
57
58 it("never renders as available while its since version is above the published release", () => {
59 expect(capabilityAvailability(fixture, at("v1.2.0"))).toBe("in_source");
60 expect(capabilityViews(at("v1.2.0"), [fixture]).map((view) => view.availability)).toEqual(["in_source"]);
61 expect(claimedItems([{ capability: { feature: fixture.id } }], at("v1.2.0"), [fixture])[0].availability).toBe("in_source");
62 });
63
64 it("renders as available at or below it", () => {
65 expect(capabilityAvailability(row("older", "1.1.0"), at("v1.2.0"))).toBe("released");
66 expect(capabilityAvailability(row("same", "1.2.0"), at("v1.2.0"))).toBe("released");
67 });
68
69 it("turns on when only the published release is raised", () => {
70 const before = capabilityAvailability(fixture, at("v1.2.9"));
71 const after = capabilityAvailability(fixture, at("v1.3.0"));
72 expect([before, after]).toEqual(["in_source", "released"]);
73 });
74
75 it("treats unreleased as source only and an unknown release or malformed version as hidden", () => {
76 expect(capabilityAvailability(row("pending", "unreleased"), at("v9.9.9"))).toBe("in_source");
77 expect(capabilityAvailability(fixture, { release: null, baseline: null })).toBe("hidden");
78 expect(capabilityAvailability(row("odd", "soon"), at("v9.9.9"))).toBe("hidden");
79 });
80
81 it("uses the registry at the published tag for rows with no recorded since", () => {
82 const unrecorded = row("unrecorded", null);
83 expect(capabilityAvailability(unrecorded, at("v1.2.0", ["unrecorded"]))).toBe("released");
84 expect(capabilityAvailability(unrecorded, at("v1.2.0", ["other"]))).toBe("in_source");
85 expect(capabilityAvailability(unrecorded, at("v1.3.0", ["unrecorded"]))).toBe("hidden");
86 expect(capabilityAvailability(unrecorded, { release: { tag: "v1.2.0", version: "1.2.0" }, baseline: null })).toBe("hidden");
87 });
88
89 it("shows the wording the published release had, not the newer source wording", () => {
90 const changed = row("changed", null, { summary: "Newer wording" });
91 const context = at("v1.2.0", ["changed"], { changed: "Wording at the tag" });
92 const [view] = capabilityViews(context, [changed]);
93 expect(view.summary).toBe("Wording at the tag");
94 expect(view.name).toBe("Tagged changed");
95 });
96
97 it("holds back a claim that describes the current row when the row changed since the release", () => {
98 const changed = row("changed", null, { summary: "Newer wording" });
99 const claim = { feature: "changed", describesCurrentRow: true };
100 expect(claimAvailability(claim, at("v1.2.0", ["changed"], { changed: "Older wording" }), [changed])).toBe("in_source");
101 expect(claimAvailability(claim, at("v1.2.0", ["changed"], { changed: "Newer wording" }), [changed])).toBe("released");
102 expect(claimAvailability({ feature: "changed" }, at("v1.2.0", ["changed"], { changed: "Older wording" }), [changed])).toBe("released");
103 expect(claimAvailability({ feature: "missing-row" }, at("v1.2.0"), [changed])).toBe("hidden");
104 });
105 });
106
107 describe("founder holds stay hidden after a tag", () => {
108 const far = at("v9.9.9");
109
110 it("hides cloud computer rows and vendor agents over ACP, whatever the registry says", () => {
111 const cloud = row("cloud-computer-dispatch", "0.1.0");
112 const acp = row("acp-agents", "0.1.0", { summary: "Drive Claude Code and Codex as agents over ACP." });
113 const acpReverse = row("acp-other", "0.1.0", { summary: "ACP lets Codewhale drive other agents such as Codex." });
114 for (const feature of [cloud, acp, acpReverse]) {
115 expect(isFounderHeld(feature), feature.id).toBe(true);
116 expect(capabilityAvailability(feature, far), feature.id).toBe("hidden");
117 expect(capabilityViews(far, [feature])).toEqual([]);
118 }
119 expect(capabilityAvailability(row("acp-client", "0.1.0", { summary: "ACP stdio connects compatible editors." }), far)).toBe("released");
120 });
121
122 it("hides rows about Codewhale plans or pricing until plans are published", () => {
123 const plans = row("plan-row", "0.1.0", { summary: "Codewhale plans include a monthly allowance." });
124 expect(capabilityAvailability(plans, far)).toBe("hidden");
125 expect(capabilityAvailability(plans, { ...far, switches: { ...FOUNDER_SWITCH_VALUES, plansPublished: true } })).toBe("released");
126 expect(capabilityAvailability(row("plan-mode", "0.1.0", { summary: "Plan and Work modes." }), far)).toBe("released");
127 });
128
129 it("opens only when the committed switch is turned on", () => {
130 const acp = row("acp-agents", "0.1.0", { summary: "Drive Claude Code over ACP." });
131 expect(capabilityAvailability(acp, { ...far, switches: { ...FOUNDER_SWITCH_VALUES, namedVendorAgentsOverAcp: true } })).toBe("released");
132 });
133
134 it("are committed off with a decision reference and not read from the environment", () => {
135 for (const key of ["cloudComputersBeyondInvite", "namedVendorAgentsOverAcp", "plansPublished"] as const) {
136 expect(FOUNDER_SWITCHES[key].value, key).toBe(false);
137 expect(FOUNDER_SWITCHES[key].decision, key).toMatch(/§\d/);
138 }
139 });
140
141 it("hold every real registry row about cloud computers", () => {
142 const context = runtimeCapabilityContext(FACTS);
143 const listed = new Set(capabilityViews(context).map((view) => view.feature.id));
144 expect(listed.has("cloud-computer-dispatch")).toBe(false);
145 expect(listed.has("account-computer-files")).toBe(false);
146 });
147 });
148
149 describe("the committed registry and baseline", () => {
150 const context = runtimeCapabilityContext(FACTS);
151
152 it("has a baseline for exactly the published release", () => {
153 expect(RELEASE_BASELINE).not.toBeNull();
154 expect(baselineFor(context)?.tag).toBe(FACTS.latestPublishedRelease?.tag);
155 });
156
157 it("never lists a row newer than the published release as available", () => {
158 const published = context.release!.version;
159 const tagged = new Set(RELEASE_BASELINE!.features.map((feature) => feature.id));
160 for (const view of capabilityViews(context, FEATURE_CATALOGUE.features)) {
161 if (view.availability !== "released") continue;
162 const { since, id } = view.feature;
163 expect(since === "unreleased", id).toBe(false);
164 if (since) {
165 const [a, b, c] = since.split(".").map(Number);
166 const [x, y, z] = published.split(".").map(Number);
167 expect(a * 1e6 + b * 1e3 + c, id).toBeLessThanOrEqual(x * 1e6 + y * 1e3 + z);
168 } else {
169 expect(tagged.has(id), id).toBe(true);
170 }
171 }
172 });
173
174 it("keeps main-only rows out of the structured-data feature list", () => {
175 const names = releasedFeatureNames(context.release!.version);
176 for (const view of capabilityViews(context)) {
177 if (view.availability === "in_source") expect(names).not.toContain(view.name);
178 }
179 });
180
181 it("rejects a baseline that is malformed or names another format", () => {
182 expect(parseReleaseBaseline(null)).toBeNull();
183 expect(parseReleaseBaseline({ ...RELEASE_BASELINE, format: "other" })).toBeNull();
184 expect(parseReleaseBaseline({ ...RELEASE_BASELINE, tag: "v0.0.1" })).toBeNull();
185 expect(parseReleaseBaseline({ ...RELEASE_BASELINE, models: [] })).toBeNull();
186 });
187
188 it("every capability key in the product copy resolves to a registry row", () => {
189 const ids = new Set(FEATURE_CATALOGUE.features.map((feature) => feature.id));
190 const copy = [...PRODUCT_COPY.workPatterns, ...PRODUCT_COPY.modes, ...PRODUCT_COPY.permissions];
191 for (const item of copy) expect(ids.has(item.capability!.feature), item.capability!.feature).toBe(true);
192 });
193 });
194
195 describe("counts are the counts at the published release tag", () => {
196 const view = releaseView(FACTS);
197
198 it("come from the tagged source and name what they count", () => {
199 expect(view.counts).toEqual({
200 providerRoutes: RELEASE_BASELINE!.providers.length,
201 models: RELEASE_BASELINE!.models.length,
202 tools: RELEASE_BASELINE!.toolCount,
203 });
204 expect(view.providers).toHaveLength(view.counts!.providerRoutes);
205 });
206
207 it("are unknown, not guessed, when the baseline is for a different release", () => {
208 const other = { latestPublishedRelease: { ...FACTS.latestPublishedRelease!, tag: "v9.9.9", version: "9.9.9" } };
209 expect(releaseView(other)).toEqual({ providers: [], models: [], counts: null });
210 expect(releaseView({ latestPublishedRelease: null })).toEqual({ providers: [], models: [], counts: null });
211 });
212
213 it("leave main-only models out of the models page", async () => {
214 const props = { params: Promise.resolve({ locale: "en" }), searchParams: Promise.resolve({}) } as never;
215 const render = async () => renderToStaticMarkup((await ModelsPage(props)) as never);
216 const tagged = RELEASE_BASELINE!.models;
217 const saved = [...tagged];
218 const full = await render();
219 const onMain = new Set(FACTS.models.map((model) => model.id));
220 const index = tagged.findIndex((model) => onMain.has(model.id) && model.id !== FACTS.defaultModel && full.includes(`>${model.id}<`));
221 expect(index).toBeGreaterThanOrEqual(0);
222 const { id } = tagged[index];
223 try {
224 tagged.splice(index, 1);
225 const models = await render();
226 expect(models).not.toContain(`>${id}<`);
227 expect(models).toContain(`Show all ${view.counts!.providerRoutes} provider routes`);
228 expect(models).not.toMatch(/Show all \d+ providers/);
229 } finally {
230 tagged.splice(0, tagged.length, ...saved);
231 }
232 });
233
234 it("print provider routes in llms.txt at the tag", () => {
235 const text = buildLlmsTxt();
236 expect(text).toContain(`Built-in provider routes in ${FACTS.latestPublishedRelease!.tag} (${view.counts!.providerRoutes})`);
237 });
238
239 it("omit every counted sentence when the count is unknown", async () => {
240 const other = { ...FACTS, latestPublishedRelease: { ...FACTS.latestPublishedRelease!, tag: "v9.9.9", version: "9.9.9" } };
241 expect(releaseView(other).counts).toBeNull();
242 });
243 });
244
245 describe("no literal count in copy", () => {
246 const COUNT_WORDS = "(?:\\d[\\d,]*\\+?|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve|thirteen|fourteen|fifteen|sixteen|seventeen|eighteen|nineteen|twenty|thirty|forty|fifty|sixty|seventy|eighty|ninety|hundred)";
247 const NOUNS = "(?:built-in )?(?:provider routes|providers|models|tools|native tools|locales|languages|routes)";
248 const PATTERN = new RegExp(`\\b${COUNT_WORDS} ${NOUNS}\\b`, "i");
249 const ZH = /\d+\s*(?:个|条|种)\s*(?:提供商|模型|工具|语言|路由)/;
250 const ALLOWED: Record<string, string> = {
251 "lib/content/tools.ts": "native tools for reading, editing",
252 "lib/i18n/dictionaries/en/docs-mcp.ts": "listed those two tools",
253 };
254
255 function sources(dir: string): string[] {
256 return readdirSync(join(WEB, dir)).flatMap((name) => {
257 const path = join(dir, name);
258 if (statSync(join(WEB, path)).isDirectory()) return sources(path);
259 return /\.(ts|tsx)$/.test(name) && !/\.(test|generated)\./.test(name) ? [path] : [];
260 });
261 }
262
263 it.each([...sources("lib/content"), ...sources("lib/i18n/dictionaries"), ...sources("app"), ...sources("components")])("%s", (path) => {
264 const text = readFileSync(join(WEB, path), "utf8");
265 const hit = text.split("\n").find((line) => !/^\s*(?:\*|\/\/|\/\*)/.test(line)
266 && !(ALLOWED[path] && line.includes(ALLOWED[path])) && (PATTERN.test(line) || ZH.test(line)));
267 expect(hit, hit).toBeUndefined();
268 });
269 });
270
270 lines TYPESCRIPT