You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
2.7 KiB
66 lines
2.7 KiB
import { afterEach, describe, expect, it } from 'vitest'
|
|
import Database from 'better-sqlite3'
|
|
import { mkdtempSync, rmSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import { StorageService, migrate } from '../../packages/desktop/src/storage'
|
|
import { NoteRepository } from '../../src/main/modules/notes/repository'
|
|
import { migrations } from '../../src/main/migrations'
|
|
|
|
const dirs: string[] = []
|
|
const resources: { dispose(): void }[] = []
|
|
afterEach(() => {
|
|
for (const resource of resources.splice(0)) resource.dispose()
|
|
for (const directory of dirs.splice(0)) rmSync(directory, { recursive: true, force: true })
|
|
})
|
|
|
|
describe('real native SQLite', () => {
|
|
it('rolls back schema and version together when an upgrade fails', () => {
|
|
const db = new Database(':memory:')
|
|
resources.push({ dispose: () => db.close() })
|
|
migrate(db, migrations)
|
|
const notes = new NoteRepository(db)
|
|
notes.add('existing user data')
|
|
expect(() =>
|
|
migrate(db, [
|
|
...migrations,
|
|
{ version: 2, sql: 'CREATE TABLE future (id TEXT)' },
|
|
{ version: 3, sql: 'THIS IS NOT SQL' }
|
|
])
|
|
).toThrow()
|
|
expect(db.pragma('user_version', { simple: true })).toBe(1)
|
|
expect(db.prepare("SELECT name FROM sqlite_master WHERE name = 'future'").all()).toEqual([])
|
|
expect(notes.list()[0]?.text).toBe('existing user data')
|
|
migrate(db, [...migrations, { version: 2, sql: 'CREATE TABLE future (id TEXT)' }])
|
|
expect(db.pragma('user_version', { simple: true })).toBe(2)
|
|
expect(notes.list()[0]?.text).toBe('existing user data')
|
|
})
|
|
|
|
it('refuses database downgrade and invalid migration numbering', () => {
|
|
const db = new Database(':memory:')
|
|
resources.push({ dispose: () => db.close() })
|
|
db.pragma('user_version = 9')
|
|
expect(() => migrate(db, migrations)).toThrow('downgrade refused')
|
|
expect(db.pragma('user_version', { simple: true })).toBe(9)
|
|
expect(() => migrate(db, [{ version: 2, sql: 'CREATE TABLE broken (id TEXT)' }])).toThrow(
|
|
'consecutive'
|
|
)
|
|
})
|
|
|
|
it('persists notes and JSON data across connections and parameterizes text', () => {
|
|
const directory = mkdtempSync(join(tmpdir(), 'workshop-native-'))
|
|
dirs.push(directory)
|
|
const storage = new StorageService(directory, migrations)
|
|
const notes = new NoteRepository(storage.database)
|
|
const note = notes.add("'); DROP TABLE notes; --")
|
|
storage.kv.set({ draft: 'saved' })
|
|
storage.dispose()
|
|
const restored = new StorageService(directory, migrations)
|
|
resources.push(restored)
|
|
const repository = new NoteRepository(restored.database)
|
|
expect(repository.list()).toEqual([note])
|
|
expect(restored.kv.get().draft).toBe('saved')
|
|
repository.remove(note.id)
|
|
expect(repository.list()).toEqual([])
|
|
})
|
|
})
|
|
|