24 lines
1.5 KiB
TypeScript
24 lines
1.5 KiB
TypeScript
import * as fs from 'node:fs/promises';
|
|
import { join } from 'node:path';
|
|
import { vi } from 'vitest';
|
|
|
|
/** Read-only agent transport backed by fixture files. Mutations still fail. */
|
|
export async function mockAgentFiles() {
|
|
const backend = await import('../../src/lib/server/backends/native.js');
|
|
const { serverDir } = await import('../../src/lib/server/config.js');
|
|
return vi.spyOn(backend, 'nativeRequest').mockImplementation(async (instance, kind, payload = {}) => {
|
|
const path = join(serverDir(instance.server), String(payload.path ?? '.'));
|
|
let result: unknown;
|
|
if (kind === 'files.read') {
|
|
const content = await fs.readFile(path).catch((error: NodeJS.ErrnoException) => { if (error.code === 'ENOENT') return null; throw error; });
|
|
result = { contentBase64: content?.toString('base64') ?? null };
|
|
} else if (kind === 'files.stat') {
|
|
const stat = await fs.stat(path).catch((error: NodeJS.ErrnoException) => { if (error.code === 'ENOENT') return null; throw error; });
|
|
result = stat ? { exists: true, size: stat.size, mtime: stat.mtime.toISOString(), isDirectory: stat.isDirectory(), isFile: stat.isFile() } : null;
|
|
} else if (kind === 'files.list') {
|
|
result = { entries: (await fs.readdir(path, { withFileTypes: true })).map(entry => ({ name: entry.name, isDirectory: entry.isDirectory(), isFile: entry.isFile() })) };
|
|
} else throw new Error(`Unexpected agent command: ${kind}`);
|
|
return result as never;
|
|
});
|
|
}
|