~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/knotless

git that lives in your Atmosphere account

Forked from chadtmiller.com/knotless

0
0
59 lines · 2.4 KB Raw History
// Before the screenshots: the pages of one side of what compare.mjs
// compares, played back from a recording in front of the pages built from
// that side. SHOTS_BUILD and SHOTS_FIXTURES name the side's web/build and
// fixtures.json; this checkout's are the default. With SHOTS_LIVE=1, or
// with no recording, a world instead: the one `just shots-up` keeps up if
// it is there, else a new one for this run, and knotless-serve from this
// build in front of it. The pages are written to target/shots/views.json,
// and the cards their pages name to cards.json: knotless-serve draws the
// cards, so they are shot only from a world, and from fixtures cards.json
// is null and cards.spec.mjs skips them. Which pages to skip, as their HTML
// is the same on both sides, compare.mjs has written to unchanged.json.
// Returns the teardown.

import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { create, page, root, saved, serve, until } from './world.mjs'
import { front, recording } from './fixtures.mjs'

const kept = async () => {
  if (!existsSync(saved)) return null
  const world = JSON.parse(readFileSync(saved, 'utf8'))
  // Kept up from before cards were shot, it is seeded differently.
  if (!world.cards) return null
  const up = await fetch(`${world.net.pds}/xrpc/com.atproto.server.describeServer`).then(
    (r) => r.ok,
    () => false,
  )
  return up ? world : null
}

export default async function setup() {
  const recorded = process.env.SHOTS_LIVE
    ? null
    : recording(process.env.SHOTS_FIXTURES || undefined)
  let world = recorded ? { views: recorded.views } : await kept()
  let teardown
  if (recorded) {
    const stand = await front({
      recorded: recorded.answers,
      build: process.env.SHOTS_BUILD || undefined,
    })
    teardown = () => stand.kill()
  } else if (world) {
    const server = await serve(world)
    teardown = async () => server.kill()
    // The index is on disk already; this waits only for the mirrors.
    await until('the pages', async () => (await page(world.views.pull)).includes('Merged as'))
  } else {
    world = await create()
    teardown = async () => world.stop()
  }
  mkdirSync(join(root, 'target/shots'), { recursive: true })
  writeFileSync(join(root, 'target/shots/views.json'), JSON.stringify(world.views, null, 2))
  writeFileSync(
    join(root, 'target/shots/cards.json'),
    JSON.stringify(recorded ? null : world.cards, null, 2),
  )
  return teardown
}