|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + migrateMachineOnboardingCompletion, |
| 6 | + readMachineOnboardingCompletion, |
| 7 | +} from "./machineOnboarding.ts"; |
| 8 | + |
| 9 | +// machineOnboarding.ts reads/writes window.localStorage directly, so we inject |
| 10 | +// a minimal in-memory storage into globalThis.window before each test and |
| 11 | +// restore it afterward. |
| 12 | + |
| 13 | +function createMemoryStorage(initial = {}) { |
| 14 | + const values = new Map(Object.entries(initial)); |
| 15 | + return { |
| 16 | + getItem: (key) => values.get(key) ?? null, |
| 17 | + setItem: (key, value) => values.set(key, String(value)), |
| 18 | + removeItem: (key) => values.delete(key), |
| 19 | + clear: () => values.clear(), |
| 20 | + key: (index) => Array.from(values.keys())[index] ?? null, |
| 21 | + get length() { |
| 22 | + return values.size; |
| 23 | + }, |
| 24 | + }; |
| 25 | +} |
| 26 | + |
| 27 | +function withFakeWindow(initial, fn) { |
| 28 | + const prev = globalThis.window; |
| 29 | + const storage = createMemoryStorage(initial); |
| 30 | + globalThis.window = { |
| 31 | + localStorage: storage, |
| 32 | + // location.href without a machineOnboarding=1 param → forceMachineOnboarding() returns false |
| 33 | + location: { href: "http://localhost/" }, |
| 34 | + }; |
| 35 | + try { |
| 36 | + return fn(storage); |
| 37 | + } finally { |
| 38 | + globalThis.window = prev; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const PUBKEY_A = |
| 43 | + "aaaaaa1111112222223333334444445555556666667777778888889999990000aa"; |
| 44 | +const PUBKEY_B = |
| 45 | + "bbbbbb1111112222223333334444445555556666667777778888889999990000bb"; |
| 46 | +const LEGACY_KEY = `buzz-onboarding-complete.v1:${PUBKEY_A}`; |
| 47 | +const V2_KEY = `buzz-machine-onboarding-complete.v2:${PUBKEY_A}`; |
| 48 | + |
| 49 | +// ── Fix A regression case ──────────────────────────────────────────────────── |
| 50 | + |
| 51 | +test("migrate_mismatched_community_pubkey_does_not_vouch_for_current_key", () => { |
| 52 | + // Community pubkey is PUBKEY_B (stale from a previous identity); current |
| 53 | + // pubkey is PUBKEY_A (freshly generated after a dev reset). The mismatch |
| 54 | + // must NOT grant completion. |
| 55 | + withFakeWindow({}, (storage) => { |
| 56 | + const result = migrateMachineOnboardingCompletion( |
| 57 | + PUBKEY_A, |
| 58 | + PUBKEY_B, // activeCommunityPubkey — a different identity's community |
| 59 | + false, |
| 60 | + ); |
| 61 | + assert.equal(result, false, "mismatched pubkey must not vouch"); |
| 62 | + assert.equal( |
| 63 | + storage.getItem(V2_KEY), |
| 64 | + null, |
| 65 | + "completion must not be written to storage", |
| 66 | + ); |
| 67 | + assert.equal( |
| 68 | + readMachineOnboardingCompletion(PUBKEY_A), |
| 69 | + false, |
| 70 | + "readMachineOnboardingCompletion must return false", |
| 71 | + ); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +// ── Matching pubkey vouches ────────────────────────────────────────────────── |
| 76 | + |
| 77 | +test("migrate_matching_community_pubkey_vouches_for_current_key", () => { |
| 78 | + // Community pubkey matches current pubkey → legitimate veteran, grant. |
| 79 | + withFakeWindow({}, (storage) => { |
| 80 | + const result = migrateMachineOnboardingCompletion( |
| 81 | + PUBKEY_A, |
| 82 | + PUBKEY_A, // same pubkey — community belongs to this identity |
| 83 | + false, |
| 84 | + ); |
| 85 | + assert.equal(result, true, "matching pubkey must vouch"); |
| 86 | + assert.equal(storage.getItem(V2_KEY), "true"); |
| 87 | + assert.equal(readMachineOnboardingCompletion(PUBKEY_A), true); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +// ── Absent community pubkey — regression case (Thufir pass 1) ─────────────── |
| 92 | + |
| 93 | +test("migrate_absent_community_pubkey_does_not_vouch_for_fresh_identity", () => { |
| 94 | + // Stale current-format community with no pubkey stamp (created before the |
| 95 | + // stamp was added, or from an older build) + freshly generated identity |
| 96 | + // after a dev reset. The absent pubkey must NOT vouch — this was the |
| 97 | + // reachable producer of the half-onboarded state. |
| 98 | + withFakeWindow({}, (storage) => { |
| 99 | + const result = migrateMachineOnboardingCompletion( |
| 100 | + PUBKEY_A, |
| 101 | + null, // absent pubkey — could be legacy OR an unstamped modern entry |
| 102 | + false, |
| 103 | + ); |
| 104 | + assert.equal( |
| 105 | + result, |
| 106 | + false, |
| 107 | + "absent pubkey must not vouch for a fresh identity", |
| 108 | + ); |
| 109 | + assert.equal( |
| 110 | + storage.getItem(V2_KEY), |
| 111 | + null, |
| 112 | + "completion must not be written to storage", |
| 113 | + ); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +// ── No community configured ────────────────────────────────────────────────── |
| 118 | + |
| 119 | +test("migrate_no_community_does_not_vouch_for_current_key", () => { |
| 120 | + // `undefined` = no active community at all. |
| 121 | + withFakeWindow({}, (storage) => { |
| 122 | + const result = migrateMachineOnboardingCompletion( |
| 123 | + PUBKEY_A, |
| 124 | + undefined, // no community |
| 125 | + false, |
| 126 | + ); |
| 127 | + assert.equal(result, false, "no community must not vouch"); |
| 128 | + assert.equal(storage.getItem(V2_KEY), null); |
| 129 | + }); |
| 130 | +}); |
| 131 | + |
| 132 | +// ── Legacy onboarding completion still grants (pubkey-scoped) ─────────────── |
| 133 | + |
| 134 | +test("migrate_legacy_completion_key_still_grants_migration", () => { |
| 135 | + withFakeWindow({ [LEGACY_KEY]: "true" }, (storage) => { |
| 136 | + const result = migrateMachineOnboardingCompletion( |
| 137 | + PUBKEY_A, |
| 138 | + undefined, // no community — but legacy key is present |
| 139 | + false, |
| 140 | + ); |
| 141 | + assert.equal(result, true, "legacy completion key must grant migration"); |
| 142 | + assert.equal(storage.getItem(V2_KEY), "true"); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +// ── Shared identity grants regardless of community ─────────────────────────── |
| 147 | + |
| 148 | +test("migrate_shared_identity_grants_regardless_of_community", () => { |
| 149 | + withFakeWindow({}, () => { |
| 150 | + const result = migrateMachineOnboardingCompletion( |
| 151 | + PUBKEY_A, |
| 152 | + undefined, // no community |
| 153 | + true, // isSharedIdentity |
| 154 | + ); |
| 155 | + assert.equal(result, true, "shared identity must always grant"); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
| 159 | +// ── Already-completed v2 key returns true without re-writing ───────────────── |
| 160 | + |
| 161 | +test("migrate_already_completed_pubkey_returns_true_immediately", () => { |
| 162 | + withFakeWindow({ [V2_KEY]: "true" }, (storage) => { |
| 163 | + // Pass mismatched community to prove early-exit, not community vouching. |
| 164 | + const result = migrateMachineOnboardingCompletion( |
| 165 | + PUBKEY_A, |
| 166 | + PUBKEY_B, |
| 167 | + false, |
| 168 | + ); |
| 169 | + assert.equal(result, true, "already-completed pubkey must return true"); |
| 170 | + // Value was already there; the function should not have touched it |
| 171 | + // (but a redundant write is also acceptable — just verify it's still true). |
| 172 | + assert.equal(storage.getItem(V2_KEY), "true"); |
| 173 | + }); |
| 174 | +}); |
0 commit comments