35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
const CORE = fileURLToPath(new URL('../src/core/', import.meta.url));
|
|
const FORBIDDEN =
|
|
/(?:from\s+|import\s*\(\s*|require\s*\(\s*)['"](?:obsidian|fs|path|child_process|node:[^'"]*)(?:\/[^'"]*)?['"]/;
|
|
|
|
function coreFiles(): string[] {
|
|
if (!existsSync(CORE)) return [];
|
|
return readdirSync(CORE, { recursive: true, encoding: 'utf8' })
|
|
.filter((f) => f.endsWith('.ts'))
|
|
.map((f) => join(CORE, f));
|
|
}
|
|
|
|
describe('src/core boundary', () => {
|
|
it('detects forbidden imports', () => {
|
|
expect(FORBIDDEN.test(`import { Plugin } from 'obsidian';`)).toBe(true);
|
|
expect(FORBIDDEN.test(`import { readFile } from "fs/promises";`)).toBe(true);
|
|
expect(FORBIDDEN.test(`import x from 'node:path';`)).toBe(true);
|
|
expect(FORBIDDEN.test(`const cp = require('child_process');`)).toBe(true);
|
|
expect(FORBIDDEN.test(`import { transition } from './record';`)).toBe(false);
|
|
});
|
|
|
|
it('has files to check', () => {
|
|
expect(coreFiles().length).toBeGreaterThan(0);
|
|
});
|
|
|
|
it('imports neither obsidian nor Node modules', () => {
|
|
const offenders = coreFiles().filter((f) => FORBIDDEN.test(readFileSync(f, 'utf8')));
|
|
expect(offenders).toEqual([]);
|
|
});
|
|
});
|