Files
goodbuddy/tests/canvas-core.electron.test.ts
T
mesalogo 935a8f56bf feat: add persistent document handling and protect note drafts
Add HTTP PaddleOCR-VL settings and shared document previews for chat and knowledge imports. Preserve attachment originals and parsed assets across drafts, messages and queues; support explicit image OCR, selecting document images into a conversation draft, and restoring queued input for editing.

Protect unsaved note bodies and titles during navigation and asynchronous updates. Match canvas analysis to source revisions, invalidate affected comments, preserve annotations during pagination, and provide a resizable record index with saved width preferences.

Existing OCR settings retain local behavior. Historical attachments without saved originals are not reconstructed. Advanced OCR options depend on the configured service; image sending depends on the effective model and Runtime.

Validation: full typecheck and lint passed; 477 focused tests passed across 12 files; staged whitespace checks passed. The full repository suite was not rerun for this commit.

Release note: Keep note drafts safe while switching workspaces, resize the record index, and revisit saved document attachments with OCR previews and selected images in chat.
2026-09-19 16:51:10 +08:00

76 lines
3.9 KiB
TypeScript

// @vitest-environment node
import { spawn } from 'node:child_process'
import { createRequire } from 'node:module'
import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join, resolve } from 'node:path'
import { createServer } from 'vite'
import react from '@vitejs/plugin-react'
import { expect, it } from 'vitest'
it('preserves annotations when flow removes the active page and renders scoped thumbnails', async () => {
const directory = await mkdtemp(join(tmpdir(), 'goodbuddy-canvas-core-'))
const server = await createServer({
configFile: false, root: resolve('.'), cacheDir: join(directory, 'vite'),
plugins: [react(), { name: 'canvas-regression', configureServer(server) {
server.middlewares.use('/canvas-regression.html', async (_request, response) => {
response.setHeader('Content-Type', 'text/html')
response.end(await server.transformIndexHtml('/canvas-regression.html',
'<html><body><script type="module" src="/tests/support/canvas-core-regression.tsx"></script></body></html>'))
})
} }],
server: { host: '127.0.0.1', port: 0, watch: null },
optimizeDeps: { noDiscovery: true, include: ['fabric', 'quill', 'jspdf', 'react', 'react-dom/client', 'react-i18next'] }
})
try {
await server.listen()
const driver = join(directory, 'driver.cjs')
await writeFile(driver, `
const { app, BrowserWindow } = require('electron');
const fs = require('node:fs');
app.setPath('userData', ${JSON.stringify(join(directory, 'profile'))});
app.whenReady().then(async () => {
const win = new BrowserWindow({ show: false, width: 1200, height: 950,
webPreferences: { sandbox: true, contextIsolation: true, nodeIntegration: false, backgroundThrottling: false } });
await win.loadURL(${JSON.stringify(server.resolvedUrls!.local[0] + 'canvas-regression.html')});
for (let i = 0; i < 400; i++) {
if (await win.webContents.executeJavaScript('typeof window.canvasRegression === "function"')) break;
await new Promise(resolve => setTimeout(resolve, 50));
}
const result = await win.webContents.executeJavaScript('window.canvasRegression()');
fs.writeFileSync(${JSON.stringify(join(directory, 'result.json'))}, JSON.stringify(result));
app.exit(0);
}).catch(error => { console.error(error); app.exit(1); });
`)
const env = { ...process.env }
delete env.ELECTRON_RUN_AS_NODE
const child = spawn(createRequire(import.meta.url)('electron'), [driver], { env, stdio: ['ignore', 'pipe', 'pipe'] })
let output = ''
child.stdout.on('data', (data) => { output += data })
child.stderr.on('data', (data) => { output += data })
const timeout = setTimeout(() => child.kill(), 60000)
try {
const code = await new Promise((resolve, reject) => { child.once('exit', resolve); child.once('error', reject) })
expect(code, output).toBe(0)
} finally { clearTimeout(timeout) }
const result = JSON.parse(await readFile(join(directory, 'result.json'), 'utf8'))
console.info('Canvas Electron regression:', JSON.stringify(result))
expect(result.errors).toEqual([])
expect(result.initialPages).toBe(2)
expect(result.beforeShrink).toBe('2 / 2')
expect(result.afterShrink).toBe('1 / 1')
expect(result.savedPages).toBe(1)
expect(result.savedObjects).toEqual(result.initialObjects)
expect(result.reopenedObjects).toEqual(result.initialObjects)
expect(result.visibleInkAfter).toBe(result.visibleInkBefore)
expect(result.reopenedInk).toBe(result.visibleInkBefore)
expect(result.visibleInkBefore).toBeGreaterThan(0)
expect(result.flowTransform).toBe('translateX(0px)')
expect(result.thumbnailStyles).toEqual(result.editorStyles)
expect(result.thumbnailMatchesEditor).toBe(true)
} finally {
await server.close()
await rm(directory, { recursive: true, force: true })
}
}, 90000)