Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions apps/mobile/src/a11y-scan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { contrast, stripComments, unlabelledControls } from "./a11y-scan.ts";
import { palettes } from "./theme.ts";

const SRC = new URL(".", import.meta.url).pathname;
const files = ["app", "components"].flatMap((dir) => readdirSync(join(SRC, dir)).filter((f) => f.endsWith(".tsx")).map((f) => join(dir, f))).concat(["app-lock.tsx"]);
const SRC = fileURLToPath(new URL(".", import.meta.url));
const files = [
...["app", "components"].flatMap((dir) =>
readdirSync(join(SRC, dir), { recursive: true })
.map(String)
.filter((f) => f.endsWith(".tsx"))
.map((f) => join(dir, f))
),
...readdirSync(SRC).filter((f) => f.endsWith(".tsx")),
];

test("the scanner walks nested route folders and top-level screens", () => {
assert.ok(files.includes(join("app", "oauth", "mcp.tsx")));
assert.ok(files.includes("app-lock.tsx"));
});

test("the scanner flags a Pressable without a role or a label", () => {
const src = `<Pressable onPress={() => go("/x")} style={s}>\n<Text>{t("a.b")}</Text></Pressable>`;
Expand Down
60 changes: 60 additions & 0 deletions apps/mobile/src/copy-keys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// <reference types="node" />
// CT-10: every literal copy key named in apps/mobile/src and packages/*/src exists in both locales.
import { test } from "node:test";
import assert from "node:assert/strict";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { extractCopyKeys } from "./copy-keys.ts";

const SRC = fileURLToPath(new URL(".", import.meta.url));
const ROOT = fileURLToPath(new URL("../../..", import.meta.url));

const loadLocale = (name: string): Record<string, string> =>
JSON.parse(readFileSync(join(ROOT, "packages", "copy", "locales", `${name}.json`), "utf8")) as Record<string, string>;

function collectSourceFiles(dir: string, labelPrefix: string): Array<{ label: string; fullPath: string }> {
return readdirSync(dir, { recursive: true })
.map(String)
.filter((f) => /\.(ts|tsx)$/.test(f) && !/\.test\.(ts|tsx)$/.test(f))
.map((f) => ({ label: join(labelPrefix, f), fullPath: join(dir, f) }));
}

test("extractCopyKeys finds literal keys and skips templates and non-keys", () => {
const src = [
`const a = t("HM-SET-01.title");`,
`const b = t('common.ok');`,
"const c = `HM-APPR-01.approve`;",
"const dyn = `HM-SET-10.failed.${reason}`;",
`const other = "not a copy key";`,
`const partial = "HM-SET-10";`,
].join("\n");
assert.deepEqual(extractCopyKeys(src), ["HM-SET-01.title", "common.ok", "HM-APPR-01.approve"]);
});

test("CT-10: every literal copy key in code exists in both en and es-419 locales", () => {
const locales: Array<[string, Record<string, string>]> = [
["en", loadLocale("en")],
["es-419", loadLocale("es-419")],
];
const packagesDir = join(ROOT, "packages");
const pkgFiles = readdirSync(packagesDir).flatMap((pkg) => {
const srcDir = join(packagesDir, pkg, "src");
try {
return collectSourceFiles(srcDir, join("packages", pkg, "src"));
} catch {
return [];
}
});
const allFiles = [...collectSourceFiles(SRC, join("apps", "mobile", "src")), ...pkgFiles];
const missing: string[] = [];
for (const { label, fullPath } of allFiles) {
const text = readFileSync(fullPath, "utf8");
for (const key of extractCopyKeys(text)) {
for (const [localeName, dict] of locales) {
if (!Object.hasOwn(dict, key)) missing.push(`${label}: missing ${key} in ${localeName}`);
}
}
}
assert.deepEqual(missing, []);
});
16 changes: 16 additions & 0 deletions apps/mobile/src/copy-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// CT-10: pure source scan for copy keys written as whole string literals.
import { KEY_PATTERN } from "@hermit/copy";

const LITERAL_RE = /"([^"\\\n]*)"|'([^'\\\n]*)'|`([^`\\\n]*)`/g;

/** Returns every copy key written as a whole string literal ("...", '...', or `...` without `${`). */
export function extractCopyKeys(source: string): string[] {
const keys: string[] = [];
for (const match of source.matchAll(LITERAL_RE)) {
const template = match[3];
if (template !== undefined && template.includes("${")) continue;
const value = match[1] ?? match[2] ?? template ?? "";
if (KEY_PATTERN.test(value)) keys.push(value);
}
return keys;
}
4 changes: 3 additions & 1 deletion packages/core/test/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ describe("profile values (US-FORM-03)", () => {
test("a sealed value opens only with its key and for its own kind", async () => {
const key = webcrypto.getRandomValues(new Uint8Array(32));
const sealed = await sealProfileValue(cipher, key, "id_number", "1-2345-6789");
expect(sealed).not.toContain("2345");
// The sealed value is hex: check the decoded bytes, not the hex text.
const sealedBytes = Buffer.from(sealed, "hex");
expect(sealedBytes.includes(Buffer.from("1-2345-6789", "utf8"))).toBe(false);
expect(await openProfileValue(cipher, key, "id_number", sealed)).toBe("1-2345-6789");
await expect(openProfileValue(cipher, key, "passport_number", sealed)).rejects.toThrow();
await expect(openProfileValue(cipher, new Uint8Array(32), "id_number", sealed)).rejects.toThrow();
Expand Down
Loading