|
1 | 1 | import { afterEach, describe, expect, test } from "bun:test"; |
2 | | -import { existsSync, mkdirSync, mkdtempSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { dirname, join } from "node:path"; |
5 | 5 | import { NativeProfileManager } from "../src/codex/native-profile-manager"; |
| 6 | +import { decryptNativeEnvelope, readNativeProfileVault } from "../src/codex/native-profile-store"; |
6 | 7 | import { NativeProfileError, type NativeProfileKey, type NativeProfileKeyProvider } from "../src/codex/native-profile-types"; |
7 | 8 |
|
8 | 9 | const roots: string[] = []; |
@@ -70,6 +71,31 @@ async function enrolledFixture() { |
70 | 71 | return { ...f, manager, sourceProfile: sourceProfile.profile, targetProfile: targetProfile.profile, stage }; |
71 | 72 | } |
72 | 73 |
|
| 74 | +async function leavePendingJournal(f: Awaited<ReturnType<typeof enrolledFixture>>): Promise<NativeProfileManager> { |
| 75 | + const authPath = f.manager.context.authPath; |
| 76 | + const journalPath = f.manager.context.journalPath; |
| 77 | + let authWrites = 0; |
| 78 | + const interrupted = new NativeProfileManager({ |
| 79 | + ...f.options, |
| 80 | + atomicWrite: async (path, content) => { |
| 81 | + if (path === authPath) { |
| 82 | + authWrites += 1; |
| 83 | + if (authWrites > 1) throw new Error("injected restore failure"); |
| 84 | + } |
| 85 | + if (path === journalPath && content.includes('"phase": "auth-replaced"')) { |
| 86 | + throw new Error("injected post-replacement failure"); |
| 87 | + } |
| 88 | + return atomic(path, content); |
| 89 | + }, |
| 90 | + }); |
| 91 | + let caught: unknown; |
| 92 | + try { await interrupted.switch("work", true); } catch (error) { caught = error; } |
| 93 | + expect(caught).toBeInstanceOf(NativeProfileError); |
| 94 | + expect((caught as NativeProfileError).code).toBe("AUTH_RESTORE_FAILED"); |
| 95 | + expect(existsSync(journalPath)).toBe(true); |
| 96 | + return interrupted; |
| 97 | +} |
| 98 | + |
73 | 99 | async function waitForPath(path: string): Promise<void> { |
74 | 100 | const deadline = Date.now() + 5_000; |
75 | 101 | while (!existsSync(path) && Date.now() < deadline) await Bun.sleep(10); |
@@ -363,6 +389,122 @@ describe("native main profile transactions", () => { |
363 | 389 | expect(existsSync(journalPath)).toBe(true); |
364 | 390 | }); |
365 | 391 |
|
| 392 | + test("pending recovery blocks register before auth or vault mutation", async () => { |
| 393 | + const f = await enrolledFixture(); |
| 394 | + await leavePendingJournal(f); |
| 395 | + const authBefore = readFileSync(f.manager.context.authPath, "utf8"); |
| 396 | + const vaultBefore = readFileSync(f.manager.context.vaultPath, "utf8"); |
| 397 | + const journalBefore = readFileSync(f.manager.context.journalPath, "utf8"); |
| 398 | + |
| 399 | + let caught: unknown; |
| 400 | + try { await f.manager.register("renamed"); } catch (error) { caught = error; } |
| 401 | + |
| 402 | + expect(caught).toBeInstanceOf(NativeProfileError); |
| 403 | + expect((caught as NativeProfileError).code).toBe("RECOVERY_REQUIRED"); |
| 404 | + expect((caught as NativeProfileError).message).toContain("ocx account main recover"); |
| 405 | + expect(readFileSync(f.manager.context.authPath, "utf8")).toBe(authBefore); |
| 406 | + expect(readFileSync(f.manager.context.vaultPath, "utf8")).toBe(vaultBefore); |
| 407 | + expect(readFileSync(f.manager.context.journalPath, "utf8")).toBe(journalBefore); |
| 408 | + }); |
| 409 | + |
| 410 | + test("pending recovery blocks prepareStage before creating staging plaintext", async () => { |
| 411 | + const f = await enrolledFixture(); |
| 412 | + await leavePendingJournal(f); |
| 413 | + const authBefore = readFileSync(f.manager.context.authPath, "utf8"); |
| 414 | + const vaultBefore = readFileSync(f.manager.context.vaultPath, "utf8"); |
| 415 | + const journalBefore = readFileSync(f.manager.context.journalPath, "utf8"); |
| 416 | + const stagingBefore = readdirSync(f.manager.context.stagingRoot); |
| 417 | + |
| 418 | + let caught: unknown; |
| 419 | + try { await f.manager.prepareStage(); } catch (error) { caught = error; } |
| 420 | + |
| 421 | + expect(caught).toBeInstanceOf(NativeProfileError); |
| 422 | + expect((caught as NativeProfileError).code).toBe("RECOVERY_REQUIRED"); |
| 423 | + expect(readdirSync(f.manager.context.stagingRoot)).toEqual(stagingBefore); |
| 424 | + expect(readFileSync(f.manager.context.authPath, "utf8")).toBe(authBefore); |
| 425 | + expect(readFileSync(f.manager.context.vaultPath, "utf8")).toBe(vaultBefore); |
| 426 | + expect(readFileSync(f.manager.context.journalPath, "utf8")).toBe(journalBefore); |
| 427 | + }); |
| 428 | + |
| 429 | + test("pending recovery blocks finishStage without deleting staged plaintext", async () => { |
| 430 | + const f = await enrolledFixture(); |
| 431 | + const stage = await f.manager.prepareStage(); |
| 432 | + const stagedEnvelope = envelope("account-third", "third"); |
| 433 | + writeFileSync(join(stage.stagingCodexHome, "auth.json"), stagedEnvelope); |
| 434 | + await leavePendingJournal(f); |
| 435 | + const authBefore = readFileSync(f.manager.context.authPath, "utf8"); |
| 436 | + const vaultBefore = readFileSync(f.manager.context.vaultPath, "utf8"); |
| 437 | + const journalBefore = readFileSync(f.manager.context.journalPath, "utf8"); |
| 438 | + |
| 439 | + let caught: unknown; |
| 440 | + try { await f.manager.finishStage(stage.stageId, "third"); } catch (error) { caught = error; } |
| 441 | + |
| 442 | + expect(caught).toBeInstanceOf(NativeProfileError); |
| 443 | + expect((caught as NativeProfileError).code).toBe("RECOVERY_REQUIRED"); |
| 444 | + expect(readFileSync(join(stage.stagingCodexHome, "auth.json"), "utf8")).toBe(stagedEnvelope); |
| 445 | + expect(readFileSync(f.manager.context.authPath, "utf8")).toBe(authBefore); |
| 446 | + expect(readFileSync(f.manager.context.vaultPath, "utf8")).toBe(vaultBefore); |
| 447 | + expect(readFileSync(f.manager.context.journalPath, "utf8")).toBe(journalBefore); |
| 448 | + }); |
| 449 | + |
| 450 | + test("explicit rollback preserves a digest-changed target envelope before restoring source auth", async () => { |
| 451 | + const f = await enrolledFixture(); |
| 452 | + await leavePendingJournal(f); |
| 453 | + const refreshedTarget = envelope("account-target", "target-refreshed"); |
| 454 | + writeFileSync(f.manager.context.authPath, refreshedTarget); |
| 455 | + |
| 456 | + const result = await f.manager.recover(true, true); |
| 457 | + |
| 458 | + expect(result).toMatchObject({ recovered: true, action: "rollback-source", restartRequired: true }); |
| 459 | + expect(readFileSync(f.manager.context.authPath, "utf8")).toBe(f.source); |
| 460 | + expect(existsSync(f.manager.context.journalPath)).toBe(false); |
| 461 | + const vaultText = readFileSync(f.manager.context.vaultPath, "utf8"); |
| 462 | + expect(vaultText).not.toContain("target-refreshed"); |
| 463 | + expect(vaultText).not.toContain("opaque-access-target-refreshed"); |
| 464 | + expect(vaultText).not.toContain("opaque-refresh-target-refreshed"); |
| 465 | + const vault = readNativeProfileVault(f.manager.context)!; |
| 466 | + expect(vault.activeProfileId).toBe(f.sourceProfile.id); |
| 467 | + const targetProfile = vault.profiles.find(profile => profile.id === f.targetProfile.id)!; |
| 468 | + expect(targetProfile.state).toBe("inactive"); |
| 469 | + const key = await f.keyProvider.get(f.manager.context.homeId); |
| 470 | + expect(key).not.toBeNull(); |
| 471 | + const decrypted = decryptNativeEnvelope( |
| 472 | + f.manager.context, |
| 473 | + targetProfile.id, |
| 474 | + targetProfile.identityHash, |
| 475 | + targetProfile.payload!, |
| 476 | + key!, |
| 477 | + ); |
| 478 | + try { expect(decrypted.text).toBe(refreshedTarget); } finally { decrypted.raw.fill(0); key!.key.fill(0); } |
| 479 | + expect(f.transitions).toEqual(["account-target->account-source"]); |
| 480 | + }); |
| 481 | + |
| 482 | + test("rollback preservation failure leaves refreshed target auth untouched and journaled", async () => { |
| 483 | + const f = await enrolledFixture(); |
| 484 | + await leavePendingJournal(f); |
| 485 | + const refreshedTarget = envelope("account-target", "target-refreshed-failure"); |
| 486 | + writeFileSync(f.manager.context.authPath, refreshedTarget); |
| 487 | + const vaultBefore = readFileSync(f.manager.context.vaultPath, "utf8"); |
| 488 | + const failing = new NativeProfileManager({ |
| 489 | + ...f.options, |
| 490 | + atomicWrite: async (path, content) => { |
| 491 | + if (path === f.manager.context.vaultPath) throw new Error("injected rollback vault failure"); |
| 492 | + return atomic(path, content); |
| 493 | + }, |
| 494 | + }); |
| 495 | + |
| 496 | + let caught: unknown; |
| 497 | + try { await failing.recover(true, true); } catch (error) { caught = error; } |
| 498 | + |
| 499 | + expect(caught).toBeInstanceOf(Error); |
| 500 | + expect(readFileSync(f.manager.context.authPath, "utf8")).toBe(refreshedTarget); |
| 501 | + expect(readFileSync(f.manager.context.vaultPath, "utf8")).toBe(vaultBefore); |
| 502 | + expect(existsSync(f.manager.context.journalPath)).toBe(true); |
| 503 | + const journalText = readFileSync(f.manager.context.journalPath, "utf8"); |
| 504 | + expect(journalText).not.toContain("target-refreshed-failure"); |
| 505 | + expect(journalText).not.toContain("opaque-refresh-target-refreshed-failure"); |
| 506 | + }); |
| 507 | + |
366 | 508 | test("non-file Codex credential stores fail before vault or auth mutation", async () => { |
367 | 509 | const f = fixture(); |
368 | 510 | writeFileSync(join(f.codexHome, "config.toml"), 'cli_auth_credentials_store = "auto"\n'); |
|
0 commit comments