Skip to content

Commit 119eb41

Browse files
committed
test(storage,index): codify org-preserve and no-org collapse identity rules
1 parent 0c1370e commit 119eb41

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

test/index.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ describe("OpenAIOAuthPlugin persistAccountPool", () => {
20642064
expect(mockStorage.accounts[0]?.accessToken).toBe("access-no-org-update");
20652065
});
20662066

2067-
it("does not merge ambiguous org-scoped matches when organization metadata is missing", async () => {
2067+
it("preserves org-scoped variants when organizationId differs despite shared account/refresh context", async () => {
20682068
const accountsModule = await import("../lib/accounts.js");
20692069
const authModule = await import("../lib/auth/auth.js");
20702070

@@ -2113,10 +2113,16 @@ describe("OpenAIOAuthPlugin persistAccountPool", () => {
21132113
expect(mockStorage.accounts).toHaveLength(2);
21142114
const orgScopedEntries = mockStorage.accounts.filter((account) => account.organizationId);
21152115
expect(orgScopedEntries).toHaveLength(2);
2116+
expect(orgScopedEntries.map((account) => account.organizationId).sort()).toEqual([
2117+
"org-a",
2118+
"org-b",
2119+
]);
21162120
expect(orgScopedEntries.some((account) => account.accessToken === "access-ambiguous")).toBe(true);
2121+
expect(mockStorage.activeIndex).toBe(0);
2122+
expect(mockStorage.activeIndexByFamily).toEqual({});
21172123
});
21182124

2119-
it("remaps active indices to merged org account when fallback variant is pruned", async () => {
2125+
it("allows no-org fallback collision collapse and safely remaps active indices", async () => {
21202126
const accountsModule = await import("../lib/accounts.js");
21212127
const authModule = await import("../lib/auth/auth.js");
21222128

@@ -2180,9 +2186,9 @@ describe("OpenAIOAuthPlugin persistAccountPool", () => {
21802186
expect(mockStorage.accounts).toHaveLength(3);
21812187
expect(mockStorage.accounts.some((account) => account.accountId === "token-shared")).toBe(false);
21822188
expect(mockStorage.accounts[1]?.accountId).toBe("org-shared");
2189+
expect(mockStorage.accounts[1]?.organizationId).toBe("org-keep");
21832190
expect(mockStorage.activeIndex).toBe(1);
2184-
expect(mockStorage.activeIndexByFamily.codex).toBe(1);
2185-
expect(mockStorage.activeIndexByFamily["gpt-5.1"]).toBe(1);
2191+
expect(mockStorage.activeIndexByFamily).toEqual({ codex: 1, "gpt-5.1": 1 });
21862192
});
21872193

21882194
it("keeps latest rate-limit reset windows when collapsing refresh-token duplicates", async () => {

test/storage.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ describe("storage", () => {
202202
expect(basename(path)).toMatch(/^unsafe-name-\d{8}-\d{9}-[a-f0-9]{6}\.json$/);
203203
});
204204

205-
it("collapses same-refresh imports when accountId differs", async () => {
205+
it("collapses same-refresh no-organization imports when accountId differs (legacy fallback)", async () => {
206206
await saveAccounts({
207207
version: 3,
208208
activeIndex: 0,
209+
activeIndexByFamily: { codex: 0, "gpt-5.1": 0 },
209210
accounts: [
210211
{
211212
accountId: "workspace-a",
@@ -239,6 +240,8 @@ describe("storage", () => {
239240
const loaded = await loadAccounts();
240241
expect(loaded?.accounts).toHaveLength(1);
241242
expect(loaded?.accounts[0]?.accountId).toBe("workspace-b");
243+
expect(loaded?.activeIndex).toBe(0);
244+
expect(loaded?.activeIndexByFamily?.codex).toBe(0);
242245
});
243246

244247
it("collapses same-organization records to newest during import and remaps active keys", async () => {
@@ -294,9 +297,10 @@ describe("storage", () => {
294297
expect(org1?.refreshToken).toBe("refresh-new");
295298
expect(loaded?.activeIndex).toBe(1);
296299
expect(loaded?.activeIndexByFamily?.codex).toBe(1);
300+
expect(loaded?.activeIndexByFamily?.["gpt-5.1"]).toBe(1);
297301
});
298302

299-
it("preserves same refresh token across different organizations during import", async () => {
303+
it("preserves same refresh token across different organizationId values during import", async () => {
300304
await fs.writeFile(
301305
exportPath,
302306
JSON.stringify({
@@ -325,6 +329,7 @@ describe("storage", () => {
325329

326330
const loaded = await loadAccounts();
327331
expect(loaded?.accounts).toHaveLength(2);
332+
expect(loaded?.accounts.every((account) => account.refreshToken === "shared-refresh")).toBe(true);
328333
const organizationIds = loaded?.accounts
329334
.map((account) => account.organizationId)
330335
.filter((organizationId): organizationId is string => typeof organizationId === "string");

0 commit comments

Comments
 (0)