diff --git a/src/engine/__tests__/purity.test.ts b/src/engine/__tests__/purity.test.ts new file mode 100644 index 0000000..5881642 --- /dev/null +++ b/src/engine/__tests__/purity.test.ts @@ -0,0 +1,26 @@ +import { readdir, readFile } from 'node:fs/promises'; +import { join } from 'node:path'; +import { describe, expect, it } from 'vitest'; + +const ENGINE_DIR = join(import.meta.dirname, '..'); +const FORBIDDEN = [/from\s+['"]react/, /from\s+['"]react-dom/, /from\s+['"]zustand/]; + +async function engineSourceFiles(): Promise { + const entries = await readdir(ENGINE_DIR, { withFileTypes: true }); + return entries + .filter((e) => e.isFile() && e.name.endsWith('.ts') && !e.name.endsWith('.test.ts')) + .map((e) => join(ENGINE_DIR, e.name)); +} + +describe('engine purity', () => { + it('does not import React, react-dom, or Zustand', async () => { + const files = await engineSourceFiles(); + expect(files.length).toBeGreaterThan(0); + for (const file of files) { + const source = await readFile(file, 'utf8'); + for (const pattern of FORBIDDEN) { + expect(source, `${file} must stay free of ${pattern}`).not.toMatch(pattern); + } + } + }); +});