Something went wrong. Try again.
A live media wall for your Bluesky follows
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372import assert from 'node:assert/strict';import { test } from 'node:test';import { PGlite } from '@electric-sql/pglite';import { createPersistentCache, openCache, THUMBHASH_CACHE_LIMIT, type GraphCache} from './cache.js';import { migrateCacheSchema } from './cache.schema.js';import { WALL_SNAPSHOT_MAX_AGE, type WallSnapshot } from './wall-snapshot.js';
const hash = Uint8Array.of(1, 2, 3, 4, 5);
async function checkValues(cache: GraphCache) { assert.equal(await cache.loadThumbhash('alice', 'missing'), null); const input = new Uint8Array(hash); await cache.saveThumbhash('alice', 'shared', input); input[0] = 99; const loaded = await cache.loadThumbhash('alice', 'shared'); assert.deepEqual(loaded, hash); loaded![0] = 88; assert.deepEqual(await cache.loadThumbhash('alice', 'shared'), hash); assert.equal(await cache.loadThumbhash('bob', 'shared'), null); const batch = await cache.loadThumbhashes('alice', ['shared', 'missing', '__proto__']); assert.deepEqual(batch, { shared: hash }); batch.shared[0] = 90; assert.deepEqual(await cache.loadThumbhashes('alice', ['shared']), { shared: hash }); assert.deepEqual(await cache.loadThumbhashes('bob', ['shared']), {}); assert.deepEqual( await cache.loadThumbhashes('alice', [...Array(300).fill('missing'), 'shared']), {} ); await cache.saveThumbhash('bob', 'shared', new Uint8Array(64)); for (const size of [0, 4, 65]) { await assert.rejects(cache.saveThumbhash('alice', 'shared', new Uint8Array(size)), RangeError); } assert.deepEqual(await cache.loadThumbhash('alice', 'shared'), hash); const snapshot = { follows: ['first', 'second'], blocks: ['blocked'], syncedAt: 123 }; await cache.save('alice', snapshot); assert.deepEqual(await cache.load('alice'), snapshot); assert.equal(await cache.load('bob'), null);}
async function checkEviction(cache: GraphCache) { assert.deepEqual(await cache.loadThumbhashes('alice', ['2']), { '2': hash }); assert.deepEqual(await cache.loadThumbhash('alice', '0'), hash); await cache.saveThumbhash('alice', '1', Uint8Array.of(5, 4, 3, 2, 1)); await cache.saveThumbhash('alice', 'new', hash); assert.equal(await cache.loadThumbhash('alice', '2'), null); assert.deepEqual(await cache.loadThumbhash('alice', '0'), hash); assert.deepEqual(await cache.loadThumbhash('alice', '1'), Uint8Array.of(5, 4, 3, 2, 1)); assert.deepEqual(await cache.loadThumbhash('alice', 'new'), hash); assert.deepEqual(await cache.loadThumbhash('bob', 'shared'), new Uint8Array(64));}
async function checkClosed(cache: GraphCache) { await cache.close(); await cache.close(); await assert.rejects(cache.loadThumbhash('alice', '0'), /closed/); await assert.rejects(cache.saveThumbhash('alice', '0', hash), /closed/); await assert.rejects(cache.load('alice'), /closed/); await assert.rejects(cache.loadWall('alice'), /closed/); await assert.rejects(cache.clearWall('alice'), /closed/); await assert.rejects(cache.saveWall('alice', wall('alice')), /closed/); await assert.rejects(cache.loadThumbhashes('alice', []), /closed/); await assert.rejects(cache.save('alice', { follows: [], blocks: [], syncedAt: 0 }), /closed/);}
void test('memory thumbhash cache copies values, isolates accounts, and bounds LRU entries', async () => { const cache = await openCache(); assert.equal(cache.persistent, false); try { await checkValues(cache); for (let i = 0; i < THUMBHASH_CACHE_LIMIT; i++) { await cache.saveThumbhash('alice', String(i), hash); } assert.equal(await cache.loadThumbhash('alice', 'shared'), null); await checkEviction(cache); const independent = await openCache(); assert.equal(await independent.loadThumbhash('bob', 'shared'), null); await independent.close(); await checkClosed(cache); } finally { await cache.close(); }});
void test('PGlite schema migrates v1 without losing graph and persists bounded thumbhashes', async () => { const db = await PGlite.create(); const cache = createPersistentCache(db); try { await db.exec(` CREATE TABLE graph_cache_schema (id INTEGER PRIMARY KEY CHECK (id = 1), version INTEGER NOT NULL); INSERT INTO graph_cache_schema VALUES (1, 1); CREATE TABLE graph_snapshots (account TEXT PRIMARY KEY, synced_at DOUBLE PRECISION NOT NULL); CREATE TABLE graph_records ( account TEXT NOT NULL REFERENCES graph_snapshots(account) ON DELETE CASCADE, kind TEXT NOT NULL CHECK (kind IN ('follow', 'block')), position INTEGER NOT NULL, subject_did TEXT NOT NULL, PRIMARY KEY (account, kind, position) ); INSERT INTO graph_snapshots VALUES ('existing', 42); INSERT INTO graph_records VALUES ('existing', 'follow', 1, 'friend'); `); await migrateCacheSchema(db); await migrateCacheSchema(db); assert.equal( (await db.query<{ version: number }>('SELECT version FROM graph_cache_schema')).rows[0] .version, 3 ); assert.deepEqual(await cache.load('existing'), { follows: ['friend'], blocks: [], syncedAt: 42 }); await checkValues(cache); await db.query( `INSERT INTO asset_thumbhashes (account, asset_key, hash) SELECT 'alice', n::text, $1 FROM generate_series(0, $2::integer - 1) n`, [hash, THUMBHASH_CACHE_LIMIT] ); await cache.saveThumbhash('alice', String(THUMBHASH_CACHE_LIMIT - 1), hash); assert.equal(await cache.loadThumbhash('alice', 'shared'), null); await checkEviction(cache); assert.equal( ( await db.query<{ count: number }>( "SELECT count(*)::integer AS count FROM asset_thumbhashes WHERE account = 'alice'" ) ).rows[0].count, THUMBHASH_CACHE_LIMIT ); await assert.rejects( db.query('INSERT INTO asset_thumbhashes (account, asset_key, hash) VALUES ($1, $2, $3)', [ 'alice', 'invalid', new Uint8Array(4) ]) ); await checkClosed(cache); } finally { await cache.close(); }});
void test('PGlite schema initializes fresh databases and rejects future versions atomically', async () => { const db = await PGlite.create(); try { await migrateCacheSchema(db); await db.exec('UPDATE graph_cache_schema SET version = 4'); await assert.rejects(migrateCacheSchema(db), /Unsupported/); assert.equal( (await db.query<{ version: number }>('SELECT version FROM graph_cache_schema')).rows[0] .version, 4 ); } finally { await db.close(); }});
function wall(account: string): WallSnapshot { return { posts: [], savedAt: Date.now(), moderation: { userDid: account, prefs: { adultContentEnabled: false, labels: {}, labelers: [], mutedWords: [], hiddenPosts: [] } } };}
async function checkWalls(cache: GraphCache) { const alice = 'did:plc:alice'; const bob = 'did:plc:bob'; assert.equal(await cache.loadWall(alice), null); const snapshot = wall(alice); await cache.saveWall(alice, snapshot); snapshot.moderation.prefs.hiddenPosts.push('mutated'); const loaded = (await cache.loadWall(alice))!; assert.equal(loaded.moderation.prefs.hiddenPosts.length, 0); loaded.moderation.prefs.hiddenPosts.push('also mutated'); assert.deepEqual((await cache.loadWall(alice))!.moderation.prefs.hiddenPosts, []); assert.equal(await cache.loadWall(bob), null); const capped = wall(bob); capped.posts = Array.from({ length: 105 }, () => ({ uri: 'at://did:plc:author/app.bsky.feed.post/example', cid: 'bafyreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku', author: { did: 'did:plc:author', handle: 'author.bsky.social' }, record: {}, indexedAt: '2026-11-07T00:00:00Z' })); await cache.saveWall(bob, capped); assert.equal((await cache.loadWall(bob))!.posts.length, 100); const large = wall(bob); large.posts = capped.posts .slice(0, 10) .map((post) => ({ ...post, record: { text: '界'.repeat(400_000) } })); await cache.saveWall(bob, large); const bounded = (await cache.loadWall(bob))!; assert.equal(bounded.posts.length, 6); assert.deepEqual(bounded.moderation, large.moderation); assert.equal(large.posts.length, 10); await assert.rejects(cache.saveWall(alice, wall(bob)), { name: 'TypeError', message: 'Unable to cache the wall: snapshot belongs to a different account' }); await assert.rejects( cache.saveWall(alice, { ...wall(alice), savedAt: Date.now() - WALL_SNAPSHOT_MAX_AGE - 1 }), { name: 'TypeError', message: 'Unable to cache the wall: invalid structure or snapshot timestamp' } ); const invalidPrefs = wall(alice); invalidPrefs.moderation.prefs.labels = { porn: 'private-invalid-setting' as 'warn' }; await assert.rejects(cache.saveWall(alice, invalidPrefs), { name: 'TypeError', message: 'Unable to cache the wall: invalid account label preferences' }); assert.deepEqual((await cache.loadWall(alice))!.moderation.prefs.hiddenPosts, []); await cache.clearWall(alice); assert.equal(await cache.loadWall(alice), null); assert.ok(await cache.loadWall(bob));}
void test('memory wall cache isolates accounts and copies snapshots', async () => { const cache = await openCache(); try { await checkWalls(cache); const independent = await openCache(); assert.equal(await independent.loadWall('did:plc:bob'), null); await independent.close(); } finally { await cache.close(); }});
void test('PGlite v2 migration preserves graph and hashes and validates wall payloads', async () => { const db = await PGlite.create(); const cache = createPersistentCache(db); try { await migrateCacheSchema(db); await cache.save('existing', { follows: ['friend'], blocks: ['blocked'], syncedAt: 42 }); await cache.saveThumbhash('existing', 'asset', hash); await db.exec('DROP TABLE wall_snapshots; UPDATE graph_cache_schema SET version = 2'); await migrateCacheSchema(db); await migrateCacheSchema(db); assert.equal( (await db.query<{ version: number }>('SELECT version FROM graph_cache_schema')).rows[0] .version, 3 ); assert.deepEqual(await cache.load('existing'), { follows: ['friend'], blocks: ['blocked'], syncedAt: 42 }); assert.deepEqual(await cache.loadThumbhash('existing', 'asset'), hash); await checkWalls(cache); const account = 'did:plc:alice'; for (const payload of [ {}, { ...wall(account), posts: [{}] }, wall('did:plc:bob'), { ...wall(account), savedAt: Date.now() - WALL_SNAPSHOT_MAX_AGE - 1 } ]) { await db.query( 'INSERT INTO wall_snapshots (account, payload) VALUES ($1, $2::jsonb) ON CONFLICT (account) DO UPDATE SET payload = EXCLUDED.payload', [account, JSON.stringify(payload)] ); assert.equal(await cache.loadWall(account), null); } } finally { await cache.close(); }});
void test('persistent cache failure trips once and continues with mirrored memory data', async () => { const { resilientCache } = await import('./cache.js'); const primary = await openCache(); let failed = false; let attempts = 0; let stopped = 0; const broken = new Proxy(primary, { get(target, property) { if (property === 'persistent') return true; if (property === 'saveWall') return async () => { attempts++; throw new DOMException( 'The current transaction exceeded its quota limitations', 'QuotaExceededError' ); }; return Reflect.get(target, property); } }); const cache = resilientCache(broken, () => { failed = true; stopped++; }); const snapshot = { follows: ['did:plc:follow'], blocks: [], syncedAt: 1 }; await cache.save('did:plc:alice', snapshot); await cache.saveThumbhash('did:plc:alice', 'asset', hash); assert.equal(cache.persistent, true); await cache.saveWall('did:plc:alice', wall('did:plc:alice')); assert.equal(failed, true); assert.equal(cache.persistent, false); assert.deepEqual(await cache.load('did:plc:alice'), snapshot); assert.deepEqual(await cache.loadThumbhash('did:plc:alice', 'asset'), hash); assert.ok(await cache.loadWall('did:plc:alice')); await cache.saveWall('did:plc:alice', wall('did:plc:alice')); assert.equal(attempts, 1); assert.equal(stopped, 1); await cache.close(); await assert.rejects(cache.load('did:plc:alice'), /closed/);});
void test('an unresponsive persistent transaction does not strand later cache operations', async () => { const { resilientCache } = await import('./cache.js'); const primary = await openCache(); let attempts = 0; let failures = 0; const cache = resilientCache( { ...primary, persistent: true, saveWall() { attempts++; return new Promise(() => {}); } }, () => failures++, 5 ); const account = 'did:plc:alice'; const value = wall(account); await Promise.all([cache.saveWall(account, value), cache.saveThumbhash(account, 'asset', hash)]); assert.equal(cache.persistent, false); assert.deepEqual(await cache.loadWall(account), value); assert.deepEqual(await cache.loadThumbhash(account, 'asset'), hash); await cache.saveWall(account, value); assert.equal(attempts, 1); assert.equal(failures, 1); await cache.close();});
void test('resilient cache is not a thenable and drains operations before closing', async () => { const { resilientCache } = await import('./cache.js'); const primary = await openCache(); const cache = resilientCache(primary); assert.equal(Reflect.get(cache, 'then'), undefined); assert.equal(await Promise.resolve(cache), cache); const saving = cache.save('alice', { follows: [], blocks: [], syncedAt: 1 }); const reading = cache.load('alice'); const closing = cache.close(); await saving; assert.equal((await reading)?.syncedAt, 1); await closing; await assert.rejects(cache.load('alice'), /closed/);});