aboutsummaryrefslogtreecommitdiff
path: root/test/profile/list-test.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-27 00:27:08 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commit15a537a99efba614e6e108722462bcb78ec6c3c4 (patch)
tree2ff20907fee8ace7a071470d491fe7808dce6a63 /test/profile/list-test.ts
parentce5d808cb4cc3581d85b829656d1d478dc43e3a6 (diff)
test: port remaining suites to node:test
Mechanical port of the hand-rolled check() harness. Import-after-env ordering preserved so suites still read their sandbox dirs. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'test/profile/list-test.ts')
-rw-r--r--test/profile/list-test.ts158
1 files changed, 93 insertions, 65 deletions
diff --git a/test/profile/list-test.ts b/test/profile/list-test.ts
index ca39b73..b4397e3 100644
--- a/test/profile/list-test.ts
+++ b/test/profile/list-test.ts
@@ -4,25 +4,15 @@
* Builds throwaway user-data-dirs on disk so both tiers (Local State and the
* Preferences scan) run against real files.
*
- * Run: npx tsx test/profile/list-test.ts
+ * Run: npm test
*/
+import { test, after, describe } from "node:test";
+import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { formatProfileLabel, listProfiles } from "../../src/profile/list";
-let passed = 0;
-let failed = 0;
-const check = (cond: boolean, label: string): void => {
- if (cond) {
- passed++;
- console.log(` ✓ ${label}`);
- } else {
- failed++;
- console.error(` ✗ ${label}`);
- }
-};
-
const roots: string[] = [];
const makeUdd = (): string => {
const dir = mkdtempSync(join(tmpdir(), "pi-profile-test-"));
@@ -36,9 +26,13 @@ const writeProfileDir = (udd: string, dir: string, prefs?: unknown): void => {
if (prefs !== undefined) writeFileSync(join(udd, dir, "Preferences"), JSON.stringify(prefs), "utf8");
};
-async function main(): Promise<void> {
+describe("profile enumeration", () => {
+ after(() => {
+ for (const root of roots) rmSync(root, { recursive: true, force: true });
+ });
+
// L1: tier 1 — names, emails, ordering, last-used marker
- {
+ const makeL1Udd = (): string => {
const udd = makeUdd();
writeLocalState(udd, {
profile: {
@@ -51,28 +45,53 @@ async function main(): Promise<void> {
},
},
});
- const profiles = await listProfiles(udd);
- check(profiles.length === 3, "L1: all three profiles listed");
- check(
- profiles.map((p) => p.dir).join(",") === "Default,Profile 1,Profile 2",
- "L1: profiles_order drives ordering, not object key order",
- );
- check(profiles[1]?.label === "Aman (aman@personal.com)", "L1: label is 'Name (email)'");
- check(profiles[1]?.lastUsed === true, "L1: last_used profile is marked");
- check(profiles[0]?.lastUsed === false, "L1: other profiles are not marked");
- }
+ return udd;
+ };
+
+ test("L1: all three profiles listed", async () => {
+ const profiles = await listProfiles(makeL1Udd());
+ assert.equal(profiles.length, 3);
+ });
+
+ test("L1: profiles_order drives ordering, not object key order", async () => {
+ const profiles = await listProfiles(makeL1Udd());
+ assert.equal(profiles.map((p) => p.dir).join(","), "Default,Profile 1,Profile 2");
+ });
+
+ test("L1: label is 'Name (email)'", async () => {
+ const profiles = await listProfiles(makeL1Udd());
+ assert.equal(profiles[1]?.label, "Aman (aman@personal.com)");
+ });
+
+ test("L1: last_used profile is marked", async () => {
+ const profiles = await listProfiles(makeL1Udd());
+ assert.equal(profiles[1]?.lastUsed, true);
+ });
+
+ test("L1: other profiles are not marked", async () => {
+ const profiles = await listProfiles(makeL1Udd());
+ assert.equal(profiles[0]?.lastUsed, false);
+ });
// L2: profile with no signed-in account falls back to the directory
- {
+ const makeL2Udd = (): string => {
const udd = makeUdd();
writeLocalState(udd, { profile: { info_cache: { "Profile 3": { name: "Work" } } } });
- const profiles = await listProfiles(udd);
- check(profiles[0]?.label === "Work (Profile 3)", "L2: no account → 'Name (dir)'");
- check(profiles[0]?.email === undefined, "L2: email stays undefined");
- }
+ return udd;
+ };
+
+ test("L2: no account → 'Name (dir)'", async () => {
+ const profiles = await listProfiles(makeL2Udd());
+ assert.equal(profiles[0]?.label, "Work (Profile 3)");
+ });
+
+ test("L2: email stays undefined", async () => {
+ const profiles = await listProfiles(makeL2Udd());
+ assert.equal(profiles[0]?.email, undefined);
+ });
// L3: gaia_name backfills a missing name; ephemeral profiles are skipped
- {
+ const makeL3Udd = (): string => {
const udd = makeUdd();
writeLocalState(udd, {
profile: {
@@ -83,13 +102,21 @@ async function main(): Promise<void> {
},
},
});
- const profiles = await listProfiles(udd);
- check(profiles.length === 1, "L3: ephemeral and System Profile entries are excluded");
- check(profiles[0]?.label === "Aman Kumar (gaia@example.com)", "L3: gaia_name backfills a missing name");
- }
+ return udd;
+ };
+
+ test("L3: ephemeral and System Profile entries are excluded", async () => {
+ const profiles = await listProfiles(makeL3Udd());
+ assert.equal(profiles.length, 1);
+ });
+
+ test("L3: gaia_name backfills a missing name", async () => {
+ const profiles = await listProfiles(makeL3Udd());
+ assert.equal(profiles[0]?.label, "Aman Kumar (gaia@example.com)");
+ });
// L4: unlisted profiles are appended alphabetically after profiles_order
- {
+ test("L4: ordered entries first, remainder alphabetical", async () => {
const udd = makeUdd();
writeLocalState(udd, {
profile: {
@@ -102,14 +129,11 @@ async function main(): Promise<void> {
},
});
const profiles = await listProfiles(udd);
- check(
- profiles.map((p) => p.dir).join(",") === "Profile 9,Default,Profile 3",
- "L4: ordered entries first, remainder alphabetical",
- );
- }
+ assert.equal(profiles.map((p) => p.dir).join(","), "Profile 9,Default,Profile 3");
+ });
// L5: corrupt Local State falls through to the Preferences scan
- {
+ const makeL5Udd = (): string => {
const udd = makeUdd();
writeLocalState(udd, "{ this is not json");
writeProfileDir(udd, "Default", {
@@ -117,40 +141,44 @@ async function main(): Promise<void> {
account_info: [{ email: "fallback@example.com" }],
});
writeProfileDir(udd, "Crash Reports"); // no Preferences → not a profile
- const profiles = await listProfiles(udd);
- check(profiles.length === 1, "L5: only directories holding Preferences count as profiles");
- check(profiles[0]?.label === "Fallback Name (fallback@example.com)", "L5: tier 2 reads name + account email");
- }
+ return udd;
+ };
+
+ test("L5: only directories holding Preferences count as profiles", async () => {
+ const profiles = await listProfiles(makeL5Udd());
+ assert.equal(profiles.length, 1);
+ });
+
+ test("L5: tier 2 reads name + account email", async () => {
+ const profiles = await listProfiles(makeL5Udd());
+ assert.equal(profiles[0]?.label, "Fallback Name (fallback@example.com)");
+ });
// L6: missing info_cache also falls through to tier 2
- {
+ test("L6: missing info_cache → Preferences scan", async () => {
const udd = makeUdd();
writeLocalState(udd, { profile: { last_used: "Default" } });
writeProfileDir(udd, "Profile 1", { profile: { name: "Tier Two" } });
const profiles = await listProfiles(udd);
- check(profiles[0]?.label === "Tier Two (Profile 1)", "L6: missing info_cache → Preferences scan");
- }
+ assert.equal(profiles[0]?.label, "Tier Two (Profile 1)");
+ });
// L7: nothing readable → empty list, never a throw
- {
+ test("L7: unreadable user-data-dir yields an empty list", async () => {
const profiles = await listProfiles(join(tmpdir(), "pi-profile-does-not-exist-xyz"));
- check(profiles.length === 0, "L7: unreadable user-data-dir yields an empty list");
- }
+ assert.equal(profiles.length, 0);
+ });
// L8: label formatting is total
- {
- check(formatProfileLabel("A", "a@b.c", "Default") === "A (a@b.c)", "L8: email wins the brackets");
- check(formatProfileLabel("A", undefined, "Profile 7") === "A (Profile 7)", "L8: dir fills in without an email");
- check(formatProfileLabel("A", "", "Profile 7") === "A (Profile 7)", "L8: empty email is treated as absent");
- }
-
- for (const root of roots) rmSync(root, { recursive: true, force: true });
+ test("L8: email wins the brackets", () => {
+ assert.equal(formatProfileLabel("A", "a@b.c", "Default"), "A (a@b.c)");
+ });
- console.log(`\n${passed} passed, ${failed} failed`);
- process.exit(failed === 0 ? 0 : 1);
-}
+ test("L8: dir fills in without an email", () => {
+ assert.equal(formatProfileLabel("A", undefined, "Profile 7"), "A (Profile 7)");
+ });
-main().catch((e) => {
- console.error("profile list test failed:", e);
- process.exit(1);
+ test("L8: empty email is treated as absent", () => {
+ assert.equal(formatProfileLabel("A", "", "Profile 7"), "A (Profile 7)");
+ });
});