import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const pagePath = path.join(
repoRoot,
"src/app/(dashboard)/dashboard/api-manager/ApiManagerPageClient.tsx"
);
const providerModelPermissionListPath = path.join(
repoRoot,
"src/app/(dashboard)/dashboard/api-manager/components/ProviderModelPermissionList.tsx"
);
const messagesDir = path.join(repoRoot, "src/i18n/messages");
const selfServiceScopeMessageKeys = [
"selfServiceVisibility",
"selfServiceVisibilityDesc",
"ownUsageVisibility",
"ownUsageVisibilityDesc",
"sharedAccountQuotaVisibility",
"sharedAccountQuotaVisibilityDesc",
];
function readApiManagerPage() {
return fs.readFileSync(pagePath, "utf8");
}
test("permissions modal uses i18n for management access description", () => {
const source = readApiManagerPage();
const managementBlock = source.slice(
source.indexOf("{/* Management Access */}", source.indexOf("const PermissionsModal")),
source.indexOf("{/* Self-service Visibility */}", source.indexOf("const PermissionsModal"))
);
assert.match(managementBlock, /\{t\("managementAccessDesc"\)\}/);
assert.doesNotMatch(managementBlock, /Allow this API key to manage OmniRoute configuration\./);
});
test("API manager page renders purpose-first header", () => {
const source = readApiManagerPage();
const headerBlock = source.slice(
source.indexOf('
\s*Your app\s*);
assert.doesNotMatch(headerBlock, />\s*API key\s*);
assert.doesNotMatch(headerBlock, />\s*OmniRoute\s*);
assert.match(headerBlock, /setShowAddModal\(true\)/);
assert.match(headerBlock, /\{t\("createKey"\)\}/);
});
test("permissions modal converts API key expiration ISO timestamps to local datetime input values", () => {
const source = readApiManagerPage();
const expirationBlock = source.slice(
source.indexOf("{/* Expiration Date */}", source.indexOf("const PermissionsModal")),
source.indexOf("{/* Management Access */}", source.indexOf("const PermissionsModal"))
);
assert.match(expirationBlock, /value=\{toLocalDateTimeInputValue\(expiresAt\)\}/);
assert.match(expirationBlock, /const date = new Date\(val\)/);
assert.match(expirationBlock, /setExpiresAt\(date\.toISOString\(\)\)/);
assert.match(expirationBlock, /onClick=\{\(\) => setExpiresAt\(""\)\}/);
assert.match(expirationBlock, /\{tc\("clear"\)\}/);
assert.doesNotMatch(expirationBlock, /expiresAt\.slice\(0, 16\)/);
});
test("permissions modal switch buttons declare button type", () => {
const source = readApiManagerPage();
const modalStart = source.indexOf("const PermissionsModal");
const visibilityStart = source.indexOf("{/* Self-service Visibility */}", modalStart);
const visibilityEnd = source.indexOf("{/* Selected Models Summary", visibilityStart);
const selfServiceBlock = source.slice(visibilityStart, visibilityEnd);
const switchButtonCount = (selfServiceBlock.match(/role="switch"/g) ?? []).length;
const typedSwitchButtonCount = (
selfServiceBlock.match(/