feat(cli): migrate mode-scoped helpers to shared functions
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { afterEach, expect, test } from "bun:test";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { migrateModeFunctions } from "../src/migrations/mode-functions.ts";
|
||||
import { updateApp } from "../src/update.ts";
|
||||
|
||||
const SOURCE = `page Hello {
|
||||
ssr {
|
||||
functions {
|
||||
function userNames(users) {
|
||||
return users.map((user) => user.name).join(", ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
view { <main>x</main> }
|
||||
}
|
||||
`;
|
||||
|
||||
test("a mode helper becomes a shared function", () => {
|
||||
const result = migrateModeFunctions(SOURCE) as { source: string; changed: boolean };
|
||||
|
||||
expect(result.changed).toBe(true);
|
||||
expect(result.source).toContain("shared function userNames");
|
||||
expect(result.source).not.toContain("ssr {");
|
||||
});
|
||||
|
||||
test("running it again changes nothing", () => {
|
||||
const once = (migrateModeFunctions(SOURCE) as { source: string }).source;
|
||||
const twice = migrateModeFunctions(once) as { changed: boolean; source: string };
|
||||
|
||||
expect(twice.changed).toBe(false);
|
||||
expect(twice.source).toBe(once);
|
||||
});
|
||||
|
||||
test("a name that already exists at page level is skipped with a reason", () => {
|
||||
const clash = `page P {
|
||||
functions { shared function userNames() { return "" } }
|
||||
ssr { functions { function userNames(users) { return "" } } }
|
||||
view { <main>x</main> }
|
||||
}
|
||||
`;
|
||||
const result = migrateModeFunctions(clash) as { skip: string };
|
||||
|
||||
expect(result.skip).toContain("userNames");
|
||||
});
|
||||
|
||||
test("a helper body containing a brace inside a string literal survives", () => {
|
||||
const source = `page Hello {
|
||||
ssr {
|
||||
functions {
|
||||
function label(user) {
|
||||
return user.name + " {tag}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
view { <main>x</main> }
|
||||
}
|
||||
`;
|
||||
const result = migrateModeFunctions(source) as { source: string; changed: boolean };
|
||||
|
||||
expect(result.changed).toBe(true);
|
||||
expect(result.source).toContain("shared function label");
|
||||
expect(result.source).toContain('" {tag}"');
|
||||
});
|
||||
|
||||
test("both ssr and client declaring the same helper name is skipped with a reason", () => {
|
||||
const clash = `page P {
|
||||
ssr { functions { function helper() { return 1 } } }
|
||||
client { functions { function helper() { return 2 } } }
|
||||
view { <main>x</main> }
|
||||
}
|
||||
`;
|
||||
const result = migrateModeFunctions(clash) as { skip: string };
|
||||
|
||||
expect(result.skip).toContain("helper");
|
||||
});
|
||||
|
||||
const roots: string[] = [];
|
||||
afterEach(() => {
|
||||
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
function project(): string {
|
||||
const root = mkdtempSync(join(tmpdir(), "wrnexus-migrate-mode-functions-"));
|
||||
roots.push(root);
|
||||
mkdirSync(join(root, "app"), { recursive: true });
|
||||
writeFileSync(
|
||||
join(root, "package.json"),
|
||||
JSON.stringify({
|
||||
name: "mode-functions-migrate-app",
|
||||
dependencies: { "@wrnexus/core": "^0.8.0" },
|
||||
wrnexus: { version: "0.8.0" },
|
||||
}),
|
||||
);
|
||||
return root;
|
||||
}
|
||||
|
||||
test("one update run fully migrates a page mixing api entries and mode functions", () => {
|
||||
const root = project();
|
||||
writeFileSync(
|
||||
join(root, "app", "P.wrn"),
|
||||
`page P {
|
||||
ssr {
|
||||
api getUsers GET /api/users { response { return data } }
|
||||
functions { function label(u) { return u.name } }
|
||||
}
|
||||
view { <main>x</main> }
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
const report = {
|
||||
changedAutomatically: [] as string[],
|
||||
needsReview: [] as string[],
|
||||
unresolvedImports: [] as string[],
|
||||
ambiguousFunctions: [] as string[],
|
||||
legacyOutputPayloads: [] as string[],
|
||||
parseFailures: [] as string[],
|
||||
};
|
||||
|
||||
updateApp(root, "0.9.0", false, { report });
|
||||
|
||||
const migrated = readFileSync(join(root, "app", "P.wrn"), "utf8");
|
||||
|
||||
expect(migrated).toContain("apis {");
|
||||
expect(migrated).toContain("shared function label");
|
||||
expect(migrated).not.toContain("ssr {");
|
||||
expect(report.needsReview).toEqual([]);
|
||||
});
|
||||
Reference in New Issue
Block a user