Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

1.7 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
//
// Regenerates tray/fixtures/expected.json from the reference indexer.
// Run this after changing tray/doc-index.mjs or adding a case, then re-run
// EVERY host's rig — see tray/fixtures/README.md.
//
//   node scripts/gen-doc-index-expected.mjs

import { readFileSync, readdirSync, writeFileSync } from 'node:fs'
import { createHash } from 'node:crypto'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

import { describe, TEXT_BUDGET, SNIFF_BYTES, HEAD_BYTES } from '../tray/doc-index.mjs'

const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const dir = join(root, 'tray/fixtures')
const sha = (s) => createHash('sha256').update(s, 'utf8').digest('hex').slice(0, 16)

const cases = {}
for (const f of readdirSync(join(dir, 'cases')).sort()) {
  const d = describe(readFileSync(join(dir, 'cases', f), 'utf8'))
  const e = {
    isDocument: d.isDocument,
    title: d.title,
    app: d.app,
    encrypted: d.encrypted,
    hasPreview: d.preview !== null,
    textLength: d.text === null ? null : d.text.length,
    textSha256_16: d.text === null ? null : sha(d.text),
  }
  // Verbatim for the short cases, so the answer key is readable by a human and
  // not only checkable by a machine.
  if (d.text !== null && d.text.length <= 200) e.text = d.text
  cases[f] = e
}

writeFileSync(join(dir, 'expected.json'), JSON.stringify({
  note: 'GENERATED by scripts/gen-doc-index-expected.mjs from tray/doc-index.mjs. See README.md here.',
  textBudget: TEXT_BUDGET,
  sniffBytes: SNIFF_BYTES,
  headBytes: HEAD_BYTES,
  cases,
}, null, 2) + '\n')

console.log(`regenerated expected.json for ${Object.keys(cases).length} cases`)