Files
stack/packages/seat/tests/workspace-files.test.mjs

36 lines
2.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, linkSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { workspaceFiles } from '../src/workspace-files.mjs';
test('lists only safe relative names and never follows links', t => {
const root = mkdtempSync(join(tmpdir(), 'files-')); t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, 'dir')); writeFileSync(join(root, 'dir', 'ok'), ''); writeFileSync(join(root, '.secret'), '');
writeFileSync(join(root, 'hard'), ''); linkSync(join(root, 'hard'), join(root, 'hard2'));
symlinkSync('/etc', join(root, 'outside')); symlinkSync(join(root, 'dir'), join(root, 'alias'));
assert.deepEqual(workspaceFiles(root).entries, [{ name: 'dir/ok' }]);
assert.throws(() => workspaceFiles(join(root, 'alias')));
assert.throws(() => workspaceFiles(`${root}/../other`));
});
test('each walk cap reports truncation', t => {
const root = mkdtempSync(join(tmpdir(), 'files-')); t.after(() => rmSync(root, { recursive: true, force: true }));
mkdirSync(join(root, 'a')); mkdirSync(join(root, 'a', 'b')); writeFileSync(join(root, 'a', 'b', 'x'), ''); writeFileSync(join(root, 'y'), '');
for (const options of [{ maxEntries: 0 }, { maxDepth: 0 }, { maxDirectories: 1 }, { maxMs: 0 }]) assert.equal(workspaceFiles(root, options).truncated, true);
});
test('the production limits truncate wide, deep, directory-heavy and slow walks', t => {
const root = mkdtempSync(join(tmpdir(), 'files-caps-')); t.after(() => rmSync(root, { recursive: true, force: true }));
const wide = join(root, 'wide'); mkdirSync(wide);
for (let n = 0; n < 101; n++) writeFileSync(join(wide, `f${n}`), '');
const listed = workspaceFiles(wide); assert.equal(listed.entries.length, 100); assert.equal(listed.truncated, true);
const deep = join(root, 'deep'); mkdirSync(deep); let current = deep;
for (let n = 0; n < 6; n++) { current = join(current, `d${n}`); mkdirSync(current); }
writeFileSync(join(current, 'hidden-by-depth'), ''); assert.equal(workspaceFiles(deep).truncated, true);
const many = join(root, 'many'); mkdirSync(many);
for (let n = 0; n < 1001; n++) mkdirSync(join(many, `d${n}`));
assert.equal(workspaceFiles(many, { maxMs: Infinity }).truncated, true);
let ticks = 0; assert.equal(workspaceFiles(wide, { clock: () => ticks++ * 251 }).truncated, true);
});