// `npm run unit` in web/ (`just check` runs it). import { strict as assert } from 'node:assert'; import { test } from 'node:test'; import type { Typeahead } from './app.ts'; import { findActors, foundIn, partsOf, typeaheadUrl, typed } from './typeahead.ts'; const WAOW: Typeahead = { url: 'https://typeahead.waow.tech', method: 'tech.waow.typeahead.searchActors', param: 'q', params: { limit: 5, x: true }, items: 'actors', did: 'did', handle: 'handle', name: 'displayName', avatar: 'avatar', avatarFallback: 'avatarFallback' }; test('what is typed is asked trimmed, without its @, from two letters', () => { assert.equal(typed(' @chadtm '), 'chadtm'); assert.equal(typed('@c'), null); assert.equal(typed(' '), null); assert.equal(typed('a'.repeat(300))?.length, 256); }); test("the typeahead is asked at its own host, what is typed its param, with its others", () => { assert.equal(typeaheadUrl(WAOW, 'chad tm'), 'https://typeahead.waow.tech/xrpc/tech.waow.typeahead.searchActors?limit=5&x=true&q=chad+tm'); assert.equal(typeaheadUrl({ ...WAOW, params: undefined, param: 'term' }, 'a'), 'https://typeahead.waow.tech/xrpc/tech.waow.typeahead.searchActors?term=a'); }); test('its answer is read by its paths: an account with a DID and a handle, its pictures https alone', () => { const answer = { actors: [ { did: 'did:plc:a', handle: 'chadtmiller.com', displayName: 'Chad', avatar: 'https://cdn.example/a', avatarFallback: 'https://pds.example/a' }, { did: 'did:plc:b', handle: 'b.test', displayName: '', avatar: 'http://cdn.example/b' }, { did: 'not a did', handle: 'c.test' }, { did: 'did:plc:d', handle: 'handle.invalid' }, { did: 'did:plc:e' }, 'nothing' ] }; assert.deepEqual(foundIn(WAOW, answer), [ { did: 'did:plc:a', handle: 'chadtmiller.com', name: 'Chad', avatar: 'https://cdn.example/a', fallback: 'https://pds.example/a' }, { did: 'did:plc:b', handle: 'b.test', name: null, avatar: null, fallback: null } ]); const nested: Typeahead = { ...WAOW, items: 'data.found', did: 'who.did', handle: 'who.handle', name: undefined, avatar: undefined, avatarFallback: undefined }; assert.deepEqual(foundIn(nested, { data: { found: [{ who: { did: 'did:plc:a', handle: 'a.test' } }] } }), [{ did: 'did:plc:a', handle: 'a.test', name: null, avatar: null, fallback: null }]); assert.deepEqual(foundIn(WAOW, { actors: 'none' }), []); assert.deepEqual(foundIn(WAOW, null), []); }); test('finding asks nothing for under two letters, and finds none where the typeahead cannot say', async () => { const was = globalThis.fetch; const asked: string[] = []; let answer: () => Promise = async () => Response.json({ actors: [{ did: 'did:plc:a', handle: 'a.test' }] }); globalThis.fetch = ((url: string) => { asked.push(url); return answer(); }) as typeof fetch; try { assert.deepEqual(await findActors(WAOW, 'a'), []); assert.deepEqual(asked, []); assert.deepEqual(await findActors(WAOW, 'ab'), [{ did: 'did:plc:a', handle: 'a.test', name: null, avatar: null, fallback: null }]); answer = async () => new Response('no', { status: 500 }); assert.deepEqual(await findActors(WAOW, 'ab'), []); answer = async () => { throw new TypeError('offline'); }; assert.deepEqual(await findActors(WAOW, 'ab'), []); assert.equal(asked.length, 3); } finally { globalThis.fetch = was; } }); test("a line's Markdown links are its links, the rest its words", () => { assert.deepEqual(partsOf('By signing in you agree to our [Terms](/support/terms), and [Privacy](https://x.example/p).'), [ { text: 'By signing in you agree to our ' }, { text: 'Terms', href: '/support/terms' }, { text: ', and ' }, { text: 'Privacy', href: 'https://x.example/p' }, { text: '.' } ]); assert.deepEqual(partsOf('[What is an atmosphere account?](https://atmosphereaccount.com/)'), [{ text: 'What is an atmosphere account?', href: 'https://atmosphereaccount.com/' }]); assert.deepEqual(partsOf('No links [here].'), [{ text: 'No links [here].' }]); });