Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

5. Write a test

Test the app the way it runs — against a real node, with vitest. A global setup boots a node for the run; the tests mint an enclave, write, and read back over HTTP.

npm install -D vitest

vitest.config.mjs:

import { defineConfig } from 'vitest/config'
 
export default defineConfig({
  test: { globalSetup: './test/global-node.mjs', hookTimeout: 120_000, testTimeout: 60_000 },
})

test/global-node.mjs — boots the node (and reuses one if already running):

import { spawn } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { dirname, resolve } from 'node:path'
 
const __dirname = dirname(fileURLToPath(import.meta.url))
const NODE_DIR = process.env.NODE_DIR || resolve(__dirname, '../../impl-node')
const PORT = Number(process.env.NODE_PORT || 8787)
const BASE = `http://localhost:${PORT}/`
 
export default async function () {
  try { if ((await fetch(BASE)).ok) return () => {} } catch {}     // reuse a running node
  const node = spawn(
    'npx', ['wrangler', 'dev', '--config', 'test/wrangler.toml', '--local', '--port', String(PORT)],
    { cwd: NODE_DIR, stdio: 'ignore', detached: true },
  )
  for (let i = 0; i < 90; i++) {
    await new Promise((r) => setTimeout(r, 1000))
    try { if ((await fetch(BASE)).ok) return async () => { try { process.kill(-node.pid) } catch {} } } catch {}
  }
  throw new Error(`ENC node did not start at ${BASE}`)
}

personal.test.mjs:

import { test, expect } from 'vitest'
import { createPersonalSdk } from './enc-personal.mjs'
 
const nodeUrl = 'http://localhost:8787'
 
test('public post round-trips on a real node', async () => {
  const sdk = await createPersonalSdk({ nodeUrl })
  await sdk.submitPublic({ draft: 'gm everyone' })
  const posts = await sdk.queryPublic()
  expect(posts.map((p) => JSON.parse(p.content).draft)).toContain('gm everyone')
})
 
test('private notes round-trip for the owner', async () => {
  const sdk = await createPersonalSdk({ nodeUrl })
  await sdk.submitPrivate({ draft: 'a private note' })
  const notes = await sdk.queryPrivate()
  expect(notes.map((n) => JSON.parse(n.content).draft)).toContain('a private note')
})

Run them:

$ npx vitest run
 Test Files  1 passed (1)
      Tests  2 passed (2)

The tests sign real commits and read them back through the node's ECDH-authenticated query path — the same code your app runs, exercised against the real protocol, not a mock.

Next: Deploy to production →