Skip to content

Commit 3777f00

Browse files
authored
refactor(doctor): unify legacy auth migration ownership (#117361)
1 parent 7bd7d7d commit 3777f00

9 files changed

Lines changed: 232 additions & 860 deletions

‎src/commands/doctor-auth-canonical-api-key-alias.test.ts‎

Lines changed: 101 additions & 286 deletions
Large diffs are not rendered by default.

‎src/commands/doctor-auth-flat-profiles.test.ts‎

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ import {
2626
import {
2727
collectOpenAICodexAuthProfileStoreIdMap,
2828
maybeMigrateAuthProfileJsonStoresToSqlite,
29-
maybeRepairLegacyFlatAuthProfileStores,
3029
maybeRepairOpenAICodexAuthConfig,
31-
maybeRepairOpenAICodexAuthProfileStores,
3230
} from "./doctor-auth-flat-profiles.js";
3331
import type { DoctorPrompter } from "./doctor-prompter.js";
3432

@@ -1308,8 +1306,8 @@ describe("maybeMigrateAuthProfileJsonStoresToSqlite", () => {
13081306
});
13091307
});
13101308

1311-
describe("maybeRepairLegacyFlatAuthProfileStores", () => {
1312-
it("migrates legacy flat auth-profiles.json stores with a backup", async () => {
1309+
describe("legacy flat profiles through the canonical auth migration owner", () => {
1310+
it("migrates legacy flat auth-profiles.json stores with a receipted archive", async () => {
13131311
const state = await makeTestState();
13141312
const legacy = {
13151313
"ollama-windows": {
@@ -1319,16 +1317,14 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
13191317
};
13201318
const authPath = await writeLegacyAuthProfilesJson(state, legacy);
13211319

1322-
const result = await maybeRepairLegacyFlatAuthProfileStores({
1320+
const result = await maybeMigrateAuthProfileJsonStoresToSqlite({
13231321
cfg: {},
13241322
prompter: makePrompter(true),
13251323
now: () => 123,
13261324
});
13271325

13281326
expect(result.detected).toEqual([authPath]);
1329-
expect(result.changes).toStrictEqual([
1330-
`Migrated ${authPath} to the SQLite auth profile store (backup: ${authPath}.legacy-flat.123.bak).`,
1331-
]);
1327+
expect(result.changes).toEqual([expect.stringContaining("Migrated auth profile JSON")]);
13321328
expect(result.warnings).toStrictEqual([]);
13331329
expect(loadPersistedAuthProfileStore(state.agentDir())).toEqual({
13341330
version: 1,
@@ -1341,7 +1337,8 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
13411337
},
13421338
});
13431339
expect(fs.existsSync(authPath)).toBe(false);
1344-
expect(JSON.parse(fs.readFileSync(`${authPath}.legacy-flat.123.bak`, "utf8"))).toEqual(legacy);
1340+
const [archive] = listMigratedArchives(authPath);
1341+
expect(JSON.parse(fs.readFileSync(archive!, "utf8"))).toEqual(legacy);
13451342
});
13461343

13471344
it("preserves existing SQLite auth profiles when migrating a legacy flat store", async () => {
@@ -1364,16 +1361,14 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
13641361
const legacy = { openai: { apiKey: "sk-openai-flat" } };
13651362
const authPath = await writeLegacyAuthProfilesJson(state, legacy);
13661363

1367-
const result = await maybeRepairLegacyFlatAuthProfileStores({
1364+
const result = await maybeMigrateAuthProfileJsonStoresToSqlite({
13681365
cfg: {},
13691366
prompter: makePrompter(true),
13701367
now: () => 123,
13711368
});
13721369

13731370
expect(result.warnings).toStrictEqual([]);
1374-
expect(result.changes).toStrictEqual([
1375-
`Migrated ${authPath} to the SQLite auth profile store (backup: ${authPath}.legacy-flat.123.bak).`,
1376-
]);
1371+
expect(result.changes).toEqual([expect.stringContaining("Migrated auth profile JSON")]);
13771372
expect(loadPersistedAuthProfileStore(state.agentDir())?.profiles).toEqual({
13781373
"anthropic:default": {
13791374
type: "oauth",
@@ -1400,7 +1395,7 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
14001395
};
14011396
const authPath = await writeLegacyAuthProfilesJson(state, legacy);
14021397

1403-
const result = await maybeRepairLegacyFlatAuthProfileStores({
1398+
const result = await maybeMigrateAuthProfileJsonStoresToSqlite({
14041399
cfg: {},
14051400
prompter: makePrompter(false),
14061401
});
@@ -1430,15 +1425,16 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
14301425
const authPath = await writeLegacyAuthProfilesJson(state, legacy);
14311426
const cfg = {};
14321427

1433-
const result = await maybeRepairLegacyFlatAuthProfileStores({
1428+
const result = await maybeMigrateAuthProfileJsonStoresToSqlite({
14341429
cfg,
14351430
prompter: makePrompter(true),
14361431
now: () => 456,
14371432
});
14381433

14391434
expect(result.detected).toEqual([authPath]);
1440-
expect(result.changes).toStrictEqual([
1441-
`Moved aws-sdk profile metadata from ${authPath} to auth.profiles (backup: ${authPath}.aws-sdk-profile.456.bak).`,
1435+
expect(result.changes).toEqual([
1436+
expect.stringContaining("Migrated auth profile JSON"),
1437+
expect.stringContaining("Moved aws-sdk profile metadata"),
14421438
]);
14431439
expect(result.warnings).toStrictEqual([]);
14441440
expect(cfg).toEqual({
@@ -1451,19 +1447,16 @@ describe("maybeRepairLegacyFlatAuthProfileStores", () => {
14511447
},
14521448
},
14531449
});
1454-
expect(JSON.parse(fs.readFileSync(authPath, "utf8"))).toEqual({
1455-
version: 1,
1456-
profiles: {
1457-
"openrouter:default": {
1458-
type: "api_key",
1459-
provider: "openrouter",
1460-
key: "sk-openrouter",
1461-
},
1450+
expect(loadPersistedAuthProfileStore(state.agentDir())?.profiles).toEqual({
1451+
"openrouter:default": {
1452+
type: "api_key",
1453+
provider: "openrouter",
1454+
key: "sk-openrouter",
14621455
},
14631456
});
1464-
expect(JSON.parse(fs.readFileSync(`${authPath}.aws-sdk-profile.456.bak`, "utf8"))).toEqual(
1465-
legacy,
1466-
);
1457+
expect(fs.existsSync(authPath)).toBe(false);
1458+
const [archive] = listMigratedArchives(authPath);
1459+
expect(JSON.parse(fs.readFileSync(archive!, "utf8"))).toEqual(legacy);
14671460
});
14681461
});
14691462

@@ -1780,7 +1773,7 @@ describe("maybeRepairOpenAICodexAuthConfig", () => {
17801773
});
17811774
});
17821775

1783-
describe("maybeRepairOpenAICodexAuthProfileStores", () => {
1776+
describe("legacy OpenAI auth profiles through the canonical migration owner", () => {
17841777
it("collects the store-derived legacy OpenAI Codex profile id map", async () => {
17851778
const state = await makeTestState();
17861779
await writeLegacyAuthProfilesJson(state, {
@@ -1811,7 +1804,7 @@ describe("maybeRepairOpenAICodexAuthProfileStores", () => {
18111804
).toEqual([["openai-codex:default", "openai:chatgpt-default"]]);
18121805
});
18131806

1814-
it("renames legacy OpenAI Codex auth store profiles with a backup", async () => {
1807+
it("renames legacy OpenAI Codex auth profiles while archiving the untouched source", async () => {
18151808
const state = await makeTestState();
18161809
const legacy = {
18171810
version: 1,
@@ -1839,18 +1832,20 @@ describe("maybeRepairOpenAICodexAuthProfileStores", () => {
18391832
};
18401833
const authPath = await writeLegacyAuthProfilesJson(state, legacy);
18411834

1842-
const result = await maybeRepairOpenAICodexAuthProfileStores({
1835+
const result = await maybeMigrateAuthProfileJsonStoresToSqlite({
18431836
cfg: {},
18441837
env: state.env,
1838+
prompter: makePrompter(true),
18451839
now: () => 789,
18461840
});
18471841

18481842
expect(result.detected).toEqual([authPath]);
1849-
expect(result.changes).toStrictEqual([
1850-
`Migrated 1 OpenAI Codex auth profile(s) in ${authPath} to provider "openai" (backup: ${authPath}.openai-provider-unification.789.bak).`,
1843+
expect(result.changes).toEqual([
1844+
expect.stringContaining("Migrated auth profile JSON"),
1845+
`Migrated 1 OpenAI Codex auth profile(s) in ${authPath} to provider "openai".`,
18511846
]);
18521847
expect(result.warnings).toStrictEqual([]);
1853-
expect(JSON.parse(fs.readFileSync(authPath, "utf8"))).toEqual({
1848+
expect(loadPersistedAuthProfileStore(state.agentDir())).toEqual({
18541849
version: 1,
18551850
profiles: {
18561851
"openai:work": {
@@ -1874,9 +1869,9 @@ describe("maybeRepairOpenAICodexAuthProfileStores", () => {
18741869
},
18751870
},
18761871
});
1877-
expect(
1878-
JSON.parse(fs.readFileSync(`${authPath}.openai-provider-unification.789.bak`, "utf8")),
1879-
).toEqual(legacy);
1872+
expect(fs.existsSync(authPath)).toBe(false);
1873+
const [archive] = listMigratedArchives(authPath);
1874+
expect(JSON.parse(fs.readFileSync(archive!, "utf8"))).toEqual(legacy);
18801875
});
18811876

18821877
it("canonicalizes a mixed Codex store before importing it into SQLite", async () => {
@@ -1904,13 +1899,6 @@ describe("maybeRepairOpenAICodexAuthProfileStores", () => {
19041899
},
19051900
});
19061901

1907-
const providerRepair = await maybeRepairOpenAICodexAuthProfileStores({
1908-
cfg: {},
1909-
env: state.env,
1910-
now: () => 790,
1911-
});
1912-
expect(providerRepair.warnings).toStrictEqual([]);
1913-
19141902
const sqliteMigration = await maybeMigrateAuthProfileJsonStoresToSqlite({
19151903
cfg: {},
19161904
env: state.env,

0 commit comments

Comments
 (0)