import assert from 'node:assert/strict'; import { test } from 'node:test'; import type { InteractionUpdate } from './feed.js'; import { createInteractionController } from './interaction-controller.js'; import type { createInteractions } from './interactions.js'; const actor = 'did:plc:author'; const post = { uri: `at://${actor}/app.bsky.feed.post/example`, cid: 'bafyreihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku' }; const likeUri = 'at://did:plc:viewer/app.bsky.feed.like/like'; const repostUri = 'at://did:plc:viewer/app.bsky.feed.repost/repost'; const blockUri = 'at://did:plc:viewer/app.bsky.graph.block/block'; const followUri = 'at://did:plc:viewer/app.bsky.graph.follow/follow'; type Controller = ReturnType; const actions: { method: string; run: (controller: Controller) => Promise; args: unknown[]; update: InteractionUpdate; result?: string; }[] = [ { method: 'like', run: (c) => c.like(post), args: [post], update: { kind: 'post', ...post, changes: { likeUri } }, result: likeUri }, { method: 'unlike', run: (c) => c.unlike(post, likeUri), args: [post.uri, likeUri], update: { kind: 'post', ...post, changes: { likeUri: null } } }, { method: 'repost', run: (c) => c.repost(post), args: [post], update: { kind: 'post', ...post, changes: { repostUri } }, result: repostUri }, { method: 'unrepost', run: (c) => c.unrepost(post, repostUri), args: [post.uri, repostUri], update: { kind: 'post', ...post, changes: { repostUri: null } } }, { method: 'bookmark', run: (c) => c.bookmark(post), args: [post], update: { kind: 'post', ...post, changes: { bookmarked: true } } }, { method: 'unbookmark', run: (c) => c.unbookmark(post), args: [post.uri], update: { kind: 'post', ...post, changes: { bookmarked: false } } }, { method: 'follow', run: (c) => c.follow(actor), args: [actor], update: { kind: 'actor', did: actor, following: followUri }, result: followUri }, { method: 'unfollow', run: (c) => c.unfollow(actor, followUri), args: [actor, followUri], update: { kind: 'actor', did: actor, following: null } }, { method: 'mute', run: (c) => c.mute(actor), args: [actor], update: { kind: 'actor', did: actor, muted: true } }, { method: 'unmute', run: (c) => c.unmute(actor), args: [actor], update: { kind: 'actor', did: actor, muted: false } }, { method: 'block', run: (c) => c.block(actor), args: [actor], update: { kind: 'actor', did: actor, blocking: blockUri }, result: blockUri }, { method: 'unblock', run: (c) => c.unblock(actor, blockUri), args: [actor, blockUri], update: { kind: 'actor', did: actor, blocking: null } } ]; function fixture() { const calls: { method: string; args: unknown[] }[] = []; const updates: InteractionUpdate[] = []; let error: Error | undefined; let gate: Promise | undefined; let closes = 0; const record = async (method: string, args: unknown[]) => { calls.push({ method, args }); await gate; if (error) throw error; }; const service = { async like(...args) { await record('like', args); return likeUri; }, async unlike(...args) { await record('unlike', args); }, async repost(...args) { await record('repost', args); return repostUri; }, async unrepost(...args) { await record('unrepost', args); }, async bookmark(...args) { await record('bookmark', args); }, async unbookmark(...args) { await record('unbookmark', args); }, async follow(...args) { await record('follow', args); return followUri; }, async unfollow(...args) { await record('unfollow', args); }, async mute(...args) { await record('mute', args); }, async unmute(...args) { await record('unmute', args); }, async block(...args) { await record('block', args); return blockUri; }, async unblock(...args) { await record('unblock', args); }, close() { closes++; } } satisfies ReturnType; return { controller: createInteractionController(service, (update) => { updates.push(update); }), calls, updates, get closes() { return closes; }, fail(value: Error) { error = value; }, pause() { let release!: () => void; gate = new Promise((resolve) => { release = resolve; }); return release; } }; } for (const action of actions) { void test(`${action.method} reconciles exactly once only after confirmed success`, async () => { const f = fixture(); const release = f.pause(); const pending = action.run(f.controller); assert.deepEqual(f.calls, [{ method: action.method, args: action.args }]); assert.deepEqual(f.updates, []); release(); assert.equal(await pending, action.result); assert.deepEqual(f.updates, [action.update]); }); void test(`${action.method} propagates failure without reconciliation or retries`, async () => { const f = fixture(); const error = new Error('write failed'); f.fail(error); await assert.rejects(action.run(f.controller), (actual) => actual === error); assert.deepEqual(f.calls, [{ method: action.method, args: action.args }]); assert.deepEqual(f.updates, []); }); void test(`${action.method} may finish after close but cannot reconcile`, async () => { const f = fixture(); const release = f.pause(); const pending = action.run(f.controller); f.controller.close(); assert.equal(f.closes, 1); release(); assert.equal(await pending, action.result); assert.deepEqual(f.updates, []); }); void test(`${action.method} rejects new writes after close without calling the service`, async () => { const f = fixture(); f.controller.close(); f.controller.close(); await assert.rejects(action.run(f.controller), /closed/); assert.equal(f.closes, 1); assert.deepEqual(f.calls, []); assert.deepEqual(f.updates, []); }); }