|
1 | 1 | // @vitest-environment jsdom |
2 | 2 | import { afterEach, expect, it, vi } from "vitest"; |
3 | 3 | import { createJoinJournal } from "./join-journal"; |
| 4 | +import * as destinations from "./destination"; |
| 5 | + |
| 6 | +const viewer = "a".repeat(64); |
| 7 | +const key = `buzz-community-joins.v1:${viewer}`; |
| 8 | +const resolveDestination = destinations.communityDestination; |
| 9 | +function restart(aliases: Record<string, string>) { |
| 10 | + vi.spyOn(destinations, "communityDestination").mockImplementation((value) => |
| 11 | + resolveDestination(value, aliases), |
| 12 | + ); |
| 13 | + return createJoinJournal(viewer); |
| 14 | +} |
4 | 15 |
|
5 | 16 | afterEach(() => { |
6 | 17 | localStorage.clear(); |
@@ -32,3 +43,113 @@ it("preserves malformed recovery data instead of overwriting it before a new cla |
32 | 43 | ); |
33 | 44 | expect(localStorage.getItem(key)).toBe("corrupt"); |
34 | 45 | }); |
| 46 | + |
| 47 | +it("recovers the same draft when an alias is added and removed across restarts", () => { |
| 48 | + const initial = restart({}).begin("wss://ONE.test:443/", { |
| 49 | + name: "Saved draft", |
| 50 | + picture: "", |
| 51 | + }); |
| 52 | + const aliased = restart({ primary: "https://one.test" }); |
| 53 | + expect(aliased.latest()).toEqual(initial); |
| 54 | + expect(aliased.get("primary")).toEqual(initial); |
| 55 | + expect(aliased.current(initial)).toBe(true); |
| 56 | + const replacement = aliased.begin("primary"); |
| 57 | + expect(replacement.profile).toEqual(initial.profile); |
| 58 | + expect(aliased.current(initial)).toBe(false); |
| 59 | + aliased.finish(initial); |
| 60 | + |
| 61 | + const unaliased = restart({}); |
| 62 | + expect(unaliased.latest()).toEqual(replacement); |
| 63 | + expect(unaliased.get("https://one.test")).toEqual(replacement); |
| 64 | + expect(unaliased.current(replacement)).toBe(true); |
| 65 | + unaliased.finish(replacement); |
| 66 | + expect(unaliased.latest()).toBeUndefined(); |
| 67 | +}); |
| 68 | + |
| 69 | +it("normalizes existing alias records to origins on the next journal write", () => { |
| 70 | + const legacy = { |
| 71 | + id: "legacy", |
| 72 | + community: "primary", |
| 73 | + profile: { name: "Legacy draft", picture: "" }, |
| 74 | + }; |
| 75 | + localStorage.setItem(key, JSON.stringify([legacy])); |
| 76 | + const configured = restart({ primary: "https://one.test" }); |
| 77 | + expect(configured.get("primary")).toEqual({ |
| 78 | + ...legacy, |
| 79 | + community: "https://one.test", |
| 80 | + }); |
| 81 | + configured.begin("https://two.test"); |
| 82 | + expect(restart({}).get("https://one.test")?.profile).toEqual(legacy.profile); |
| 83 | +}); |
| 84 | + |
| 85 | +it("retains unresolved legacy aliases without blocking unrelated recovery or joins", () => { |
| 86 | + const known = restart({}).begin("https://two.test"); |
| 87 | + const unresolved = { |
| 88 | + id: "unresolved", |
| 89 | + community: "old-alias", |
| 90 | + profile: { name: "Keep me", picture: "", about: "Unfinished" }, |
| 91 | + }; |
| 92 | + localStorage.setItem(key, JSON.stringify([known, unresolved])); |
| 93 | + const journal = restart({}); |
| 94 | + expect(journal.latest()).toEqual(known); |
| 95 | + expect(journal.get("https://two.test")).toEqual(known); |
| 96 | + const other = journal.begin("https://three.test"); |
| 97 | + journal.finish(known); |
| 98 | + journal.finish(other); |
| 99 | + expect(journal.latest()).toBeUndefined(); |
| 100 | + expect(JSON.parse(localStorage.getItem(key) ?? "null")).toEqual([unresolved]); |
| 101 | + |
| 102 | + const restored = restart({ "old-alias": "https://one.test" }); |
| 103 | + expect(restored.latest()).toEqual({ |
| 104 | + ...unresolved, |
| 105 | + community: "https://one.test", |
| 106 | + }); |
| 107 | + expect(restored.begin("https://one.test").profile).toEqual( |
| 108 | + unresolved.profile, |
| 109 | + ); |
| 110 | +}); |
| 111 | + |
| 112 | +it("keeps the newest draft when a restored alias resolves to an already pending origin", () => { |
| 113 | + localStorage.setItem( |
| 114 | + key, |
| 115 | + JSON.stringify([ |
| 116 | + { |
| 117 | + id: "legacy", |
| 118 | + community: "old-alias", |
| 119 | + profile: { name: "Old draft", picture: "" }, |
| 120 | + }, |
| 121 | + ]), |
| 122 | + ); |
| 123 | + const newer = restart({}).begin("https://one.test", { |
| 124 | + name: "New draft", |
| 125 | + picture: "", |
| 126 | + }); |
| 127 | + const restored = restart({ "old-alias": "https://one.test" }); |
| 128 | + expect(restored.latest()).toEqual(newer); |
| 129 | + expect(restored.get("old-alias")).toEqual(newer); |
| 130 | + expect(restored.get("https://one.test")).toEqual(newer); |
| 131 | + const retry = restored.begin("old-alias"); |
| 132 | + expect(retry.profile).toEqual(newer.profile); |
| 133 | + expect(restored.current(newer)).toBe(false); |
| 134 | + restored.finish(retry); |
| 135 | + expect(restored.latest()).toBeUndefined(); |
| 136 | + expect(JSON.parse(localStorage.getItem(key) ?? "null")).toEqual([]); |
| 137 | +}); |
| 138 | + |
| 139 | +it("preserves recovery records when device storage cannot be read", () => { |
| 140 | + const journal = restart({}); |
| 141 | + const entry = journal.begin("https://one.test", { |
| 142 | + name: "Keep me", |
| 143 | + picture: "", |
| 144 | + }); |
| 145 | + const reads = vi |
| 146 | + .spyOn(Storage.prototype, "getItem") |
| 147 | + .mockImplementation(() => { |
| 148 | + throw new Error("Storage unavailable"); |
| 149 | + }); |
| 150 | + expect(() => journal.begin("https://two.test")).toThrow( |
| 151 | + "Could not read unfinished", |
| 152 | + ); |
| 153 | + reads.mockRestore(); |
| 154 | + expect(journal.latest()).toEqual(entry); |
| 155 | +}); |
0 commit comments