Files
superbrain/scripts/regressions.mjs
T
Andrew S Erwin a8bd0ab876 CalDAV calendars: each server calendar a Collection, each event a URL bookmark
Settings -> CalDAV connects an external CalDAV endpoint (any RFC 4791
service; on Core the deployment's NEXTCLOUD_URL adds a Nextcloud-calendars
one-click prefill alongside the existing files one). Each server calendar
materializes as its own Collection and every in-window event becomes a
Bookmark carrying the event's DAV .ics URL.

internal/caldav mirrors internal/webdav:
- client.go — PROPFIND discovery (accepts a calendar home via
  calendar-home-set or a single calendar), REPORT calendar-query over a
  rolling window (last 30d .. next 180d), netguard-pinned 80/443, Basic
  auth in the header only, redirects refused, bounded (50 calendars /
  500 events);
- ics.go — RFC 5545 unfolding + first-VEVENT parse (UID/SUMMARY/DTSTART);
- sync.go — RunLoop/RunOnce under a dedicated advisory lease, reconciling
  each calendar to a Collection and each event to a bookmark via the
  CalDavEvent ledger. Same tombstone/detach discipline as WebDAV: a
  vanished OR out-of-window event detaches membership but keeps the
  bookmark; a user-deleted event never resurrects; a re-deleted collection
  is recreated and re-linked; a truncated window never reconciles
  deletions; one unreadable calendar does not abort the rest.

Three tables (CalDavConnection/CalDavCalendar/CalDavEvent) via migration 021
+ schema.sql. Handlers expose /api/caldav/connections CRUD + /test + /sync
with the house credential (encrypted, masked, write-only), version (409 on
stale), and AllowConnector("caldav") plan gate on creation AND every sync.
Wired as the fifth background loop in main.go.

Settings -> CalDAV tab (web/components/caldav-settings.tsx): masked
write-only password, version on PATCH, per-row calendar list, verbatim
test/sync results, Core suggestion prefill with the viewer's email
local-part, allowed flag drives the plan notice.

Verification: full Go integration suite green incl. four TestCalDav*
(lifecycle, collection-recreation, plan gate + suggestion) on an httptest
fake CalDAV server; make db-converge passes; browser regressions all 18
PASS incl. the new CalDAV settings walkthrough; web lint/SSR-lint/build
clean. API.md, README, .env.example, Core installer and local AGENTS
documents updated.
2026-11-15 09:34:33 -06:00

735 lines
45 KiB
JavaScript

