// The trees web/renderer/test/ssr.mjs draws as a server would and hydrates // as a browser would: Grain's screens, each an answer given its `_meta.ui` // by `target/debug/hud resolve` with Grain's views (harness/apps.mjs, as // HUD_APPS) and written to web/renderer/test/trees/.json. The feed, a // gallery, a profile (Grain's page of an account) and notifications are the // gallery's recorded answers (harness/gallery/answers.json); Explore, its // map, an account's stories and their archive are made here of those // answers' people and photos, each held to its method's lexicon. Every // tree is held to HUD's tree lexicon (place.hud.ui). `just ssr-data`. import { execFileSync } from 'node:child_process'; import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import { apps } from '../apps.mjs'; import { answersFile, lexiconsOf, root, schemas, shown, viewOf } from '../gallery/common.mjs'; const say = (line) => console.log(`==> ${line}`); const out = join(root, 'web/renderer/test/trees'); const hud = join(root, 'target/debug/hud'); if (!existsSync(hud)) { say('no target/debug/hud: building it'); execFileSync('rustup', ['run', 'stable', 'cargo', 'build', '-p', 'hud'], { cwd: root, stdio: 'inherit' }); } const book = JSON.parse(readFileSync(answersFile, 'utf8')); const recorded = (id) => book.answers[id] ?? (say(`no ${id} in harness/gallery/answers.json: \`node harness/gallery/record.mjs\``), process.exit(2)); const feed = recorded('grain-feed'); const gallery = recorded('grain-gallery-view'); const GRAIN = 'did:web:grain.social#grain_appview'; const RECENT = 'at://did:web:grain.social/social.grain.feed.generator/recent'; const q = (method, params) => ({ name: 'query', args: { method, params } }); /** A query's answer as the server gives it to no one signed in. */ const answer = (method, params, response, summary, joins) => ({ content: [{ type: 'text', text: summary }], structuredContent: { method, service: GRAIN, params, response, ...(joins ? { joins } : {}) }, isError: false }); // What the made answers are made of: the recorded feed's galleries and the // recorded gallery, its account's photos of New York. const galleries = feed.structuredContent.response.items; const nyc = gallery.structuredContent.response.gallery; const thumbs = (g) => g.items.map((photo) => photo.thumb).slice(0, 4); // More than Explore's ten, so its chips end in a See all. const cameras = [ { camera: 'samsung SM-G990W', photoCount: 9, thumbs: thumbs(galleries[0]) }, { camera: 'Apple iPhone 13 Pro Max', photoCount: 3, thumbs: thumbs(nyc) }, ...['Fujifilm X100V', 'Fujifilm X-T5', 'Ricoh GR III', 'Leica Q2', 'Sony A7 IV', 'Canon EOS R6', 'Nikon Z6', 'Olympus OM-1', 'Pentax K-3'].map((camera) => ({ camera, photoCount: 1, thumbs: [] })) ]; // Places at H3 cells HUD's own tests name (crates/hud-core/src/ui/map.rs, places.rs). const places = [ { name: 'New York, New York, US', h3Index: '8a2a10728907fff', galleryCount: 12, h3Cells: ['8a2a10728907fff'], thumbs: thumbs(nyc) }, { name: 'Brooklyn, New York, US', h3Index: '8a2a10766407fff', galleryCount: 5, h3Cells: ['8a2a10766407fff'], thumbs: thumbs(galleries[3]) }, { name: 'Amsterdam, Noord-Holland, NL', h3Index: '8a1969c9b397fff', galleryCount: 3, h3Cells: ['8a1969c9b397fff'], thumbs: thumbs(galleries[1]) }, { name: 'Cupertino, California, US', h3Index: '85283473fffffff', galleryCount: 2, h3Cells: ['85283473fffffff'], thumbs: thumbs(galleries[2]) } ]; const pins = places.map(({ name, h3Index, galleryCount }) => ({ name, h3Index, galleryCount })); const recent = { ...feed.structuredContent.response, cursor: undefined }; // The recorded gallery's photos as its account's stories of the day before the gallery's moment. const stories = nyc.items.map((photo, i) => ({ uri: `at://${nyc.creator.did}/social.grain.story/3n2story0000${i}`, cid: photo.cid, creator: nyc.creator, thumb: photo.thumb, fullsize: photo.fullsize, aspectRatio: photo.aspectRatio, locationDisplay: nyc.locationDisplay, createdAt: new Date(Date.parse('2026-11-16T20:00:00.000Z') + i * 1_800_000).toISOString() })); const actor = nyc.creator.handle; /** The screens, by the name of their tree's file: the call, its answer, and the joins of one made here, by the method each answers. */ const screens = { feed: { call: q('social.grain.feed.getFeed', feed.structuredContent.params), answer: feed }, gallery: { call: q('social.grain.gallery.getGallery', gallery.structuredContent.params), answer: gallery }, profile: { call: { name: 'describe_app', args: { domain: 'grain.social', actor: 'tx3.grapevyne.social' } }, answer: recorded('grain-app') }, notifications: { call: q('social.grain.notification.listNotifications', {}), answer: recorded('grain-notifications') }, explore: { call: q('social.grain.gallery.getLocations', {}), answer: answer('social.grain.gallery.getLocations', {}, { locations: places }, `${places.length} places from social.grain.gallery.getLocations on grain.social`, { cameras: { cameras }, recent: JSON.parse(JSON.stringify(recent)) }), joins: { cameras: 'social.grain.gallery.getCameras', recent: 'social.grain.feed.getFeed' } }, map: { call: q('social.grain.gallery.getLocations', { pins: true }), answer: answer('social.grain.gallery.getLocations', { pins: true }, { locations: pins }, `${pins.length} places from social.grain.gallery.getLocations on grain.social`, { places: { locations: places } }), joins: { places: 'social.grain.gallery.getLocations' } }, stories: { call: q('social.grain.story.getStories', { actor }), answer: answer('social.grain.story.getStories', { actor }, { stories }, `${stories.length} stories from social.grain.story.getStories on grain.social`, { who: { ...nyc.creator, galleryCount: 1 } }), joins: { who: 'social.grain.actor.getProfile' } }, 'story-archive': { call: q('social.grain.story.getArchive', { actor }), answer: answer('social.grain.story.getArchive', { actor }, { stories }, `${stories.length} stories from social.grain.story.getArchive on grain.social`), joins: {} } }; const bad = []; // The answers made here, and their joins, as their methods' lexicons say. const lexicons = lexiconsOf(); for (const [name, { answer: made, joins }] of Object.entries(screens)) { if (!joins) continue; const s = made.structuredContent; for (const [method, value, what] of [[s.method, s.response, 'the answer'], ...Object.entries(joins).map(([as, method]) => [method, s.joins[as], `its join ${as}`])]) { try { lexicons.assertValidXrpcOutput(method, value); } catch (e) { bad.push(`${name}: ${what} is not as ${method} says: ${e.message}`); } } } const trees = schemas(); rmSync(out, { recursive: true, force: true }); mkdirSync(out, { recursive: true }); for (const [name, { call, answer: given }] of Object.entries(screens)) { const doc = viewOf(call.name, call.args); const now = doc ? { ...given, _meta: { ...given._meta, view: shown(call.name, doc) } } : given; const result = JSON.parse( execFileSync(hud, ['resolve', call.name, JSON.stringify(call.args)], { input: JSON.stringify(now), encoding: 'utf8', env: { ...process.env, HUD_APPS: apps } }) ); const ui = result._meta?.ui; const checked = ui ? trees.validate('place.hud.ui', ui) : { success: false, error: new Error('no _meta.ui') }; if (!checked.success) bad.push(`${name}: _meta.ui is not as place.hud.ui says: ${checked.error.message}`); else writeFileSync(join(out, `${name}.json`), `${JSON.stringify(ui, null, '\t')}\n`); } for (const b of bad) say(b); say(`${Object.keys(screens).length - bad.length} trees in ${out}`); process.exit(bad.length ? 1 : 0);