From da7a97a835d40771831d7b2bdf96f2832a28faaa Mon Sep 17 00:00:00 2001 From: Void Date: Tue, 10 Nov 2026 14:45:26 +0000 Subject: [PATCH] Preview demo: seeded file-zone room with two albums The pretend server answers /threads, m.thread relations and media downloads (generated WAV and PNG bytes, no binaries in the repo). Co-Authored-By: Claude Sonnet 5.5 --- src/preview-demo/fixtures.ts | 44 +++++++++++++++ src/preview-demo/install.ts | 6 +++ src/preview-demo/media.ts | 95 +++++++++++++++++++++++++++++++++ src/preview-demo/server.test.ts | 71 ++++++++++++++++++++++-- src/preview-demo/server.ts | 64 +++++++++++++++++++++- 5 files changed, 275 insertions(+), 5 deletions(-) create mode 100644 src/preview-demo/media.ts diff --git a/src/preview-demo/fixtures.ts b/src/preview-demo/fixtures.ts index c96a4ccf..18b2fac1 100644 --- a/src/preview-demo/fixtures.ts +++ b/src/preview-demo/fixtures.ts @@ -44,6 +44,8 @@ export type DemoRoom = { topic?: string; members: string[]; notice?: 'show' | 'study' | 'train'; + /** The m.room.create `type`. */ + type?: string; parent?: string; steward?: { mind: string; repo: string }; joined: boolean; @@ -54,6 +56,7 @@ export const ROOT = `!root:${DEMO_SERVER}`; export const PROJECTS = `!projects:${DEMO_SERVER}`; export const GENERAL = `!general:${DEMO_SERVER}`; export const COMMONS = `!commons:${DEMO_SERVER}`; +export const FILES = `!files:${DEMO_SERVER}`; const everyone = USERS.map((u) => u.id); export const MIND_IDS = [user('eureka'), user('void'), user('nibu')]; @@ -115,6 +118,17 @@ export const ROOMS: DemoRoom[] = [ { from: user('heimdall'), body: 'Ok.', pose: 'thinking', minutesAgo: 20 }, ], }, + { + id: FILES, + alias: `#files:${DEMO_SERVER}`, + name: 'Files', + topic: 'The file zone: one thread per Mind, shown as albums.', + type: 'org.gamecult.file_zone', + members: [ME, ...minds], + parent: ROOT, + joined: true, + lines: [], + }, { id: PROJECTS, alias: `#projects:${DEMO_SERVER}`, @@ -302,6 +316,36 @@ export const ROOMS: DemoRoom[] = [ }, ]; +// The file zone's albums: a thread root per album, one thread reply per file. Media is generated +// in media.ts: audio by tone frequency, images by gradient color. +export type DemoAlbum = { + mind: string; + title: string; + minutesAgo: number; + files: { kind: 'audio' | 'image'; name: string; seed: number | [number, number, number] }[]; +}; +export const ALBUMS: DemoAlbum[] = [ + { + mind: user('void'), + title: 'void', + minutesAgo: 90, + files: [ + { kind: 'audio', name: 'low-tone.wav', seed: 220 }, + { kind: 'audio', name: 'mid-tone.wav', seed: 330 }, + { kind: 'audio', name: 'high-tone.wav', seed: 440 }, + ], + }, + { + mind: user('nibu'), + title: 'nibu', + minutesAgo: 30, + files: [ + { kind: 'image', name: 'dusk.png', seed: [200, 90, 60] }, + { kind: 'image', name: 'lagoon.png', seed: [40, 150, 190] }, + ], + }, +]; + // A Mind's canned answer to whatever the visitor types: label and line. export const REPLIES: { pose: string; body: string }[] = [ { pose: 'thinking', body: 'Hm. Let me think about that.' }, diff --git a/src/preview-demo/install.ts b/src/preview-demo/install.ts index a7b66de9..f3d3b1bb 100644 --- a/src/preview-demo/install.ts +++ b/src/preview-demo/install.ts @@ -72,6 +72,12 @@ export const guardedFetch = body: parseBody(raw), signal: init?.signal ?? req?.signal ?? undefined, }); + if (out.raw) { + return new Response(out.raw.data, { + status: out.status, + headers: { 'Content-Type': out.raw.type }, + }); + } return new Response(JSON.stringify(out.body), { status: out.status, headers: { 'Content-Type': 'application/json' }, diff --git a/src/preview-demo/media.ts b/src/preview-demo/media.ts new file mode 100644 index 00000000..cfde5669 --- /dev/null +++ b/src/preview-demo/media.ts @@ -0,0 +1,95 @@ +/* eslint-disable no-bitwise */ +// Tiny media for the pretend server's file zone, generated here so the repo holds no binaries. +export type DemoMedia = { type: string; data: Uint8Array }; + +const u32 = (n: number): number[] => [(n >>> 24) & 255, (n >>> 16) & 255, (n >>> 8) & 255, n & 255]; +const le = (n: number, bytes: number): number[] => + Array.from({ length: bytes }, (_, i) => (n >>> (8 * i)) & 255); + +/** A 0.6 s mono 8 kHz 8-bit WAV: a sine at `hz` with a soft fade. */ +export const wavTone = (hz: number): DemoMedia => { + const rate = 8000; + const n = Math.round(rate * 0.6); + const pcm = Array.from({ length: n }, (_, i) => { + const fade = Math.min(1, i / 400, (n - i) / 400); + return Math.round(128 + 90 * fade * Math.sin((2 * Math.PI * hz * i) / rate)); + }); + const tag = (s: string) => Array.from(s, (c) => c.charCodeAt(0)); + return { + type: 'audio/wav', + data: Uint8Array.from([ + ...tag('RIFF'), + ...le(36 + n, 4), + ...tag('WAVEfmt '), + ...le(16, 4), + ...le(1, 2), + ...le(1, 2), + ...le(rate, 4), + ...le(rate, 4), + ...le(1, 2), + ...le(8, 2), + ...tag('data'), + ...le(n, 4), + ...pcm, + ]), + }; +}; + +const crc32 = (bytes: number[]): number => { + let c = ~0; + bytes.forEach((b) => { + c ^= b; + for (let k = 0; k < 8; k += 1) c = (c >>> 1) ^ (0xedb88320 & -(c & 1)); + }); + return ~c >>> 0; +}; + +const chunk = (name: string, data: number[]): number[] => { + const body = [...Array.from(name, (c) => c.charCodeAt(0)), ...data]; + return [...u32(data.length), ...body, ...u32(crc32(body))]; +}; + +/** A 32 x 32 RGB PNG: a diagonal gradient in `rgb`, zlib "stored" blocks, no compression. */ +export const pngGradient = (rgb: [number, number, number]): DemoMedia => { + const size = 32; + const raw: number[] = []; + for (let y = 0; y < size; y += 1) { + raw.push(0); + for (let x = 0; x < size; x += 1) { + const f = 0.35 + (0.65 * (x + y)) / (2 * size); + raw.push(...rgb.map((v) => Math.round(v * f))); + } + } + let a = 1; + let b = 0; + raw.forEach((v) => { + a = (a + v) % 65521; + b = (b + a) % 65521; + }); + // One stored block holds up to 65535 bytes; 32 * 97 = 3104 fits. + const zlib = [ + 0x78, + 0x01, + 1, + ...le(raw.length, 2), + ...le(~raw.length & 0xffff, 2), + ...raw, + ...u32(((b << 16) | a) >>> 0), + ]; + return { + type: 'image/png', + data: Uint8Array.from([ + 137, + 80, + 78, + 71, + 13, + 10, + 26, + 10, + ...chunk('IHDR', [...u32(size), ...u32(size), 8, 2, 0, 0, 0]), + ...chunk('IDAT', zlib), + ...chunk('IEND', []), + ]), + }; +}; diff --git a/src/preview-demo/server.test.ts b/src/preview-demo/server.test.ts index d277b97b..1462c2c5 100644 --- a/src/preview-demo/server.test.ts +++ b/src/preview-demo/server.test.ts @@ -1,7 +1,14 @@ /* eslint-disable max-classes-per-file, no-promise-executor-return, no-useless-constructor, no-empty-function, no-void, @typescript-eslint/no-non-null-assertion */ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { ClientEvent, MatrixClient, SyncState, createClient } from 'matrix-js-sdk'; -import { DEMO_ORIGIN, ME, ROOMS, GENERAL, COMMONS } from './fixtures'; +import { + ClientEvent, + ClientPrefix, + MatrixClient, + Method, + SyncState, + createClient, +} from 'matrix-js-sdk'; +import { ALBUMS, DEMO_ORIGIN, ME, ROOMS, GENERAL, COMMONS, FILES } from './fixtures'; import { guardedFetch, installDemo, routeOf } from './install'; import { createDemoServer, DemoServer } from './server'; import { seedDemoSettings } from './seed'; @@ -29,6 +36,9 @@ const waitFor = (read: () => T | undefined, ms = 5000): Promise => tick(); }); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +type Ev = { event_id: string; content: Record }; + const live: { server: DemoServer; mx: MatrixClient }[] = []; afterEach(() => { live.splice(0).forEach(({ server, mx }) => { @@ -115,7 +125,12 @@ describe('the pretend homeserver through matrix-js-sdk', () => { const ev = mx.getRoom(r.id)!.currentState.getStateEvents('org.gamecult.notice', ''); return [r.name, roomTrainingLine(parseRoomNotice(ev?.getContent())) !== undefined]; }); - expect(Object.fromEntries(lines)).toEqual({ General: true, Ratatoskr: false, Huginn: false }); + expect(Object.fromEntries(lines)).toEqual({ + General: true, + Files: false, + Ratatoskr: false, + Huginn: false, + }); }); it('sends a message, echoes it once with its transaction id, and a Mind answers with a pose', async () => { @@ -171,6 +186,56 @@ describe('the pretend homeserver through matrix-js-sdk', () => { expect(stewards.filter(Boolean).length).toBe(2); }); + it('syncs the file zone as a joined room whose create type marks it', async () => { + const { mx } = await syncedClient(); + const create = mx.getRoom(FILES)!.currentState.getStateEvents('m.room.create', '')!; + expect(create.getContent().type).toBe('org.gamecult.file_zone'); + expect( + mx.getRoom(GENERAL)!.currentState.getStateEvents('m.room.create', '')!.getContent().type + ).toBeUndefined(); + }); + + it('serves the zone albums through the thread endpoints and their media as bytes', async () => { + const { mx } = await syncedClient(); + const get = (path: string, params?: Record) => + mx.http.authedRequest<{ chunk: Ev[] }>(Method.Get, path, params, undefined, { + prefix: ClientPrefix.V1, + }); + const roots = (await get(`/rooms/${encodeURIComponent(FILES)}/threads`)).chunk; + expect(roots.map((r) => r.content.body).sort()).toEqual(ALBUMS.map((a) => a.title).sort()); + const counts: Record = {}; + await Promise.all( + roots.map(async (root: Ev) => { + const rel = `/rooms/${encodeURIComponent(FILES)}/relations/${encodeURIComponent( + root.event_id + )}/m.thread`; + const oldest = (await get(rel, { dir: 'f' })).chunk; + const newest = (await get(rel)).chunk; + expect(newest.map((e: Ev) => e.event_id)).toEqual( + oldest.map((e: Ev) => e.event_id).reverse() + ); + counts[root.content.body] = oldest.map((e: Ev) => e.content.msgtype); + expect(oldest.every((e: Ev) => e.content['m.relates_to'].event_id === root.event_id)).toBe( + true + ); + }) + ); + expect(counts).toEqual({ + void: ['m.audio', 'm.audio', 'm.audio'], + nibu: ['m.image', 'm.image'], + }); + const f = guardedFetch(createDemoServer(), PAGE, never as never); + const wav = await f(`${DEMO_ORIGIN}/_matrix/client/v1/media/download/demo.invalid/album0file0`); + expect(wav.headers.get('content-type')).toBe('audio/wav'); + expect(new TextDecoder().decode((await wav.arrayBuffer()).slice(0, 4))).toBe('RIFF'); + const png = await f(`${DEMO_ORIGIN}/_matrix/media/v3/download/demo.invalid/album1file1`); + expect(png.headers.get('content-type')).toBe('image/png'); + expect((await png.arrayBuffer()).byteLength).toBeGreaterThan(100); + expect( + (await f(`${DEMO_ORIGIN}/_matrix/client/v1/media/download/demo.invalid/nope`)).status + ).toBe(404); + }); + it('answers a long poll with nothing new after its timeout', async () => { const server = createDemoServer(); live.push({ server, mx: clientFor(server) }); diff --git a/src/preview-demo/server.ts b/src/preview-demo/server.ts index 33a51b16..ed8cbf46 100644 --- a/src/preview-demo/server.ts +++ b/src/preview-demo/server.ts @@ -3,6 +3,7 @@ // send messages and get a Mind's canned answer. It touches no network and no storage; the // fetch guard that routes requests here lives in install.ts. import { + ALBUMS, COMMONS, DEMO_DEVICE, DEMO_ORIGIN, @@ -10,14 +11,17 @@ import { DEMO_TOKEN, DemoEvent, DemoRoom, + FILES, ME, MIND_IDS, REPLIES, ROOMS, USERS, } from './fixtures'; +import { DemoMedia, pngGradient, wavTone } from './media'; -export type DemoResponse = { status: number; body: unknown }; +/** `raw` answers a media download with bytes instead of JSON. */ +export type DemoResponse = { status: number; body: unknown; raw?: DemoMedia }; export type DemoRequest = { method: string; url: string; body?: unknown; signal?: AbortSignal }; const POSE_KEY = 'org.gamecult.pose'; @@ -79,7 +83,12 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => { mk( 'm.room.create', creator, - room.space ? { type: 'm.space', room_version: '10' } : { room_version: '10' }, + { + room_version: '10', + ...((room.space ? 'm.space' : room.type) + ? { type: room.space ? 'm.space' : room.type } + : {}), + }, t0, '' ), @@ -159,6 +168,45 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => { [`@eureka:${DEMO_SERVER}`]: ROOMS.filter((r) => r.direct).map((r) => r.id), }); + // The file zone's albums live outside the sync timeline: thread roots per room, replies per + // root, media by mxc id. The thread endpoints and media downloads answer from these. + const threadRoots = new Map(); + const threadReplies = new Map(); + const media = new Map(); + ALBUMS.forEach((album, a) => { + const t0 = now() - album.minutesAgo * 60 * 1000; + const root = mk('m.room.message', album.mind, { msgtype: 'm.text', body: album.title }, t0); + threadRoots.set(FILES, [...(threadRoots.get(FILES) ?? []), root]); + threadReplies.set( + root.event_id, + album.files.map((f, i) => { + const id = `album${a}file${i}`; + const m = + f.kind === 'audio' + ? wavTone(f.seed as number) + : pngGradient(f.seed as [number, number, number]); + media.set(id, m); + return mk( + 'm.room.message', + album.mind, + { + msgtype: f.kind === 'audio' ? 'm.audio' : 'm.image', + body: f.name, + url: `mxc://${DEMO_SERVER}/${id}`, + info: { mimetype: m.type, size: m.data.length }, + 'm.relates_to': { + rel_type: 'm.thread', + event_id: root.event_id, + is_falling_back: true, + 'm.in_reply_to': { event_id: root.event_id }, + }, + }, + t0 + (i + 1) * 60 * 1000 + ); + }) + ); + }); + const notify = () => { waiters.forEach((w) => w()); waiters.clear(); @@ -290,6 +338,11 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => { if (p === '/.well-known/matrix/client') return ok({ 'm.homeserver': { base_url: DEMO_ORIGIN } }); if (!p.startsWith('/_matrix/')) return err(404, 'M_NOT_FOUND', 'Not found'); + g = p.match(/^\/_matrix\/(?:client\/v1\/media|media\/v3)\/download\/[^/]+\/([^/]+)$/); + if (g) { + const found = media.get(g[1]); + return found ? { status: 200, body: null, raw: found } : err(404, 'M_NOT_FOUND', 'No media'); + } const c = p.replace(/^\/_matrix\/client\/(v\d+|r0|unstable)/, ''); if (p.endsWith('/versions')) return ok({ versions: ['v1.1', 'v1.5', 'v1.11'], unstable_features: {} }); @@ -394,6 +447,13 @@ export const createDemoServer = (now: () => number = Date.now): DemoServer => { if (g) { return ok({ chunk: (stateOf.get(g[1]) ?? []).filter((e) => e.type === 'm.room.member') }); } + g = c.match(/^\/rooms\/([^/]+)\/threads$/); + if (g) return ok({ chunk: [...(threadRoots.get(g[1]) ?? [])].reverse() }); + g = c.match(/^\/rooms\/([^/]+)\/relations\/([^/]+)\/m\.thread$/); + if (g) { + const oldestFirst = [...(threadReplies.get(g[2]) ?? [])]; + return ok({ chunk: u.searchParams.get('dir') === 'f' ? oldestFirst : oldestFirst.reverse() }); + } g = c.match(/^\/rooms\/([^/]+)\/messages$/); if (g) return ok({ chunk: [], start: 'p0', end: 'p0' }); g = c.match(/^\/rooms\/([^/]+)\/(?:summary|context\/.+)$/);