Skip to content

armgit / bento

Public Imported from GitHub
0

Bento, the office suite that fits in a file

17.0 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The Bento authors
// home/webext placement rig.
//
//   node scripts/test-webext-place.ts
//
// WHAT THIS PROVES. A granted folder is placed on disk by GUESSING paths and
// PROVING one: the folder's name under each likely parent, one document's
// bytes compared at that path, the grant asked to confirm. Two things must
// hold: a wrong guess (same name, different bytes; same bytes, a copy outside
// the grant) is never recorded, and a right one is found among the usual
// places without the person doing anything. Also here: the title card drawn
// from a document that carries no preview render.

import { readFileSync } from 'node:fs'
import { join, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'

const root = join(dirname(fileURLToPath(import.meta.url)), '..')
const SRC = join(root, 'home/webext')
const place = await import('../home/webext/src/place.js')
const { cardFrom } = await import('../home/webext/src/library.js')

let failures = 0
let checks = 0
function ok(cond: boolean, msg: string) {
  checks++
  if (!cond) { failures++; console.log(`  FAIL  ${msg}`) }
  else console.log(`  ok    ${msg}`)
}

/** A fake disk: paths → bytes; directories → Chrome-style listings. */
function disk(files: Record<string, string>) {
  const fetched: string[] = []
  const listing = (dir: string) => {
    const names = new Map<string, boolean>()
    for (const p of Object.keys(files)) {
      if (!p.startsWith(dir + '/')) continue
      const rest = p.slice(dir.length + 1)
      const head = rest.split('/')[0]
      names.set(head, rest.includes('/'))
    }
    if (!names.size) return null
    return [...names].map(([n, isDir]) => `<script>addRow("${n}","${n}",${isDir ? 1 : 0},0,"0",0,"");</script>`).join('\n')
  }
  const fetch = async (url: string) => {
    const path = decodeURIComponent(url.replace(/^file:\/\//, ''))
    fetched.push(path)
    if (path.endsWith('/')) {
      const html = listing(path.slice(0, -1))
      return html ? { ok: true, text: async () => html } : { ok: false }
    }
    const body = files[path]
    if (body === undefined) return { ok: false }
    const bytes = new TextEncoder().encode(body)
    return { ok: true, arrayBuffer: async () => bytes.buffer, text: async () => body }
  }
  return { fetch, fetched }
}

/** A grant: its name, the documents in it (rel → bytes). `prefixFor` proves a path by looking the tail up in it. */
function grant(name: string, docs: Record<string, string>) {
  const dir = { name }
  const handleOf = (rel: string) => ({ getFile: async () => { const b = new TextEncoder().encode(docs[rel]); return { size: b.length, arrayBuffer: async () => b.buffer } } })
  const prefixFor = async (d: any, path: string) => {
    for (const rel of Object.keys(docs)) {
      const suffix = `/${name}/${rel}`
      if (path.endsWith(suffix)) return path.slice(0, -suffix.length) + `/${name}`
    }
    return null
  }
  const probe = (rel: string) => ({ rel: rel.split('/'), handle: handleOf(rel) })
  return { dir, prefixFor, probe }
}

console.log('\n— listing and fingerprints')
{
  const d = disk({ '/Users/you/Desktop/x.txt': 'x', '/Users/you/Decks/a.bento.html': 'A' })
  const users = await place.listDir('/Users', { fetch: d.fetch })
  ok(users.length === 1 && users[0].name === 'you' && users[0].dir, 'listDir: Chrome\'s addRow() listing → names and kinds')
  ok((await place.listDir('/nowhere', { fetch: d.fetch })).length === 0, 'listDir: a refused fetch is an empty listing, not an error')
  const file = { size: 1, arrayBuffer: async () => new TextEncoder().encode('A').buffer }
  ok(await place.sameFile('/Users/you/Decks/a.bento.html', file, { fetch: d.fetch }), 'sameFile: the same bytes at the path')
  ok(!(await place.sameFile('/Users/you/Desktop/x.txt', file, { fetch: d.fetch })), 'sameFile: same size, different bytes → no')
  ok(!(await place.sameFile('/Users/you/Decks/missing.bento.html', file, { fetch: d.fetch })), 'sameFile: nothing there → no')
  ok(place.fileUrl('/Users/you/My Decks/Q3 #1.bento.html') === 'file:///Users/you/My%20Decks/Q3%20%231.bento.html', 'fileUrl: segments encoded')
}

console.log('\n— placing a grant')
{
  const g = grant('teams-test', { 'S1.bento.html': 'DECK-S1', 'sub/B9.bento.html': 'DECK-B9' })
  // the real folder on the Desktop; a same-named folder with other bytes in Documents; a copy of S1 elsewhere
  const d = disk({
    '/Users/you/Desktop/teams-test/S1.bento.html': 'OTHER',
    '/Users/you/Documents/teams-test/S1.bento.html': 'DECK-S1',
    '/Users/you/Documents/teams-test/sub/B9.bento.html': 'DECK-B9',
    '/Users/you/Downloads/copies/S1.bento.html': 'DECK-S1',
    '/Users/shared-user/Desktop/x': 'x',
  })
  const prefix = await place.placeFolder(g.dir, g.probe('S1.bento.html'), {}, { fetch: d.fetch, prefixFor: g.prefixFor })
  ok(prefix === '/Users/you/Documents/teams-test', `placeFolder: found in Documents from nothing but the /Users listing (${prefix})`)
  ok(d.fetched.some((p) => p === '/Users/you/Desktop/teams-test/S1.bento.html'), 'the same-named folder with other bytes (Desktop, tried first) was rejected')
  ok(!d.fetched.some((p) => p.includes('/copies/')), 'a copy elsewhere is never a candidate: only the folder\'s own name is looked for')
  ok(d.fetched.length < place.MAX_CANDIDATES, `bounded: ${d.fetched.length} fetches`)
  // a probe deeper in the tree proves the same prefix
  const viaSub = await place.placeFolder(g.dir, g.probe('sub/B9.bento.html'), {}, { fetch: d.fetch, prefixFor: g.prefixFor })
  ok(viaSub === '/Users/you/Documents/teams-test', 'placeFolder: a nested document as the fingerprint lands on the same prefix')
}
{
  // the grant must agree: same bytes at the path, but prefixFor says no → not recorded
  const g = grant('Decks', { 'a.bento.html': 'A' })
  const d = disk({ '/Users/you/Decks/a.bento.html': 'A' })
  const prefix = await place.placeFolder(g.dir, g.probe('a.bento.html'), {}, { fetch: d.fetch, prefixFor: async () => null })
  ok(prefix === null, 'placeFolder: matching bytes are not enough — the grant has to resolve the path')
}
{
  // near what is already known: a sibling of a placed folder is found without any listing
  const g = grant('Decks', { 'a.bento.html': 'A' })
  const d = disk({ '/Volumes/Work/projects/Decks/a.bento.html': 'A' })
  const prefix = await place.placeFolder(g.dir, g.probe('a.bento.html'), { Notes: '/Volumes/Work/projects/Notes' }, { fetch: d.fetch, prefixFor: g.prefixFor })
  ok(prefix === '/Volumes/Work/projects/Decks', 'placeFolder: a sibling of an already-placed folder, on a volume no listing names')
  const empty = await place.placeFolder(g.dir, g.probe('a.bento.html'), {}, { fetch: async () => ({ ok: false }), prefixFor: g.prefixFor })
  ok(empty === null, 'placeFolder: a disk that answers nothing → null, quietly')
  ok((await place.placeFolder(g.dir, { rel: [], handle: null }, {}, { fetch: d.fetch, prefixFor: g.prefixFor })) === null, 'placeFolder: no probe document → null')
}
{
  const dirs = await place.candidateDirs('X', { A: '/Users/you/Documents/A' }, { fetch: async () => ({ ok: false }) })
  ok(dirs[0] === '/Users/you/Documents/X' && dirs.includes('/Users/you/Desktop/X') && dirs.includes('/Users/you/X'), 'candidateDirs: beside the known folder first, then the usual places in that home')
  ok(new Set(dirs).size === dirs.length && dirs.length <= place.MAX_CANDIDATES, 'candidateDirs: unique and bounded')
}

console.log('\n— the title card from a document with no preview')
{
  const doc = { format: 'bento/slides', title: 'Q3', theme: { background: '#FFFFFF', color: '#1E2A3A', accent: '#F28C5A' }, slides: [
    { id: 's0', stateOf: 'x', background: '#000000', elements: [{ type: 'text', html: 'hidden state' }] },
    { id: 's1', background: '#123456', elements: [{ type: 'text', role: 'kicker', html: '<b>Kicker</b>', fontSize: 20 }, { type: 'text', role: 'title', html: 'The <i>file</i> is the&nbsp;software.', fontSize: 64 }, { type: 'text', html: 'sub', placeholder: 'x' }, { type: 'shape', html: 'no' }, { type: 'text', md: 'body words', fontSize: 24 }] },
  ] }
  const shell = `<html><script id="bento-doc" type="application/json">${JSON.stringify(doc).replace(/</g, '\\u003c')}</script><body></body></html>`
  const card = cardFrom(shell)!
  ok(!!card && card.bg === '#123456' && card.ink === '#1E2A3A' && card.accent === '#F28C5A', 'cardFrom: the first NON-state slide\'s background, the theme\'s ink and accent')
  ok(card.lines.map((l) => l.text).join('|') === 'Kicker|The file is the software.|body words', 'cardFrom: the slide\'s texts, tags stripped, placeholders and shapes skipped')
  ok(card.lines[1].role === 'title' && card.lines[1].size === 64, 'cardFrom: role and size ride along so the card can lead with the title')
  ok(cardFrom('<html>no doc</html>') === null && cardFrom('<script id="bento-doc">{bad</script>') === null, 'cardFrom: not a deck → null')
  const bare = cardFrom(`<script id="bento-doc">${JSON.stringify({ slides: [{ id: 'a', elements: [] }] })}</script>`)!
  ok(bare && bare.bg === null && bare.lines.length === 0, 'cardFrom: missing colours stay null (the UI palette fills in), no literal of its own')
  ok(cardFrom(`<script id="bento-doc">${JSON.stringify({ slides: [{ id: 'a', background: 'url(javascript:x)', elements: [] }] })}</script>`)!.bg === null, 'cardFrom: a background that is not a colour is dropped')
}
{
  const manifest = JSON.parse(readFileSync(join(SRC, 'manifest.json'), 'utf8'))
  ok(Array.isArray(manifest.host_permissions) && manifest.host_permissions.includes('file:///*') && manifest.host_permissions.length === 1, 'manifest: file:///* is the only declared host — what reading a local path needs, and nothing on the web')
  const home = readFileSync(join(SRC, 'src/home.js'), 'utf8')
  ok(/placeFolders\(\)/.test(home) && /placeFolders\(\{ force: true \}\)/.test(home), 'home.js places on load and again on Find my folders / a click')
  ok(!/card\.disabled = true/.test(home), 'an unplaced card is no longer disabled — it explains and retries')
}

console.log('\n— the scan: documents found without a grant')
{
  const d = disk({
    '/Users/you/Documents/Decks/Q3.bento.html': 'A',
    '/Users/you/Documents/Decks/old/Q2.bento.html': 'B',
    '/Users/you/Documents/notes.txt': 'n',
    '/Users/you/Documents/node_modules/x/y.bento.html': 'skip',
    '/Users/you/Desktop/teams-test/S1.bento.html': 'C',
    '/Users/you/Downloads/Untitled.bento.html': 'D',
    '/Users/you/Library/Mobile Documents/com~apple~CloudDocs/Work/W.bento.html': 'E',
    '/Users/you/Movies/x.bento.html': 'not scanned',
    '/Users/Shared/y.bento.html': 'not a home',
  })
  const found = await place.scanDisk({ fetch: d.fetch })
  const paths = found.map((f) => f.path).sort()
  ok(paths.join('|') === [
    '/Users/you/Desktop/teams-test/S1.bento.html',
    '/Users/you/Documents/Decks/Q3.bento.html',
    '/Users/you/Documents/Decks/old/Q2.bento.html',
    '/Users/you/Downloads/Untitled.bento.html',
    '/Users/you/Library/Mobile Documents/com~apple~CloudDocs/Work/W.bento.html',
  ].join('|'), `scanDisk: every .bento.html under Documents/Desktop/Downloads/iCloud, nested, nothing else (${found.length})`)
  ok(!paths.some((p) => p.includes('node_modules') || p.includes('/Movies/') || p.startsWith('/Users/Shared')), 'scanDisk: noisy trees, other folders and non-home users are skipped')
  ok(found[0].name.endsWith('.bento.html') && found.every((f) => f.dir && !f.dir.endsWith('/')), 'scanDisk: name and directory for each')
  ok(!d.fetched.some((p) => p.endsWith('.bento.html')), 'scanDisk: listings only — no document bytes are read')
  const none = await place.scanDisk({ fetch: async () => ({ ok: false }) })
  ok(none.length === 0, 'scanDisk: a disk that answers nothing → nothing, no throw')
  const deep = disk(Object.fromEntries(Array.from({ length: 3 }, (_, i) => [`/Users/u/Documents/${'d/'.repeat(place.SCAN_DEPTH + 2)}f${i}.bento.html`, 'x'])))
  ok((await place.scanDisk({ fetch: deep.fetch })).length === 0, 'scanDisk: depth-limited')
  const home = readFileSync(join(SRC, 'src/home.js'), 'utf8')
  ok(/scannedDocs\(/.test(home) && /scanned: true/.test(home) && /addFolderFor/.test(home), 'home.js lists found documents beside granted ones and offers the grant per folder')
  ok(/prefixFor\(dir, d\.path\)/.test(home), 'a grant made for a found document is checked against that document\'s path before it is kept')
}

console.log('\n— Windows and Linux homes')
{
  // Windows: a drive-letter home, Documents moved into OneDrive, listings under C:/Users
  const d = disk({
    '/C:/Users/you/OneDrive/Documents/Decks/Q3.bento.html': 'A',
    '/C:/Users/you/Desktop/D.bento.html': 'B',
    '/C:/Users/Public/x.bento.html': 'no',
    '/C:/Windows/System32/y.bento.html': 'no',
  })
  const found = (await place.scanDisk({ fetch: d.fetch })).map((f) => f.path).sort()
  ok(found.join('|') === '/C:/Users/you/Desktop/D.bento.html|/C:/Users/you/OneDrive/Documents/Decks/Q3.bento.html', 'Windows: C:/Users/<name> homes, OneDrive known-folder move, Public and Windows skipped')
  ok(place.fileUrl('/C:/Users/you/Desktop/D.bento.html') === 'file:///C:/Users/you/Desktop/D.bento.html', 'Windows: the drive letter survives in the file URL')
  // Linux: localized XDG names from user-dirs.dirs, a /home listing
  const l = disk({
    '/home/you/.config/user-dirs.dirs': 'XDG_DESKTOP_DIR="$HOME/Schreibtisch"\nXDG_DOWNLOAD_DIR="$HOME/Downloads"\nXDG_DOCUMENTS_DIR="$HOME/Dokumente"\nXDG_MUSIC_DIR="$HOME/Musik"\n',
    '/home/you/Dokumente/Vortrag.bento.html': 'A',
    '/home/you/Schreibtisch/S.bento.html': 'B',
    '/home/you/Musik/m.bento.html': 'no',
  })
  const lf = (await place.scanDisk({ fetch: l.fetch })).map((f) => f.path).sort()
  ok(lf.join('|') === '/home/you/Dokumente/Vortrag.bento.html|/home/you/Schreibtisch/S.bento.html', 'Linux: the XDG names the home declares (Dokumente, Schreibtisch), music not scanned')
  ok(place.xdgDirs('XDG_DOCUMENTS_DIR="/mnt/data/docs"\nXDG_DESKTOP_DIR="$HOME/Desktop/"', '/home/u').join('|') === '/mnt/data/docs|/home/u/Desktop', 'xdgDirs: absolute paths kept, $HOME expanded, trailing slash dropped')
}

console.log('\n— an empty grant (the Bento folder) is placed through a marker')
{
  const files: Record<string, string> = {}
  const d = disk(files)
  let removed: string | null = null
  const dir: any = {
    name: 'Bento',
    // writing the marker into the grant makes it appear on the fake disk at the real path
    getFileHandle: async (name: string) => ({
      createWritable: async () => ({ write: async (b: Uint8Array) => { files[`/Users/you/Documents/Bento/${name}`] = String.fromCharCode(...b) }, close: async () => {} }),
      getFile: async () => { const body = files[`/Users/you/Documents/Bento/${name}`]; const b = new TextEncoder().encode(body); return { size: b.length, arrayBuffer: async () => b.buffer } },
    }),
    removeEntry: async (name: string) => { removed = name; delete files[`/Users/you/Documents/Bento/${name}`] },
  }
  ;(globalThis as any).crypto ??= { getRandomValues: (a: Uint8Array) => { for (let i = 0; i < a.length; i++) a[i] = (i * 37) % 251; return a } }
  const prefixFor = async (_d: any, path: string) => (path.startsWith('/Users/you/Documents/Bento/') ? '/Users/you/Documents/Bento' : null)
  const prefix = await place.placeFolder(dir, null, {}, { fetch: d.fetch, prefixFor })
  ok(prefix === '/Users/you/Documents/Bento', 'placeFolder: no document to fingerprint → a marker file is written, found, and the folder placed')
  ok(removed !== null && String(removed).startsWith('.bento-place-') && Object.keys(files).length === 0, 'the marker is removed afterwards, whatever happened')
  const home = readFileSync(join(SRC, 'src/home.js'), 'utf8')
  ok(/createBentoFolder/.test(home) && /defaultFolder/.test(home) && /startIn: 'documents'/.test(home), 'home.js offers the Bento folder, remembers it as the default for new documents, and opens the picker in Documents')
}

console.log('\n— folders the OS keeps from the browser')
{
  // Documents named by the home listing but refused when listed itself; Desktop readable; Downloads empty but readable
  const files = { '/Users/you/Desktop/x.bento.html': 'x', '/Users/you/Documents/hidden.bento.html': 'h' }
  const d = disk(files)
  const inner = d.fetch
  const fetch = async (url: string) => {
    if (/\/Users\/you\/Documents\//.test(url)) return { ok: false }
    if (url === 'file:///Users/you/Downloads/') return { ok: true, text: async () => '' }
    if (url === 'file:///Users/you/') return { ok: true, text: async () => ['Desktop', 'Documents', 'Downloads'].map((n) => `<script>addRow("${n}","${n}",1,0,"0",0,"");</script>`).join('') }
    return inner(url)
  }
  const blocked = await place.blockedFolders({ fetch })
  ok(blocked.join() === '/Users/you/Documents', `blockedFolders: a folder the home names but refuses to list, and only that (${blocked.join()})`)
  ok(!blocked.includes('/Users/you/Downloads'), 'an EMPTY folder that lists fine is not "blocked"')
  const home = readFileSync(join(SRC, 'src/home.js'), 'utf8')
  ok(/noticeOsBlocked/.test(home) && /openedAt/.test(home), 'the library says which folders the OS refuses, and lists opened documents')
}

console.log(`\n${checks - failures}/${checks} checks passed`)
process.exit(failures ? 1 : 0)