import assert from 'node:assert/strict'
import http from 'node:http'
import { spawn } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import { chromium } from 'playwright'
// The real built Next server/UI with isolated API fixtures: no app users or DB writes.
// The real Go projection (pageView) always carries these arrays; the fixture
// must too, or client code reading them crashes exactly as the tests prove.
const newPageFixture = (id, position) => ({ id, title: `Page ${id.toUpperCase()}`, content: '', version: 1, position, pinned: false, bookmarks: [], backlinks: [], links: [], embeds: [] })
const pages = new Map(['a', 'b'].map((id, position) => [id, newPageFixture(id, position)]))
const tags = [{ id: 'one', name: 'First tag', color: '#123456' }, { id: 'two', name: 'Second tag', color: '#654321' }]
const kanbanColumns = [
{ id: 'inbox', label: 'Inbox', color: '#123456', key: 'INBOX', position: 0 },
{ id: 'todo', label: 'To Do', color: '#abcdef', key: 'TODO', position: 1 },
]
const bookmarks = Array.from({ length: 205 }, (_, i) => ({
id: `bookmark-${i}`, ownerId: 'fixture-user', title: `Fixture ${String(i).padStart(3, '0')}`, url: 'https://127.0.0.1/test',
description: '', notes: '', tags: [{ tag: tags[0] }], sharedWith: [], kanbanStatus: 'INBOX', kanbanColumnId: 'inbox', kanbanPosition: i,
isPublic: false, imapWatchEnabled: false, alertAt: null, dueDate: null, version: 0, createdAt: '2026-10-07T00:00:00Z', updatedAt: '2026-10-07T00:00:00Z',
}))
let failSaves = false
// Review CR-02: forces Go's signout endpoint to fail so the browser test can
// prove a revocation failure is reported accurately and local sign-out still
// completes.
let failSignout = false
// Fails only saves whose body.content matches (deterministic mid-loop
// failure for the DATA-07 envelope test — no racing the serial queue).
let failContent = null
// Mirrors the proxy's streaming body cap (web/app/api/[...path]/route.ts).
const MAX_PROXY_BODY = 1 << 20
// Records Authorization headers of POST /api/auth/signout so the browser
// sign-out test can prove the Next server reached Go's revocation endpoint.
const signouts = []
let delaySaves = 0
let activeSaves = 0
let maxConcurrentSaves = 0
let paginatedRequests = 0
// Board moves are recorded so the browser test can prove a drag reached the
// API with the card's expected version/position.
const kanbanMoves = []
// Counts every PATCH /api/pages/* attempt (including 409/500 rejections) so
// tests can prove a paused conflict issues no further saves (review TEST-01).
let patchAttempts = 0
// WebDAV fixture: masked rows + a deployment suggestion, mirroring
// handlers/webdav.go's projection so the settings UI exercises against data
// it will really see.
const webdavConnections = [{
id: 'webdav-1', name: 'House docs', url: 'https://dav.example.org/remote.php/dav/files/erwin/Documents',
username: 'erwin', secret: '••••••••', collectionId: null, enabled: true,
lastSyncedAt: null, lastError: null, version: 1, createdAt: '2026-11-10T00:00:00Z', updatedAt: '2026-11-10T00:00:00Z',
}]
let lastWebdavCreate = null
let lastCaldavCreate = null
const webdavSuggestions = [{ kind: 'nextcloud', name: 'Nextcloud files', url: 'https://cloud.federatedcomputer.ai/remote.php/dav/files/' }]
// CalDAV fixture: same discipline, but a connection carries a calendars[]
// array (one collection per server calendar) and no top-level collectionId.
const caldavConnections = [{
id: 'caldav-1', name: 'My calendars', url: 'https://dav.example.org/remote.php/dav/calendars/erwin',
username: 'erwin', secret: '••••••••', enabled: true,
calendars: [{ href: '/home/work/', name: 'Work', collectionId: 'col-work' }],
lastSyncedAt: null, lastError: null, version: 1, createdAt: '2026-11-10T00:00:00Z', updatedAt: '2026-11-10T00:00:00Z',
}]
const caldavSuggestions = [{ kind: 'nextcloud', name: 'Nextcloud calendars', url: 'https://cloud.federatedcomputer.ai/remote.php/dav/calendars/' }]
const api = http.createServer(async (req, res) => {
try {
let raw = ''
for await (const chunk of req) raw += chunk
// Mirror Go's content-type discipline (internal/http security_test):
// mutating requests carrying a non-JSON Content-Type are rejected
// outright — the body is never JSON-sniffed.
const ctype = (req.headers['content-type'] || '').split(';')[0].trim().toLowerCase()
if (!['GET', 'HEAD', 'DELETE'].includes(req.method) && req.headers['content-type'] && ctype !== 'application/json') {
res.writeHead(415, { 'Content-Type': 'application/json' })
res.end('{"error":"Content-Type must be application/json"}')
return
}
const body = raw ? JSON.parse(raw) : {}
const url = new URL(req.url, 'http://fixture')
let result = []
if (url.pathname === '/api/auth/login') result = { user: { id: 'fixture-user', name: 'Fixture', email: body.email, role: 'USER' }, tokenVersion: 1 }
else if (url.pathname === '/api/auth/signout' && req.method === 'POST') {
signouts.push(req.headers.authorization || '')
if (failSignout) { res.writeHead(500, { 'Content-Type': 'application/json' }); res.end('{"error":"Fixture revocation failed"}'); return }
result = { ok: true }
}
else if (url.pathname === '/api/me') result = { user: { id: 'fixture-user' }, effectiveUser: { id: 'fixture-user' }, isAdmin: false, viewingAs: null }
// The list endpoint is a metadata projection: content bodies stay out (PERF-01).
else if (url.pathname === '/api/pages') {
if (req.method === 'PUT') {
for (const [position, id] of (body.order || []).entries()) {
const page = pages.get(id)
if (page) page.position = position
}
result = { ok: true }
} else {
result = [...pages.values()].sort((a, b) => a.position - b.position).map(({ content, ...summary }) => summary)
}
}
else if (url.pathname.startsWith('/api/pages/')) {
const id = url.pathname.split('/')[3]
if (req.method === 'PATCH') {
patchAttempts++
if (failSaves) { res.writeHead(500, { 'Content-Type': 'application/json' }); res.end('{"error":"Fixture save failed"}'); return }
if (failContent !== null && body.content === failContent) { res.writeHead(500, { 'Content-Type': 'application/json' }); res.end('{"error":"Fixture save failed"}'); return }
const target = pages.get(id)
// Optimistic concurrency (DATA-01): a stale base version is a 409.
if (body.version !== undefined && body.version !== target.version) {
res.writeHead(409, { 'Content-Type': 'application/json' })
res.end('{"error":"Page was modified elsewhere; reload to see the latest version"}')
return
}
activeSaves++
maxConcurrentSaves = Math.max(maxConcurrentSaves, activeSaves)
await new Promise(resolve => setTimeout(resolve, delaySaves))
const { version, addBookmarkId, removeBookmarkId, ...data } = body
Object.assign(target, data)
// Mirrors Go's page PATCH link ops: add is visibility-checked and
// idempotent (ON CONFLICT DO NOTHING), remove just detaches.
if (removeBookmarkId) target.bookmarks = target.bookmarks.filter(x => x.bookmark.id !== removeBookmarkId)
if (addBookmarkId && !target.bookmarks.some(x => x.bookmark.id === addBookmarkId)) {
const bookmark = bookmarks.find(b => b.id === addBookmarkId)
if (bookmark) target.bookmarks.push({ pageId: id, bookmarkId: bookmark.id, position: target.bookmarks.length, bookmark })
}
if ('content' in body) target.version++
activeSaves--
}
result = pages.get(id)
} else if (url.pathname === '/api/bookmarks' && req.method === 'POST') {
const bookmark = {
id: `bookmark-${bookmarks.length}`, ownerId: 'fixture-user', title: body.url, url: body.url,
description: '', notes: '', tags: [], sharedWith: [], kanbanStatus: 'INBOX', kanbanColumnId: null, kanbanPosition: null,
isPublic: false, imapWatchEnabled: false, alertAt: null, dueDate: null, version: 0,
createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
}
bookmarks.push(bookmark)
result = bookmark
} else if (url.pathname === '/api/bookmarks') {
const shared = url.searchParams.get('shared') === 'true'
const q = (url.searchParams.get('search') || '').toLowerCase()
// Go's search matches title, url, notes and tags (bookmarks.go); the
// url match is what lets the pages workspace reuse an existing bookmark
// for a URL instead of creating a duplicate.
const matching = shared ? [] : bookmarks.filter(b => !q || b.title.toLowerCase().includes(q) || b.url.toLowerCase().includes(q))
const offset = Number(url.searchParams.get('cursor') || 0)
const limit = Number(url.searchParams.get('limit') || 200)
// Mirror the Go permission-specific projections (PRIV-02): shared
// readers get the narrowed allowlist shape, owners the full row.
const project = b => shared ? (({ contact, contactId, sharedWith, alertAt, alertSent, imapQuery, imapWatchEnabled, kanbanColumnId, kanbanPosition, ...rest }) => rest)(b) : b
result = matching.slice(offset, offset + limit).map(project)
if (offset + limit < matching.length) res.setHeader('X-Next-Cursor', String(offset + limit))
if (offset) paginatedRequests++
} else if (url.pathname === '/api/webdav/connections') {
if (req.method === 'POST') {
lastWebdavCreate = body
const conn = {
id: `webdav-${webdavConnections.length + 1}`, name: body.name, url: body.url, username: body.username,
secret: body.password ? '••••••••' : '', collectionId: null, enabled: true,
lastSyncedAt: null, lastError: null, version: 1, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
}
webdavConnections.push(conn)
result = conn
} else {
result = {
connections: webdavConnections, allowed: true,
suggestions: webdavSuggestions,
}
}
} else if (/^\/api\/webdav\/connections\/[^/]+\/(test|sync)$/.test(url.pathname) && req.method === 'POST') {
result = url.pathname.endsWith('/sync')
? { ok: true, files: 3, created: 3, reused: 0, updated: 0, kept: 0, removed: 0, truncated: false }
: { ok: true }
} else if (/^\/api\/webdav\/connections\/[^/]+$/.test(url.pathname)) {
const conn = webdavConnections.find(b => b.id === url.pathname.split('/')[3])
if (!conn) { res.writeHead(404); res.end('{}'); return }
if (req.method === 'DELETE') { webdavConnections.splice(webdavConnections.indexOf(conn), 1); result = { ok: true } }
else if (req.method === 'PATCH') {
if (body.version !== conn.version) {
res.writeHead(409, { 'Content-Type': 'application/json' })
res.end('{"error":"Connection was modified elsewhere; reload to see the latest version"}')
return
}
const { version, password, ...data } = body
Object.assign(conn, data, { version: conn.version + 1 })
if (password && password !== '••••••••') conn.secret = '••••••••'
result = conn
} else result = conn
} else if (url.pathname === '/api/caldav/connections') {
if (req.method === 'POST') {
lastCaldavCreate = body
const conn = {
id: `caldav-${caldavConnections.length + 1}`, name: body.name, url: body.url, username: body.username,
secret: body.password ? '••••••••' : '', enabled: true, calendars: [],
lastSyncedAt: null, lastError: null, version: 1, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(),
}
caldavConnections.push(conn)
result = conn
} else {
result = { connections: caldavConnections, allowed: true, suggestions: caldavSuggestions }
}
} else if (/^\/api\/caldav\/connections\/[^/]+\/(test|sync)$/.test(url.pathname) && req.method === 'POST') {
result = url.pathname.endsWith('/sync')
? { ok: true, calendars: 2, events: 4, created: 4, reused: 0, updated: 0, kept: 0, removed: 0, truncated: false }
: { ok: true, calendars: ['Work', 'Personal'] }
} else if (/^\/api\/caldav\/connections\/[^/]+$/.test(url.pathname)) {
const conn = caldavConnections.find(b => b.id === url.pathname.split('/')[3])
if (!conn) { res.writeHead(404); res.end('{}'); return }
if (req.method === 'DELETE') { caldavConnections.splice(caldavConnections.indexOf(conn), 1); result = { ok: true } }
else if (req.method === 'PATCH') {
if (body.version !== conn.version) {
res.writeHead(409, { 'Content-Type': 'application/json' })
res.end('{"error":"Connection was modified elsewhere; reload to see the latest version"}')
return
}
const { version, password, ...data } = body
Object.assign(conn, data, { version: conn.version + 1 })
if (password && password !== '••••••••') conn.secret = '••••••••'
result = conn
} else result = conn
} else if (url.pathname === '/api/tags') result = tags
else if (url.pathname === '/api/kanban/columns') result = kanbanColumns
else if (/^\/api\/kanban\/cards\/[^/]+$/.test(url.pathname) && req.method === 'PUT') {
const bookmark = bookmarks.find(b => b.id === url.pathname.split('/')[4])
if (!bookmark) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end('{"error":"Not found"}')
return
}
if (body.version === undefined) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end('{"error":"version is required when moving a card"}')
return
}
if (body.version !== bookmark.version) {
res.writeHead(409, { 'Content-Type': 'application/json' })
res.end('{"error":"Bookmark was modified elsewhere"}')
return
}
const column = kanbanColumns.find(c => c.id === body.columnId)
if (!column) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end('{"error":"Invalid columnId"}')
return
}
const target = bookmarks
.filter(b => b.id !== bookmark.id && b.kanbanColumnId === column.id)
.sort((a, b) => a.kanbanPosition - b.kanbanPosition || b.updatedAt.localeCompare(a.updatedAt))
const position = Math.max(0, Math.min(Number(body.position) || 0, target.length))
target.splice(position, 0, bookmark)
bookmark.kanbanColumnId = column.id
bookmark.version++
target.forEach((b, i) => { b.kanbanPosition = i })
kanbanMoves.push({ id: bookmark.id, columnId: column.id, position, version: body.version })
result = bookmark
} else if (/^\/api\/bookmarks\/[^/]+$/.test(url.pathname)) {
const bookmark = bookmarks.find(b => b.id === url.pathname.split('/')[3])
if (req.method === 'PUT') {
// Review CR-04: mirror Go's bookmark concurrency contract — version
// is required (400), a stale base is 409 with the row untouched,
// success bumps the revision.
if (body.version === undefined) {
res.writeHead(400, { 'Content-Type': 'application/json' })
res.end('{"error":"version is required when updating a bookmark; refetch it and retry"}')
return
}
if (body.version !== bookmark.version) {
res.writeHead(409, { 'Content-Type': 'application/json' })
res.end('{"error":"Bookmark was modified elsewhere; reload to see the latest version"}')
return
}
const { tagIds, version, ...data } = body
Object.assign(bookmark, data)
if (tagIds) bookmark.tags = tagIds.map(id => ({ tag: tags.find(tag => tag.id === id) }))
bookmark.version++
}
result = bookmark
} else if (url.pathname.endsWith('/signals')) result = { sources: [], signals: [], types: [] }
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(result))
} catch (error) { res.writeHead(500); res.end(String(error)) }
})
await new Promise(resolve => api.listen(0, '127.0.0.1', resolve))
// The free-port probe is TOCTOU-racy: another process can grab the port
// between close and `next start`. Retry up to three ports before giving up.
let base = ''
let web = null
let logs = ''
for (let attempt = 1; attempt <= 3; attempt++) {
const portProbe = http.createServer()
await new Promise(resolve => portProbe.listen(0, '127.0.0.1', resolve))
const port = portProbe.address().port
await new Promise(resolve => portProbe.close(resolve))
base = `http://localhost:${port}`
logs = ''
web = spawn(process.execPath, ['node_modules/next/dist/bin/next', 'start', '--port', String(port)], {
cwd: fileURLToPath(new URL('../web/', import.meta.url)),
env: { ...process.env, AUTH_SECRET: 'isolated-browser-fixture-secret', NEXTAUTH_SECRET: 'isolated-browser-fixture-secret', NEXTAUTH_URL: base, API_BASE_URL: `http://127.0.0.1:${api.address().port}`, OIDC_ISSUER: '', OIDC_CLIENT_ID: '', OIDC_CLIENT_SECRET: '' },
stdio: ['ignore', 'pipe', 'pipe'],
})
web.stdout.on('data', data => logs += data)
web.stderr.on('data', data => logs += data)
let started = false
for (let i = 0; i < 150; i++) {
try { if ((await fetch(`${base}/login`)).ok) { started = true; break } } catch { /* not up yet */ }
if (/EADDRINUSE/.test(logs) || web.exitCode !== null) break
await new Promise(resolve => setTimeout(resolve, 100))
}
if (started) break
web.kill('SIGKILL')
if (attempt === 3) assert.fail(`Next server did not start after 3 ports: ${logs}`)
}
const attacker = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html')
res.end(`<form method="POST" action="${base}/api/pages" enctype="text/plain"><input name='{"title":"forged","extra":"' value='"}'></form><script>document.forms[0].submit()</script>`)
})
await new Promise(resolve => attacker.listen(0, '127.0.0.1', resolve))
async function eventually(check, label) {
for (let i = 0; i < 100; i++) {
if (await check()) return
await new Promise(resolve => setTimeout(resolve, 100))
}
assert.fail(label)
}
// The UI logs in through NextAuth's credentials callback (app/login/client.tsx
// signIn('credentials')); the standalone Next /api/auth/login route was removed
// as dead in 4e421f6, so the tests must speak the same protocol as the browser:
// fetch a CSRF token, POST the callback, and prove the session landed.
async function fixtureLogin(context) {
const { csrfToken } = await context.request.get(`${base}/api/auth/csrf`).then(r => r.json())
await context.request.post(`${base}/api/auth/callback/credentials`, {
headers: { Origin: base },
form: { csrfToken, email: 'fixture@superbrain.test', password: 'password', callbackUrl: `${base}/pages` },
})
const session = await context.request.get(`${base}/api/auth/session`).then(r => r.json())
assert.equal(session?.user?.id, 'fixture-user', 'fixture login did not establish a NextAuth session')
}
let browser
try {
await eventually(async () => { try { return (await fetch(`${base}/login`)).ok } catch { return false } }, `Next server did not start: ${logs}`)
browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || '/usr/bin/chromium', headless: true, args: ['--no-sandbox'] })
const context = await browser.newContext({ timezoneId: 'America/Chicago' })
const page = await context.newPage()
const errors = []
page.on('pageerror', error => errors.push(error.message))
page.on('dialog', dialog => dialog.accept())
await page.goto(`http://127.0.0.1:${attacker.address().port}`)
await page.waitForURL(`${base}/api/pages`)
await page.getByText('Invalid request origin').waitFor()
assert.equal((await context.request.get(`${base}/api/auth/session`).then(r => r.json()))?.user, undefined)
// Proxy CSRF barriers (web/app/api/[...path]/route.ts): a present attacker
// Origin is rejected, and a stripped Origin cannot bypass the check for a
// cross-site Sec-Fetch-Site mutation.
for (const headers of [{ Origin: 'https://attacker.invalid' }, { 'sec-fetch-site': 'cross-site' }]) {
const response = await fetch(`${base}/api/pages`, {
method: 'POST', headers: { 'Content-Type': 'application/json', ...headers }, body: '{"title":"forged"}',
})
assert.equal(response.status, 403, `mutating proxy request survived ${JSON.stringify(headers)}`)
}
// Go rejects non-JSON bodies with 415 (internal/http security_test); the
// fixture mirrors that discipline so the Next proxy's content-type
// passthrough is exercised end to end.
const plain = await fetch(`${base}/api/pages`, { method: 'POST', headers: { Origin: base, 'Content-Type': 'text/plain' }, body: '{"title":"forged"}' })
assert.equal(plain.status, 415)
await fixtureLogin(context)
console.log('PASS: cross-site form posts and forged mutations rejected; fixture login works through NextAuth')
// Review CR-14: an oversized chunked body (no usable Content-Length) must
// be rejected by the proxy's streaming limit — counted while read and
// cancelled at the cap, never fully buffered first.
const chunkedStatus = await new Promise((resolve) => {
const payload = Buffer.alloc(MAX_PROXY_BODY + 1, 0x61)
const u = new URL(`${base}/api/pages`)
const req = http.request({
host: u.hostname, port: u.port, path: u.pathname, method: 'POST',
headers: { 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked' },
}, res => { res.resume(); res.on('end', () => resolve(res.statusCode)) })
req.on('error', () => resolve(-1))
for (let i = 0; i < payload.length; i += 65536) req.write(payload.subarray(i, Math.min(i + 65536, payload.length)))
req.end()
})
assert.equal(chunkedStatus, 413, 'oversized chunked body was not rejected at the proxy')
console.log('PASS: oversized chunked request bodies are rejected while streaming')
const editor = page.locator('[data-page-editor]')
// The WYSIWYG editor is a contenteditable ProseMirror view: select-all +
// insertText is the fill() equivalent.
const setEditorText = async (text) => {
await editor.click()
await page.keyboard.press('Control+a')
await page.keyboard.insertText(text)
}
await page.goto(`${base}/pages?id=a`)
await setEditorText('A edits before switching')
await page.getByRole('link', { name: 'Page B', exact: true }).click()
await eventually(() => page.getByRole('textbox', { name: 'Page title' }).inputValue().then(v => v === 'Page B'), 'Page B not selected')
await setEditorText('B edits')
await eventually(() => pages.get('b').content === 'B edits', 'Page B not saved')
assert.equal(pages.get('a').content, 'A edits before switching')
console.log('PASS: rapid page switching preserves both pages')
delaySaves = 900
maxConcurrentSaves = 0
await setEditorText('First in-flight edit')
await eventually(() => activeSaves === 1, 'Save did not start')
await setEditorText('Newest edit wins')
await eventually(() => pages.get('b').content === 'Newest edit wins' && activeSaves === 0, 'Newer edit did not save')
assert.equal(maxConcurrentSaves, 1)
delaySaves = 0
console.log('PASS: saves are serialized and newer edits win')
failSaves = true
await setEditorText('Recover this failed draft')
await page.getByRole('link', { name: 'Page A', exact: true }).click()
await page.getByText('Could not save page. Your draft is kept in this tab; retry saving.').first().waitFor()
assert.equal(await page.getByRole('textbox', { name: 'Page title' }).inputValue(), 'Page B')
// The sidebar row is a <Link>, so the URL may move to ?id=a even while the
// workspace keeps its unsaved B draft. Recovery lives per page — reload
// where the draft actually is.
await page.goto(`${base}/pages?id=b`)
await eventually(() => editor.textContent().then(value => value === 'Recover this failed draft'), 'Draft not recovered after reload')
failSaves = false
await page.getByRole('button', { name: 'Save now', exact: true }).click()
await eventually(() => pages.get('b').content === 'Recover this failed draft', 'Retry did not persist draft')
console.log('PASS: failed saves block switching, survive reload, and can be retried')
// Cross-session conflict (review DATA-04/TEST-01): after another "device"
// saves a newer version, the 409 pauses autosave in a distinct conflict
// state. Typing must not schedule another save; only the explicit
// overwrite button may replace the remote content.
await page.goto(`${base}/pages?id=a`)
await setEditorText('Local draft before conflict')
await eventually(() => pages.get('a').content === 'Local draft before conflict', 'Draft did not save before the conflict')
pages.get('a').content = 'Remote edit wins'
pages.get('a').version++
await setEditorText('Edit after remote change')
await page.getByText('This page was changed elsewhere', { exact: true }).waitFor()
const attemptsAtConflict = patchAttempts
await setEditorText('Typing must not overwrite')
await new Promise(resolve => setTimeout(resolve, 1200))
assert.equal(patchAttempts, attemptsAtConflict, 'Typing during a conflict scheduled another save')
assert.equal(pages.get('a').content, 'Remote edit wins', 'Conflict silently overwrote the remote content')
await page.getByRole('button', { name: 'Overwrite with my draft', exact: true }).click()
await eventually(() => pages.get('a').content === 'Typing must not overwrite', 'Explicit overwrite did not persist')
console.log('PASS: conflicts pause autosave until an explicit overwrite')
// Recovery after reload (review DATA-03/TEST-01): a failed draft remembers
// the server version it was based on. When another session saved in the
// meantime, recovery enters the conflict state instead of silently
// rebasing the stale draft onto the newer version.
failSaves = true
await page.goto(`${base}/pages?id=b`)
await setEditorText('Draft with an older base')
await page.getByText('Could not save page. Your draft is kept in this tab; retry saving.').first().waitFor()
pages.get('b').content = 'Remote saved while draft failed'
pages.get('b').version++
failSaves = false
await page.reload()
await eventually(() => editor.textContent().then(value => value === 'Draft with an older base'), 'Draft not recovered after reload')
await page.getByText('This page was changed elsewhere', { exact: true }).waitFor()
// Snapshot AFTER recovery: the reload itself lets the departing page flush
// its dirty draft once (the fixture rejects it with a 409 against the newer
// version). What must not happen is any save from the recovered session.
const attemptsAfterRecovery = patchAttempts
await new Promise(resolve => setTimeout(resolve, 1000))
assert.equal(patchAttempts, attemptsAfterRecovery, 'Recovered draft saved without an explicit resolution')
assert.equal(pages.get('b').content, 'Remote saved while draft failed', 'Recovery rebased onto and clobbered the newer version')
await page.getByRole('button', { name: 'Discard my draft and load latest', exact: true }).click()
await eventually(() => editor.textContent().then(value => value === 'Remote saved while draft failed'), 'Discard did not load the remote content')
console.log('PASS: recovered drafts keep their original base and conflict instead of rebasing')
// Unknown-provenance draft (review DATA-06): a legacy plain-string draft
// carries no baseVersion, so recovery cannot prove it predates the remote
// content. It must pause in the review-required conflict state instead of
// saving over whatever the server holds.
await page.goto(`${base}/pages?id=a`)
await eventually(() => page.getByRole('textbox', { name: 'Page title' }).inputValue().then(v => v === 'Page A'), 'Page A did not open')
await page.evaluate(() => sessionStorage.setItem('superbrain:page-draft:a', JSON.stringify('Legacy draft of unknown origin')))
const attemptsBeforeLegacy = patchAttempts
await page.reload()
await eventually(() => editor.textContent().then(value => value === 'Legacy draft of unknown origin'), 'Legacy draft not recovered')
await page.getByText('This page was changed elsewhere', { exact: true }).waitFor()
await new Promise(resolve => setTimeout(resolve, 1000))
assert.equal(patchAttempts, attemptsBeforeLegacy, 'Unknown-base draft saved without review')
assert.equal(pages.get('a').content, 'Typing must not overwrite', 'Unknown-base draft clobbered the remote content')
await page.getByRole('button', { name: 'Discard my draft and load latest', exact: true }).click()
await eventually(() => editor.textContent().then(value => value === 'Typing must not overwrite'), 'Discard did not load the remote content')
console.log('PASS: unknown-base legacy drafts pause for review instead of saving')
// Envelope refresh (review DATA-07): when a later save in the same serial
// loop fails, the stored recovery envelope must carry the newest content
// AND the base advanced by the earlier success — otherwise reload shows a
// spurious conflict for a draft that was already cleanly rebased.
delaySaves = 900
failContent = 'Envelope part two'
await page.goto(`${base}/pages?id=b`)
await setEditorText('Envelope part one')
await eventually(() => activeSaves === 1, 'First envelope save did not start')
await setEditorText('Envelope part two')
await page.getByText('Could not save page. Your draft is kept in this tab; retry saving.').first().waitFor()
assert.equal(pages.get('b').content, 'Envelope part one', 'First save of the loop did not land')
await page.reload()
await eventually(() => editor.textContent().then(value => value === 'Envelope part two'), 'Newest loop content not recovered')
assert.equal(await page.getByText('This page was changed elsewhere', { exact: true }).count(), 0,
'Recovery hit a spurious conflict — the stored base was not refreshed after the successful save')
delaySaves = 0
failContent = null
await page.getByRole('button', { name: 'Save now', exact: true }).click()
await eventually(() => pages.get('b').content === 'Envelope part two', 'Retry did not persist the recovered draft')
console.log('PASS: recovery envelope keeps the latest content and refreshed base')
// Metadata mutations must not rebase a stale draft (review CR-17): a
// rename/pin PATCH response carries the CURRENT content revision but never
// acknowledges the draft's base. Adopting it would let a stale draft
// overwrite newer remote content without any conflict.
await page.goto(`${base}/pages?id=a`)
await setEditorText('Metadata rebase draft')
await eventually(() => pages.get('a').content === 'Metadata rebase draft', 'Draft did not save before the metadata mutation')
pages.get('a').content = 'Remote content stays'
pages.get('a').version++
await page.locator('button[title="Pin to sidebar"]').click()
await eventually(() => pages.get('a').pinned === true, 'Pin metadata patch did not land')
await setEditorText('Stale draft after metadata patch')
await page.getByText('This page was changed elsewhere', { exact: true }).waitFor()
assert.equal(pages.get('a').content, 'Remote content stays', 'Metadata response rebased the stale draft over the remote content')
await page.getByRole('button', { name: 'Discard my draft and load latest', exact: true }).click()
await eventually(() => editor.textContent().then(value => value === 'Remote content stays'), 'Discard did not load the remote content')
console.log('PASS: metadata mutations do not rebase stale content drafts')
await page.goto(`${base}/bookmarks`)
await eventually(() => page.locator('tbody tr').count().then(n => n === 205), 'Collection truncated at 200')
assert.ok(paginatedRequests > 0, 'Cursor not forwarded through proxy')
await page.getByText('Fixture 000', { exact: true }).click()
const sheet = page.getByRole('dialog')
const fillSheet = async () => {
await sheet.getByLabel('URL', { exact: true }).fill('https://127.0.0.1/changed')
await sheet.getByLabel('Description', { exact: true }).fill('Edited description')
await sheet.getByLabel('First tag', { exact: true }).uncheck()
await sheet.getByLabel('Second tag', { exact: true }).check()
await sheet.locator('input[type="datetime-local"]').fill('2026-10-08T10:30')
}
await fillSheet()
// Review CR-04: a concurrent edit elsewhere bumps the revision while this
// form is open. The save must surface a conflict, preserve the unsaved
// draft, and leave the remote state untouched — no silent overwrite.
bookmarks[0].version++
bookmarks[0].notes = 'Remote edit wins'
await sheet.getByRole('button', { name: 'Save Changes', exact: true }).click()
await page.getByText('This bookmark was changed elsewhere', { exact: false }).first().waitFor()
assert.equal(bookmarks[0].url, 'https://127.0.0.1/test', 'Stale bookmark save overwrote the remote state')
assert.equal(bookmarks[0].notes, 'Remote edit wins', 'Stale bookmark save clobbered the remote notes')
// The sheet keeps the unsaved draft and offers an explicit resolution.
await sheet.getByRole('button', { name: 'Load latest version', exact: true }).click()
await eventually(() => sheet.getByLabel('URL', { exact: true }).inputValue().then(v => v === 'https://127.0.0.1/test'), 'Sheet did not reload the latest version')
await fillSheet()
await sheet.getByRole('button', { name: 'Save Changes', exact: true }).click()
await eventually(() => bookmarks[0].url === 'https://127.0.0.1/changed', 'Bookmark update missing after conflict resolution')
assert.equal(bookmarks[0].description, 'Edited description')
assert.deepEqual(bookmarks[0].tags.map(({ tag }) => tag.id), ['two'])
assert.equal(bookmarks[0].alertAt, '2026-10-08T15:30:00.000Z')
assert.equal(bookmarks[0].notes, 'Remote edit wins', 'Conflict resolution lost the remote notes')
assert.deepEqual(errors, [], 'Browser runtime errors')
console.log('PASS: 205 bookmarks displayed; stale saves conflict visibly; URL/description/tags editable; local alert time converted to UTC')
// Kanban drag-and-drop persists within-column ordering and cross-column
// moves through the versioned card endpoint.
await page.goto(`${base}/kanban`)
await page.getByRole('heading', { name: 'Kanban Board' }).waitFor()
const inboxColumn = page.locator('[data-column-id="inbox"]')
const todoColumn = page.locator('[data-column-id="todo"]')
await inboxColumn.locator('[data-bookmark-id="bookmark-2"]').dragTo(
inboxColumn.locator('[data-bookmark-id="bookmark-0"]'),
{ targetPosition: { x: 20, y: 5 } },
)
await eventually(() => kanbanMoves.length === 1, 'Within-column drag did not reach the API')
assert.deepEqual(kanbanMoves[0], { id: 'bookmark-2', columnId: 'inbox', position: 0, version: 0 })
await inboxColumn.locator('[data-bookmark-id="bookmark-3"]').dragTo(todoColumn)
await eventually(() => kanbanMoves.length === 2, 'Cross-column drag did not reach the API')
assert.deepEqual(kanbanMoves[1], { id: 'bookmark-3', columnId: 'todo', position: 0, version: 0 })
await todoColumn.locator('[data-bookmark-id="bookmark-3"]').waitFor()
// Touch/keyboard fallback: the card menu moves without a drag.
await todoColumn.locator('[data-bookmark-id="bookmark-3"]').getByRole('button', { name: /options/i }).click()
await page.getByRole('menuitem', { name: 'Move to Inbox' }).click()
await eventually(() => kanbanMoves.length === 3, 'Card menu move did not reach the API')
assert.deepEqual(kanbanMoves[2], { id: 'bookmark-3', columnId: 'inbox', position: 204, version: 1 })
await inboxColumn.locator('[data-bookmark-id="bookmark-3"]').waitFor()
console.log('PASS: kanban drag reorders within a column and moves cards between columns')
// URLs added to a page become bookmarks (product invariant): the editor's
// Link button and a bare-URL paste each create — or reuse — a bookmark and
// attach it under "Links on this page". The pasted Markdown itself is
// never rewritten without an explicit click.
await page.goto(`${base}/pages?id=a`)
await page.locator('[data-page-editor]').waitFor()
await page.evaluate(() => { window.prompt = () => 'https://pages-url.test/report' })
await page.getByRole('button', { name: 'Link', exact: true }).click()
await eventually(() => bookmarks.some(b => b.url === 'https://pages-url.test/report'), 'Link button did not save the URL as a bookmark')
await eventually(() => pages.get('a').bookmarks.some(x => x.bookmark.url === 'https://pages-url.test/report'), 'Link button bookmark was not attached to the page')
await page.getByText('pages-url.test').first().waitFor()
// Re-adding the same URL attaches nothing new: no duplicate bookmark row,
// no second link.
const urlsBefore = bookmarks.length
await page.evaluate(() => { window.prompt = () => 'https://pages-url.test/report/' })
await page.getByRole('button', { name: 'Link', exact: true }).click()
await new Promise(resolve => setTimeout(resolve, 600))
assert.equal(bookmarks.length, urlsBefore, 'Re-adding the same URL created a duplicate bookmark')
assert.equal(pages.get('a').bookmarks.filter(x => x.bookmark.url.startsWith('https://pages-url.test')).length, 1, 'Duplicate URL attached twice')
// A bare-URL paste reuses the EXISTING library bookmark whose url matches
// (search covers urls server-side) instead of POSTing a new one, and the
// raw text stays in the document.
const pasteTarget = bookmarks.find(b => b.url === 'https://127.0.0.1/changed')
await page.locator('[data-page-editor]').click()
await page.evaluate((url) => {
const dt = new DataTransfer()
dt.setData('text/plain', url)
document.querySelector('[data-page-editor]')
.dispatchEvent(new ClipboardEvent('paste', { clipboardData: dt, bubbles: true, cancelable: true }))
}, 'https://127.0.0.1/changed')
await eventually(() => pages.get('a').bookmarks.some(x => x.bookmark.id === pasteTarget.id), 'Pasted URL did not attach the existing bookmark')
await eventually(() => pages.get('a').content.includes('https://127.0.0.1/changed'), 'Pasted URL text was not inserted and saved')
assert.equal(bookmarks.length, urlsBefore, 'Pasted URL created a duplicate bookmark instead of reusing the existing one')
assert.ok(!pages.get('a').content.includes('[https://127.0.0.1/changed]'), 'Pasted URL was rewritten without an explicit click')
console.log('PASS: URLs added to a page (Link button and paste) become page bookmarks, reusing existing rows')
// The sidebar persists the complete order permutation (PUT /api/pages);
// drag b above a. Reset the pin from the earlier metadata test so both
// pages are in one reorder group.
pages.get('a').pinned = false
await page.goto(`${base}/pages`)
await page.locator('[data-page-id="a"]').waitFor()
await page.locator('[data-page-id="b"] [data-page-drag-handle]').dragTo(
page.locator('[data-page-id="a"]'),
{ targetPosition: { x: 10, y: 5 } },
)
await eventually(() => pages.get('b')?.position === 0 && pages.get('a')?.position === 1, 'Sidebar page drag did not reorder')
// And drag a back above b.
await page.locator('[data-page-id="a"] [data-page-drag-handle]').dragTo(
page.locator('[data-page-id="b"]'),
{ targetPosition: { x: 10, y: 5 } },
)
await eventually(() => pages.get('a')?.position === 0 && pages.get('b')?.position === 1, 'Sidebar page drag did not reorder')
console.log('PASS: sidebar persists drag reordering')
// Settings → WebDAV: masked list rows render; the deployment suggestion
// (NEXTCLOUD_URL) prefills the connect form with the viewer's username;
// save POSTs the form; Sync/Test surface the server's verbatim results.
await page.goto(`${base}/settings`)
await page.getByRole('tab', { name: 'WebDAV' }).click()
await page.getByText('House docs').first().waitFor()
await page.getByText('erwin@https://dav.example.org/remote.php/dav/files/erwin/Documents').first().waitFor()
await page.getByText('Nextcloud files detected on this server').waitFor()
await page.getByRole('button', { name: 'Use', exact: true }).click()
const suggestUrl = page.getByPlaceholder('https://cloud.example.com/remote.php/dav/files/username/Documents')
assert.equal(await suggestUrl.inputValue(), 'https://cloud.federatedcomputer.ai/remote.php/dav/files/')
assert.equal(await page.getByPlaceholder('erwin').inputValue(), 'fixture') // session email local-part
await page.getByPlaceholder('erwin').fill('erwin')
await page.getByPlaceholder('Account or app password').fill('app-secret')
await page.locator('[role="dialog"]').getByRole('button', { name: 'Connect share' }).click()
await eventually(() => webdavConnections.length === 2, 'WebDAV connect POST did not reach the API')
assert.equal(lastWebdavCreate.url, 'https://cloud.federatedcomputer.ai/remote.php/dav/files/')
assert.equal(lastWebdavCreate.username, 'erwin')
assert.equal(lastWebdavCreate.password, 'app-secret')
await page.getByRole('button', { name: 'Sync House docs' }).click()
await page.getByText('3 files: 3 created, 0 reused, 0 changed, 0 detached').first().waitFor()
await page.getByRole('button', { name: 'Test House docs' }).click()
await page.getByText('PROPFIND answered').first().waitFor()
assert.ok(!(await page.evaluate(() => document.body.innerText)).includes('app-secret'), 'the entered password echoed back in the UI')
console.log('PASS: Settings → WebDAV lists masked rows, prefills the Nextcloud suggestion, and round-trips connect/sync/test without echoing secrets')
// Settings → CalDAV: the calendar list renders on each row; the deployment
// suggestion prefills the connect form; Sync/Test surface the verbatim
// server results (per-calendar discovery and event counts).
await page.getByRole('tab', { name: 'CalDAV' }).click()
await page.getByText('My calendars').first().waitFor()
await page.getByText('erwin@https://dav.example.org/remote.php/dav/calendars/erwin').first().waitFor()
await page.getByText('Calendars: Work').waitFor()
await page.getByText('Nextcloud calendars detected on this server').waitFor()
await page.getByRole('button', { name: 'Use', exact: true }).click()
const calSuggestUrl = page.getByPlaceholder('https://cloud.example.com/remote.php/dav/calendars/username')
assert.equal(await calSuggestUrl.inputValue(), 'https://cloud.federatedcomputer.ai/remote.php/dav/calendars/')
assert.equal(await page.getByPlaceholder('erwin').inputValue(), 'fixture')
await page.getByPlaceholder('erwin').fill('erwin')
await page.getByPlaceholder('Account or app password').fill('cal-app-secret')
await page.locator('[role="dialog"]').getByRole('button', { name: 'Connect calendar' }).click()
await eventually(() => caldavConnections.length === 2, 'CalDAV connect POST did not reach the API')
assert.equal(lastCaldavCreate.url, 'https://cloud.federatedcomputer.ai/remote.php/dav/calendars/')
assert.equal(lastCaldavCreate.username, 'erwin')
assert.equal(lastCaldavCreate.password, 'cal-app-secret')
await page.getByRole('button', { name: 'Sync My calendars' }).click()
await page.getByText('4 events across 2 calendars: 4 created, 0 reused, 0 changed, 0 detached').first().waitFor()
await page.getByRole('button', { name: 'Test My calendars' }).click()
await page.getByText('Found: Work, Personal').first().waitFor()
assert.ok(!(await page.evaluate(() => document.body.innerText)).includes('cal-app-secret'), 'the entered CalDAV password echoed back in the UI')
console.log('PASS: Settings → CalDAV lists calendars, prefills the Nextcloud suggestion, and round-trips connect/sync/test without echoing secrets')
// Revocation failure path (review CR-02): when Go's revocation fails, the
// local sign-out still completes, but the UI must say so — local cookie
// removal is not confirmed global revocation, and other sessions survive.
await page.goto(`${base}/bookmarks`)
await page.keyboard.press('Escape') // close any detail sheet so the top bar is reachable
failSignout = true
const failedRevoke = page.waitForResponse(r => r.url().includes('/api/session/revoke') && r.request().method() === 'POST')
await page.getByRole('button', { name: 'Sign out', exact: true }).click()
await page.getByText('global revocation failed', { exact: false }).first().waitFor()
assert.equal((await (await failedRevoke).json()).revoked, false, 'Revoke route reported success despite the backend failure')
await page.waitForURL(`${base}/login`)
failSignout = false
console.log('PASS: failed global revocation is reported and local sign-out still completes')
// Browser sign-out must reach Go's revocation (review AUTH-LIFECYCLE): the
// explicit /api/session/revoke call (with the NextAuth signOut event as
// backstop) posts to Go /api/auth/signout with a signed assertion, so the
// tokenVersion bump kills Go cookie sessions and any previously minted
// assertions — not just the local NextAuth JWT.
await fixtureLogin(context)
await page.goto(`${base}/pages`)
const revoked = page.waitForResponse(r => r.url().includes('/api/session/revoke') && r.request().method() === 'POST')
await page.getByRole('button', { name: 'Sign out', exact: true }).click()
assert.equal((await (await revoked).json()).revoked, true, 'Revoke route did not confirm global revocation')
await page.waitForURL(`${base}/login`)
await eventually(() => signouts.length > 0, 'Go signout was not called during browser sign-out')
const authHeader = signouts[signouts.length - 1]
assert.ok(authHeader.startsWith('Bearer '), 'Signout call carried no bearer assertion')
const claim = JSON.parse(Buffer.from(authHeader.slice('Bearer '.length).split('.')[1], 'base64url').toString())
assert.equal(claim.uid, 'fixture-user')
assert.equal(claim.typ, 'assertion')
assert.equal(claim.ver, 1)
assert.deepEqual(errors, [], 'Browser runtime errors during sign-out')
console.log('PASS: browser sign-out revokes sessions through the Go backend')
} finally {
await browser?.close()
web.kill('SIGTERM')
api.closeAllConnections(); api.close()
attacker.closeAllConnections(); attacker.close()
}