1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
/**
* Unit tests for profile enumeration — no browser required.
*
* Builds throwaway user-data-dirs on disk so both tiers (Local State and the
* Preferences scan) run against real files.
*
* 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";
const roots: string[] = [];
const makeUdd = (): string => {
const dir = mkdtempSync(join(tmpdir(), "pi-profile-test-"));
roots.push(dir);
return dir;
};
const writeLocalState = (udd: string, body: unknown): void =>
writeFileSync(join(udd, "Local State"), typeof body === "string" ? body : JSON.stringify(body), "utf8");
const writeProfileDir = (udd: string, dir: string, prefs?: unknown): void => {
mkdirSync(join(udd, dir), { recursive: true });
if (prefs !== undefined) writeFileSync(join(udd, dir, "Preferences"), JSON.stringify(prefs), "utf8");
};
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: {
last_used: "Profile 1",
profiles_order: ["Default", "Profile 1", "Profile 2"],
info_cache: {
"Profile 2": { name: "Second", user_name: "second@example.com" },
Default: { name: "vertexcover.io", user_name: "aman@vertexcover.io" },
"Profile 1": { name: "Aman", user_name: "aman@personal.com" },
},
},
});
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" } } } });
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: {
info_cache: {
"Profile 4": { gaia_name: "Aman Kumar", user_name: "gaia@example.com" },
"Profile 5": { name: "Ghost", user_name: "ghost@example.com", is_ephemeral: true },
"System Profile": { name: "System" },
},
},
});
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: {
profiles_order: ["Profile 9"],
info_cache: {
Default: { name: "D" },
"Profile 9": { name: "Nine" },
"Profile 3": { name: "Three" },
},
},
});
const profiles = await listProfiles(udd);
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", {
profile: { name: "Fallback Name" },
account_info: [{ email: "fallback@example.com" }],
});
writeProfileDir(udd, "Crash Reports"); // no Preferences → not a profile
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);
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"));
assert.equal(profiles.length, 0);
});
// L8: label formatting is total
test("L8: email wins the brackets", () => {
assert.equal(formatProfileLabel("A", "a@b.c", "Default"), "A (a@b.c)");
});
test("L8: dir fills in without an email", () => {
assert.equal(formatProfileLabel("A", undefined, "Profile 7"), "A (Profile 7)");
});
test("L8: empty email is treated as absent", () => {
assert.equal(formatProfileLabel("A", "", "Profile 7"), "A (Profile 7)");
});
});
|