Files
Agent-Workbench/tests/file-boundaries.test.mjs

35 lines
2.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture } from './helpers.mjs';
import { readText, listFiles, proposeFile } from '../src/server/files.mjs';
import { inspectProject } from '../src/core/project-inspector.mjs';
test('hard-link aliases cannot expose protected bytes through reads, proposals, inventory or automatic project rules', async t => {
const f = fixture(t);
fs.mkdirSync(path.join(f.projectRoot, '.git')); fs.mkdirSync(path.join(f.projectRoot, '.secrets'));
fs.writeFileSync(path.join(f.projectRoot, '.git/config'), '[core]\nrepositoryformatversion = 0\n');
fs.writeFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'owned-protected-rule-marker');
fs.linkSync(path.join(f.projectRoot, '.git/config'), path.join(f.projectRoot, 'public-config.txt'));
fs.linkSync(path.join(f.projectRoot, '.secrets/owned.txt'), path.join(f.projectRoot, 'AGENTS.md'));
for (const filename of ['public-config.txt', 'AGENTS.md']) {
assert.throws(() => readText(f.project, filename), /硬链接/);
assert.throws(() => proposeFile(f.store, f.session, { path: filename, content: 'new bytes' }), /硬链接/);
}
const inventory = listFiles(f.project); assert.ok(!inventory.files.some(value => ['public-config.txt', 'AGENTS.md'].includes(value.path)));
const inspection = await inspectProject(f.project); assert.equal(inspection.rules.length, 0);
assert.equal(JSON.stringify(inspection).includes('owned-protected-rule-marker'), false);
assert.equal(fs.readFileSync(path.join(f.projectRoot, '.secrets/owned.txt'), 'utf8'), 'owned-protected-rule-marker');
});
test('ambiguous project hard links are explicit and ordinary independent file copies remain readable', t => {
const f = fixture(t), original = path.join(f.projectRoot, 'README.md'), alias = path.join(f.projectRoot, 'alias.md');
fs.linkSync(original, alias);
assert.throws(() => readText(f.project, 'README.md'), /独立文件副本/); assert.throws(() => readText(f.project, 'alias.md'), /硬链接/);
fs.unlinkSync(alias); fs.copyFileSync(original, alias);
assert.match(readText(f.project, 'README.md').content, /Fixture project/);
assert.match(readText(f.project, 'alias.md').content, /Fixture project/);
assert.ok(listFiles(f.project).files.some(value => value.path === 'alias.md'));
});