mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-07-05 13:47:59 +08:00
Adds a unified, file-backed SQLite test harness under tests/helpers/db/
that provisions a real database per test file using the production
migrations + CUSTOM_SQL_STATEMENTS, wires it through
MockMainDbServiceUtils.setDb() so production code reaches it via
application.get('DbService').getDb() transparently, truncates user
tables on beforeEach, and cleans up on afterAll. Replaces the need for
hand-written CREATE TABLE SQL and inline vi.mock('@application')
overrides in consumer tests.
Companion changes:
- Register @test-helpers/* path alias (electron.vite.config.ts,
tsconfig.node.json paths + include, vitest.config.ts main project
include).
- Relax the global vi.mock stubs for node:fs, node:os, node:path in
tests/main.setup.ts so third-party libraries (drizzle-orm migrator
etc.) can read real files. The six existing tests that depended on
the previous fully-stubbed modules now declare a local
vi.mock('node:fs', ...) via the new createNodeFsMock helper at
tests/helpers/mocks/nodeFsMock.ts.
- Self-tests under tests/helpers/db/__tests__/testDatabase.test.ts
cover PRAGMA state, truncate semantics, FTS5 trigger cascades,
NULL-searchableText handling, application-mock routing, and
replay-array accumulation guards.
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { vi } from 'vitest'
|
|
|
|
/**
|
|
* Factory for `vi.mock('node:fs', ...)` that stubs the sync and promise
|
|
* surface area typical tests need to control (existsSync, readFileSync,
|
|
* mkdirSync, etc.) while preserving every other real export via
|
|
* `vi.importActual`.
|
|
*
|
|
* The global `tests/main.setup.ts` keeps `node:fs` fully real so that
|
|
* third-party libraries (e.g. the Drizzle migrator) can read files
|
|
* normally. Tests that need deterministic fs behaviour declare a local
|
|
* override using this helper:
|
|
*
|
|
* import { createNodeFsMock } from '@test-helpers/mocks/nodeFsMock'
|
|
*
|
|
* vi.mock('node:fs', async () => createNodeFsMock())
|
|
*
|
|
* // ...
|
|
* vi.mocked(fs.existsSync).mockReturnValue(true)
|
|
*/
|
|
export async function createNodeFsMock() {
|
|
const actual = await vi.importActual<typeof import('node:fs')>('node:fs')
|
|
const mocked = {
|
|
...actual,
|
|
existsSync: vi.fn(),
|
|
readFileSync: vi.fn(),
|
|
writeFileSync: vi.fn(),
|
|
mkdirSync: vi.fn(),
|
|
readdirSync: vi.fn(),
|
|
statSync: vi.fn(),
|
|
unlinkSync: vi.fn(),
|
|
rmdirSync: vi.fn(),
|
|
renameSync: vi.fn(),
|
|
createReadStream: vi.fn(),
|
|
createWriteStream: vi.fn(),
|
|
promises: {
|
|
...actual.promises,
|
|
access: vi.fn(),
|
|
readFile: vi.fn(),
|
|
writeFile: vi.fn(),
|
|
mkdir: vi.fn(),
|
|
readdir: vi.fn(),
|
|
stat: vi.fn(),
|
|
unlink: vi.fn(),
|
|
rmdir: vi.fn()
|
|
}
|
|
}
|
|
return { ...mocked, default: mocked }
|
|
}
|