|
1 | 1 | import assert from "node:assert/strict"; |
2 | 2 | import test from "node:test"; |
3 | 3 |
|
4 | | -import { setLocalStorageItemWithRecovery } from "./localStorageQuota.ts"; |
| 4 | +import { |
| 5 | + recoverLocalStorageQuotaOnStartup, |
| 6 | + setLocalStorageItemWithRecovery, |
| 7 | +} from "./localStorageQuota.ts"; |
5 | 8 |
|
6 | 9 | function makeQuotaLocalStorage({ maxEntries }) { |
7 | 10 | const store = new Map(); |
@@ -30,6 +33,144 @@ function install(ls) { |
30 | 33 | globalThis.localStorage = ls; |
31 | 34 | } |
32 | 35 |
|
| 36 | +test("startup recovery removes disposable caches but preserves user state", () => { |
| 37 | + const ls = makeQuotaLocalStorage({ maxEntries: 5 }); |
| 38 | + install(ls); |
| 39 | + ls.store.set("buzz-channel-messages.v1:relay:chan", "big"); |
| 40 | + ls.store.set("buzz-channels.v1:relay", "big"); |
| 41 | + ls.store.set("buzz-timeline-skeleton-shape.v1:chan", "small"); |
| 42 | + ls.store.set("buzz-sidebar-skeleton-shape.v1:community:user", "small"); |
| 43 | + ls.store.set("buzz-communities", "keep"); |
| 44 | + |
| 45 | + recoverLocalStorageQuotaOnStartup(); |
| 46 | + |
| 47 | + assert.equal(ls.getItem("buzz-channel-messages.v1:relay:chan"), null); |
| 48 | + assert.equal(ls.getItem("buzz-channels.v1:relay"), null); |
| 49 | + assert.equal(ls.getItem("buzz-timeline-skeleton-shape.v1:chan"), null); |
| 50 | + assert.equal( |
| 51 | + ls.getItem("buzz-sidebar-skeleton-shape.v1:community:user"), |
| 52 | + null, |
| 53 | + ); |
| 54 | + assert.equal(ls.getItem("buzz-communities"), "keep"); |
| 55 | + assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1"); |
| 56 | +}); |
| 57 | + |
| 58 | +test("healthy startup preserves disposable caches", () => { |
| 59 | + const ls = makeQuotaLocalStorage({ maxEntries: 10 }); |
| 60 | + install(ls); |
| 61 | + ls.store.set("buzz-channel-messages.v1:relay:new", "snapshot"); |
| 62 | + |
| 63 | + recoverLocalStorageQuotaOnStartup(); |
| 64 | + |
| 65 | + assert.equal(ls.getItem("buzz-channel-messages.v1:relay:new"), "snapshot"); |
| 66 | + assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1"); |
| 67 | +}); |
| 68 | + |
| 69 | +test("startup recovery does not remove namespace near misses", () => { |
| 70 | + const ls = makeQuotaLocalStorage({ maxEntries: 2 }); |
| 71 | + install(ls); |
| 72 | + ls.store.set("buzz-channels.v10:durable", "keep"); |
| 73 | + ls.store.set("buzz-channel-messages.v1-durable", "keep"); |
| 74 | + |
| 75 | + recoverLocalStorageQuotaOnStartup(); |
| 76 | + |
| 77 | + assert.equal(ls.getItem("buzz-channels.v10:durable"), "keep"); |
| 78 | + assert.equal(ls.getItem("buzz-channel-messages.v1-durable"), "keep"); |
| 79 | + assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), null); |
| 80 | +}); |
| 81 | + |
| 82 | +test("startup recovery runs only once", () => { |
| 83 | + const ls = makeQuotaLocalStorage({ maxEntries: 10 }); |
| 84 | + install(ls); |
| 85 | + |
| 86 | + recoverLocalStorageQuotaOnStartup(); |
| 87 | + ls.store.set("buzz-channel-messages.v1:relay:new", "new snapshot"); |
| 88 | + recoverLocalStorageQuotaOnStartup(); |
| 89 | + |
| 90 | + assert.equal( |
| 91 | + ls.getItem("buzz-channel-messages.v1:relay:new"), |
| 92 | + "new snapshot", |
| 93 | + ); |
| 94 | +}); |
| 95 | + |
| 96 | +test("startup recovery retries after marker write fails", () => { |
| 97 | + const ls = makeQuotaLocalStorage({ maxEntries: 1 }); |
| 98 | + install(ls); |
| 99 | + ls.store.set("buzz-communities", "keep"); |
| 100 | + |
| 101 | + recoverLocalStorageQuotaOnStartup(); |
| 102 | + assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), null); |
| 103 | + |
| 104 | + ls.store.delete("buzz-communities"); |
| 105 | + ls.store.set("buzz-channel-messages.v1:relay:chan", "big"); |
| 106 | + recoverLocalStorageQuotaOnStartup(); |
| 107 | + |
| 108 | + assert.equal(ls.getItem("buzz-channel-messages.v1:relay:chan"), null); |
| 109 | + assert.equal(ls.getItem("buzz-local-storage-quota-recovery.v1"), "1"); |
| 110 | +}); |
| 111 | + |
| 112 | +test("global cache byte budget evicts only oldest entries needed", () => { |
| 113 | + const ls = makeQuotaLocalStorage({ maxEntries: 20 }); |
| 114 | + install(ls); |
| 115 | + ls.store.set("buzz-communities", "keep"); |
| 116 | + const snapshot = (updatedAt) => |
| 117 | + JSON.stringify({ updatedAt, payload: "x".repeat(400_000) }); |
| 118 | + const oldestKey = "buzz-channel-messages.v1:relay:oldest"; |
| 119 | + const newerKey = "buzz-channels.v1:relay-newer"; |
| 120 | + const newestKey = "buzz-channel-messages.v1:relay:newest"; |
| 121 | + |
| 122 | + assert.equal(setLocalStorageItemWithRecovery(oldestKey, snapshot(1)), true); |
| 123 | + assert.equal(setLocalStorageItemWithRecovery(newerKey, snapshot(2)), true); |
| 124 | + assert.equal(setLocalStorageItemWithRecovery(newestKey, snapshot(3)), true); |
| 125 | + |
| 126 | + assert.equal(ls.getItem(oldestKey), null); |
| 127 | + assert.notEqual(ls.getItem(newerKey), null); |
| 128 | + assert.notEqual(ls.getItem(newestKey), null); |
| 129 | + assert.equal(ls.getItem("buzz-communities"), "keep"); |
| 130 | +}); |
| 131 | + |
| 132 | +test("global cache byte budget spans relays and preserves durable state", () => { |
| 133 | + const ls = makeQuotaLocalStorage({ maxEntries: 20 }); |
| 134 | + install(ls); |
| 135 | + ls.store.set("buzz-communities", "keep"); |
| 136 | + const largeSnapshot = "x".repeat(600_000); |
| 137 | + |
| 138 | + assert.equal( |
| 139 | + setLocalStorageItemWithRecovery( |
| 140 | + "buzz-channel-messages.v1:relay-one:chan", |
| 141 | + largeSnapshot, |
| 142 | + ), |
| 143 | + true, |
| 144 | + ); |
| 145 | + assert.equal( |
| 146 | + setLocalStorageItemWithRecovery( |
| 147 | + "buzz-channel-messages.v1:relay-two:chan", |
| 148 | + largeSnapshot, |
| 149 | + ), |
| 150 | + true, |
| 151 | + ); |
| 152 | + |
| 153 | + assert.equal(ls.getItem("buzz-channel-messages.v1:relay-one:chan"), null); |
| 154 | + assert.equal( |
| 155 | + ls.getItem("buzz-channel-messages.v1:relay-two:chan"), |
| 156 | + largeSnapshot, |
| 157 | + ); |
| 158 | + assert.equal(ls.getItem("buzz-communities"), "keep"); |
| 159 | +}); |
| 160 | + |
| 161 | +test("rejects a single cache entry larger than the global byte budget", () => { |
| 162 | + const ls = makeQuotaLocalStorage({ maxEntries: 10 }); |
| 163 | + install(ls); |
| 164 | + const key = "buzz-channel-messages.v1:relay:oversized"; |
| 165 | + ls.store.set(key, "previous snapshot"); |
| 166 | + |
| 167 | + assert.equal( |
| 168 | + setLocalStorageItemWithRecovery(key, "x".repeat(1_100_000)), |
| 169 | + false, |
| 170 | + ); |
| 171 | + assert.equal(ls.getItem(key), null); |
| 172 | +}); |
| 173 | + |
33 | 174 | test("writes normally when under quota", () => { |
34 | 175 | const ls = makeQuotaLocalStorage({ maxEntries: 10 }); |
35 | 176 | install(ls); |
|
0 commit comments