Files
makelore/tests/unit/coding-work-presence.test.ts

74 lines
3.3 KiB
TypeScript

// @vitest-environment node
import { mkdir, mkdtemp, rm, symlink, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, describe, expect, it } from 'vitest';
import { inspectWorkPresence } from '../../electron/coding-runtime/work-presence';
const directories: string[] = [];
async function project() {
const directory = await mkdtemp(join(tmpdir(), 'makelore-work-presence-'));
directories.push(directory);
return directory;
}
afterEach(async () => {
await Promise.all(directories.splice(0).map(directory => rm(directory, { recursive: true, force: true })));
});
describe('local work presence', () => {
it('recognizes a new project with only metadata, knowledge and imported materials', async () => {
const root = await project();
expect(await inspectWorkPresence(root)).toBe('empty');
for (const directory of ['.makelore', '.git', 'knowledge', 'assets', 'node_modules/dependency']) {
await mkdir(join(root, directory), { recursive: true });
}
for (const file of ['.makelore/project.json', '.git/config', 'knowledge/example.html', 'node_modules/dependency/index.js',
'assets/角色.PNG', 'assets/背景.svg', 'README.md', '.DS_Store']) {
await writeFile(join(root, file), 'material');
}
expect(await inspectWorkPresence(root)).toBe('empty');
});
it.each(['index.html', 'public/game.html', 'apps/game/src/App.tsx', 'dist/index.html', 'package.json', 'server.py', 'custom.work', 'knowledge', '.makelore', 'Knowledge/game.html'])(
'retains startup for possible work at %s', async file => {
const root = await project();
const parts = file.split('/');
parts.pop();
await mkdir(join(root, ...parts), { recursive: true });
await writeFile(join(root, file), 'work');
expect(await inspectWorkPresence(root)).toBe('possible');
},
);
it('does not follow directory links or mistake them for an empty project', async () => {
const root = await project();
const outside = await project();
await symlink(outside, join(root, 'linked-work'), process.platform === 'win32' ? 'junction' : 'dir');
expect(await inspectWorkPresence(root)).toBe('unknown');
});
it('does not skip a knowledge symlink as if it were the local material directory', async () => {
const root = await project();
const outside = await project();
await symlink(outside, join(root, 'knowledge'), process.platform === 'win32' ? 'junction' : 'dir');
expect(await inspectWorkPresence(root)).toBe('unknown');
});
it('does not classify unreadable or missing projects as empty', async () => {
const root = await project();
expect(await inspectWorkPresence(join(root, 'missing'))).toBe('unknown');
});
it('bounds deep traversal without hiding potential work', async () => {
const root = await project();
await mkdir(join(root, ...Array<string>(10).fill('nested')), { recursive: true });
expect(await inspectWorkPresence(root)).toBe('unknown');
});
it('bounds wide traversal instead of declaring a partial scan empty', async () => {
const root = await project();
await Promise.all(Array.from({ length: 2_001 }, (_, index) => writeFile(join(root, `${index}.txt`), '')));
expect(await inspectWorkPresence(root)).toBe('unknown');
});
});