Something went wrong. Try again.
Browser extension: detect and subscribe to standard.site publications on ATProto
Something went wrong. Try again.
4.4 kB · 108 lines
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109// What the capture calls a finished popup.//// These ran as three waits in a row and shot the store listing image mid-load// anyway, so the rule is worth stating twice: once where the capture asks it,// and once here, against the states that used to slip through it.
import { describe, expect, it } from 'vitest'import { imagesLanded, popupExpression, popupSettled, popupStalled,} from './capture-status-docs.mjs'
/** An `<img>` as these predicates read one: a src it was given, and whether it landed. */const img = (src, complete) => ({ getAttribute: () => src, complete })
/** * Enough of a document for the predicates: whether the popup has its `#pub` * card, whether anything in it is `aria-busy` (the card while a lookup is out, * the status region while the worker has not answered), whether a `.loading` * placeholder is standing, and the images the popup has so far. */function fakeDocument({ card = true, busy = false, loading = false, images = [] } = {}) { return { getElementById: (id) => (id === 'pub' && card ? { hasAttribute: () => busy } : null), querySelector: (sel) => (sel === '[aria-busy]' && busy) || (sel === '.loading' && loading) ? {} : null, images, }}
describe('imagesLanded', () => { it('is true for a popup with no images at all', () => { expect(imagesLanded(fakeDocument())).toBe(true) })
it('waits for an image that is still being fetched', () => { expect(imagesLanded(fakeDocument({ images: [img('https://pds/blob', false)] }))).toBe(false) })
it('accepts an image that failed, which is a state the popup draws', () => { // `complete` is true either way; the popup answers a failed icon with its // monogram fallback, and a capture of that must not hang. expect(imagesLanded(fakeDocument({ images: [img('https://pds/gone', true)] }))).toBe(true) })
it('ignores an img nothing has been asked of yet', () => { // The whole bug: `#owner-avatar` has no src until the account lookup // lands, and an empty img reports itself complete. expect(imagesLanded(fakeDocument({ images: [img(null, true)] }))).toBe(true) })})
describe('popupSettled', () => { it('accepts a card with no lookup out, no placeholder and every image landed', () => { expect( popupSettled(fakeDocument({ images: [img('https://pds/icon', true)] })), ).toBe(true) })
it('refuses a card that still says it is busy', () => { expect(popupSettled(fakeDocument({ busy: true }))).toBe(false) })
it('refuses a placeholder still standing', () => { expect(popupSettled(fakeDocument({ loading: true }))).toBe(false) })
it('refuses an avatar the landed lookup has only just asked for', () => { // The state that shipped: the account card is drawn, so nothing is busy // and no placeholder is up, but its avatar is still coming off the PDS. expect(popupSettled(fakeDocument({ images: [img('https://pds/avatar', false)] }))).toBe(false) })
it('refuses a popup that has no card element, so a wrong build cannot pass', () => { expect(popupSettled(fakeDocument({ card: false }))).toBe(false) })})
describe('popupExpression', () => { /** Run the expression the way the popup does: as text, over a document. */ const evaluateInPage = (predicate, doc) => new Function('document', `return ${popupExpression(predicate)}`)(doc)
// The predicates go over the wire as text, so a helper one of them calls has // to travel with it. Left out, this is a ReferenceError inside the popup that // nothing but a real capture run would find. it('carries the helper the predicate calls into the page', () => { const stillFetching = fakeDocument({ images: [img('https://pds/avatar', false)] }) expect(evaluateInPage(popupSettled, stillFetching)).toBe(false) expect(evaluateInPage(popupSettled, fakeDocument())).toBe(true) expect(evaluateInPage(popupStalled, fakeDocument({ busy: true, loading: true }))).toBe( true, ) })})
describe('popupStalled', () => { it('accepts a busy card with a placeholder standing', () => { expect(popupStalled(fakeDocument({ busy: true, loading: true }))).toBe(true) })
it('refuses a popup that finished loading, which is not what it is a capture of', () => { expect(popupStalled(fakeDocument({ loading: true }))).toBe(false) expect(popupStalled(fakeDocument({ busy: true }))).toBe(false) })})