Something went wrong. Try again.
Add Bluesky replies, quotes, and reposts to any web page. Handy for adding a comments section anywhere.
Something went wrong. Try again.
5.8 kB · 164 lines
JavaScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165import { test } from 'node:test'import assert from 'node:assert/strict'
import { toAtUri, rkey, findQuotedUri, isNotFound, describeErr } from './hide-reply.mjs'
// ---------------------------------------------------------------------------// toAtUri — bsky.app URL → AT URI//// This is where the "reply not actually hidden" bug lived: toAtUri preserves// whatever identifier the URL carries. A handle URL yields a handle-based// AT URI, which the AppView never matches against threadgate.hiddenReplies// (it only uses the DID form). These tests pin that behavior down so callers// know they must canonicalize to a DID before writing it into a record.// ---------------------------------------------------------------------------
test('toAtUri converts a DID-based post URL', () => { assert.equal( toAtUri('https://bsky.app/profile/did:plc:abc/post/3xyz'), 'at://did:plc:abc/app.bsky.feed.post/3xyz' )})
test('toAtUri preserves a handle authority (the URI is NOT canonicalized to a DID)', () => { assert.equal( toAtUri('https://bsky.app/profile/mackuba.eu/post/3mmul5isxfk2i'), 'at://mackuba.eu/app.bsky.feed.post/3mmul5isxfk2i' )})
test('toAtUri strips a query string from the rkey', () => { assert.equal( toAtUri('https://bsky.app/profile/did:plc:abc/post/3xyz?ref=foo'), 'at://did:plc:abc/app.bsky.feed.post/3xyz' )})
test('toAtUri strips a hash fragment from the rkey', () => { assert.equal( toAtUri('https://bsky.app/profile/did:plc:abc/post/3xyz#anchor'), 'at://did:plc:abc/app.bsky.feed.post/3xyz' )})
test('toAtUri returns null for a non-post URL', () => { assert.equal(toAtUri('https://example.com/whatever'), null)})
test('toAtUri returns null for a profile URL with no post', () => { assert.equal(toAtUri('https://bsky.app/profile/did:plc:abc'), null)})
// ---------------------------------------------------------------------------// rkey — last path segment of an AT URI// ---------------------------------------------------------------------------
test('rkey extracts the record key from a post URI', () => { assert.equal(rkey('at://did:plc:abc/app.bsky.feed.post/3xyz'), '3xyz')})
test('rkey extracts the record key from a threadgate URI', () => { assert.equal(rkey('at://did:plc:abc/app.bsky.feed.threadgate/3root'), '3root')})
// ---------------------------------------------------------------------------// findQuotedUri — does this post quote one of OUR posts?// ---------------------------------------------------------------------------
const OWNER = 'did:plc:owner'
test('findQuotedUri returns the uri for a plain quote of our post', () => { const post = { record: { embed: { $type: 'app.bsky.embed.record', record: { uri: `at://${OWNER}/app.bsky.feed.post/abc` }, }, }, } assert.equal(findQuotedUri(post, OWNER), `at://${OWNER}/app.bsky.feed.post/abc`)})
test('findQuotedUri returns the nested uri for a quote-with-media of our post', () => { const post = { record: { embed: { $type: 'app.bsky.embed.recordWithMedia', record: { record: { uri: `at://${OWNER}/app.bsky.feed.post/xyz` } }, }, }, } assert.equal(findQuotedUri(post, OWNER), `at://${OWNER}/app.bsky.feed.post/xyz`)})
test('findQuotedUri returns null when the quoted post belongs to someone else', () => { const post = { record: { embed: { $type: 'app.bsky.embed.record', record: { uri: 'at://did:plc:someone-else/app.bsky.feed.post/abc' }, }, }, } assert.equal(findQuotedUri(post, OWNER), null)})
test('findQuotedUri returns null when there is no embed', () => { assert.equal(findQuotedUri({ record: {} }, OWNER), null)})
test('findQuotedUri returns null when the post has no record', () => { assert.equal(findQuotedUri({}, OWNER), null)})
// ---------------------------------------------------------------------------// isNotFound — is this getRecord error positively "record does not exist"?//// The hardened catch blocks rely on this: only a positive not-found may fall// through to creating a fresh record. Anything ambiguous (network, auth, 5xx)// must NOT be treated as not-found, or we'd clobber existing data.// ---------------------------------------------------------------------------
test('isNotFound is true for an XRPC RecordNotFound error', () => { assert.equal(isNotFound({ error: 'RecordNotFound' }), true)})
test('isNotFound is true for HTTP 400 (PDS RecordNotFound status)', () => { assert.equal(isNotFound({ status: 400 }), true)})
test('isNotFound is true for HTTP 404', () => { assert.equal(isNotFound({ status: 404 }), true)})
test('isNotFound reads a nested response.status', () => { assert.equal(isNotFound({ response: { status: 404 } }), true)})
test('isNotFound is false for a network error with no status', () => { assert.equal(isNotFound(new Error('fetch failed')), false)})
test('isNotFound is false for a server error (500)', () => { assert.equal(isNotFound({ status: 500 }), false)})
// ---------------------------------------------------------------------------// describeErr — best-effort human-readable error for logging// ---------------------------------------------------------------------------
test('describeErr prefers the XRPC error code', () => { assert.equal(describeErr({ error: 'RecordNotFound', status: 400 }), 'RecordNotFound')})
test('describeErr falls back to the status code', () => { assert.equal(describeErr({ status: 500 }), 500)})
test('describeErr falls back to the error message', () => { assert.equal(describeErr(new Error('boom')), 'boom')})
test('describeErr returns a sentinel when nothing is available', () => { assert.equal(describeErr({}), 'unknown error')})