46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
/**
|
|||
|
|
* Content-language allowlist sync (node:test).
|
||
|
|
* CONTENT_LANGUAGES codes must match apps/api/internal/company/language.go ContentLanguages.
|
||
|
|
*
|
||
|
|
* Run from apps/web:
|
||
|
|
* node --experimental-strip-types --test src/lib/content-languages.test.ts
|
||
|
|
*/
|
||
|
|
import assert from "node:assert/strict";
|
||
|
|
import { readFileSync } from "node:fs";
|
||
|
|
import { dirname, join } from "node:path";
|
||
|
|
import { describe, it } from "node:test";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
|
||
|
|
import { CONTENT_LANGUAGES } from "./content-languages.ts";
|
||
|
|
|
||
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const goLanguagePath = join(here, "../../../api/internal/company/language.go");
|
||
|
|
|
||
|
|
/** Extract quoted codes from `var ContentLanguages = []string{ ... }`. */
|
||
|
|
function parseGoContentLanguageCodes(src: string): string[] {
|
||
|
|
const marker = "var ContentLanguages = []string{";
|
||
|
|
const start = src.indexOf(marker);
|
||
|
|
assert.ok(start >= 0, "ContentLanguages slice not found in language.go");
|
||
|
|
const end = src.indexOf("}", start);
|
||
|
|
assert.ok(end > start, "ContentLanguages closing brace not found");
|
||
|
|
const block = src.slice(start, end);
|
||
|
|
const codes: string[] = [];
|
||
|
|
for (const match of block.matchAll(/"([a-z]{2})"/g)) {
|
||
|
|
codes.push(match[1]!);
|
||
|
|
}
|
||
|
|
assert.ok(codes.length > 0, "no language codes parsed from Go allowlist");
|
||
|
|
return codes;
|
||
|
|
}
|
||
|
|
|
||
|
|
describe("content-languages Web↔Go allowlist sync", () => {
|
||
|
|
it("CONTENT_LANGUAGES values match company.ContentLanguages / ParseLanguage allowlist", () => {
|
||
|
|
const goSrc = readFileSync(goLanguagePath, "utf8");
|
||
|
|
const goCodes = parseGoContentLanguageCodes(goSrc);
|
||
|
|
const webCodes = CONTENT_LANGUAGES.map((l) => l.value);
|
||
|
|
assert.deepEqual(
|
||
|
|
webCodes,
|
||
|
|
goCodes,
|
||
|
|
"Keep apps/web/src/lib/content-languages.ts in sync with apps/api/internal/company/language.go"
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|