Files
qinglong/test/back/directory-page.test.cjs
whyour c5458596c8 feat(cron): expose run receipts and modularize execution
Return committed trigger IDs and add CLI wait and trigger-specific logs while
preserving API, CLI, transaction, deduplication and unknown-result semantics.

Move scheduling, lifecycle, settlement, queries and legacy adapters into
back/cron; remove obsolete internal import paths and circular dependencies.

Apply project formatting across the repository, cover CJS/MJS in formatting
hooks, add prettier:check and require formatting checks in AGENTS.md.

Validation: backend build; 1398 backend/frontend tests passed (17 platform
skips); 327 CLI tests passed (6 platform skips); 670 files pass format checks.
2026-11-22 12:24:29 +08:00

152 lines
5.1 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const {
checkDirectoryVersions,
readDirectoryPage,
} = require('../../back/shared/directoryPage');
test('directory cursors survive inserts and deletes and search reaches unloaded folders', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-file-page-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
await fs.mkdir(path.join(root, 'dir'));
await fs.mkdir(path.join(root, '.tmp'));
await fs.writeFile(path.join(root, '.tmp', 'secret'), '');
await fs.symlink(os.tmpdir(), path.join(root, 'outside'));
for (const name of ['a', 'c', 'e', 'dir/needle#&.js', 'dir/needle2.js'])
await fs.writeFile(path.join(root, name), '1');
const read = (
after = '',
search = '',
dir = '',
folders = false,
newest = false,
) =>
readDirectoryPage(root, ['.tmp'], dir, after, search, folders, newest, 2);
const first = await read();
assert.deepEqual(
first.data.map((x) => x.key),
['dir', 'a'],
);
await fs.unlink(path.join(root, 'a'));
await fs.writeFile(path.join(root, 'b'), '2');
const second = await read(first.next);
assert.deepEqual(
second.data.map((x) => x.key),
['b', 'c'],
);
assert.deepEqual(
(await read(second.next)).data.map((x) => x.key),
['e'],
);
assert.deepEqual(
(await read('', '', '', false, true)).data.map((x) => x.key),
['dir', 'e'],
);
assert.deepEqual(
(await read('', '', '', true)).data.map((x) => x.key),
['dir'],
);
assert.deepEqual(
(await read('', 'needle')).data.map((x) => x.key),
['dir/needle#&.js', 'dir/needle2.js'],
);
assert.deepEqual((await read('', 'secret')).data, []);
for (const dir of ['../', '.tmp', 'outside'])
await assert.rejects(read('', '', dir), { status: 403 });
});
test('a sparse cross-directory search resumes after an empty batch', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-file-search-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
for (let i = 0; i < 513; i++)
await fs.writeFile(path.join(root, 'a' + i), '');
await fs.mkdir(path.join(root, 'z'));
await fs.writeFile(path.join(root, 'z', 'needle'), '');
const page = (after) =>
readDirectoryPage(root, [], '', after, 'needle', false, false);
const first = await page('');
assert.deepEqual(first.data, []);
assert.ok(first.next);
assert.deepEqual(
(await page(first.next)).data.map((x) => x.key),
['z/needle'],
);
});
test('a delayed terminal page detects changes on either side of its cursor, including a previously traversed sibling', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-moved-page-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
for (const dir of ['a', 'b']) await fs.mkdir(path.join(root, dir));
for (const key of ['a/one.js', 'b/two.js'])
await fs.writeFile(path.join(root, key), key);
const first = await readDirectoryPage(
root,
[],
'',
'',
'.js',
false,
false,
1,
);
const last = await readDirectoryPage(
root,
[],
'',
first.next,
'.js',
false,
false,
2,
);
assert.equal(last.next, null);
assert.deepEqual(
last.data.map((r) => r.key),
['b/two.js'],
);
const versions = [...first.versions, ...last.versions];
assert.equal(await checkDirectoryVersions(root, [], versions), true);
await fs.rename(path.join(root, 'b/two.js'), path.join(root, 'a/first.js'));
await fs.unlink(path.join(root, 'a/one.js'));
await fs.writeFile(path.join(root, 'b/new.js'), 'new');
assert.equal(await checkDirectoryVersions(root, [], versions), false);
const fresh = await readDirectoryPage(root, [], '', '', '.js', false, false);
assert.deepEqual(
fresh.data.map((r) => r.key),
['a/first.js', 'b/new.js'],
);
assert.equal(await checkDirectoryVersions(root, [], fresh.versions), true);
});
test('deleting and replacing the open directory invalidates its pages and a missing path can be checked again', async (t) => {
const root = await fs.mkdtemp(path.join(os.tmpdir(), 'ql-replaced-page-'));
t.after(() => fs.rm(root, { recursive: true, force: true }));
const dir = path.join(root, 'open');
await fs.mkdir(dir);
const list = () => readDirectoryPage(root, [], 'open', '', '', false, false);
const before = await list();
await fs.rename(dir, path.join(root, 'moved'));
const missing = await list();
assert.deepEqual(missing.data, []);
assert.equal(await checkDirectoryVersions(root, [], before.versions), false);
assert.equal(await checkDirectoryVersions(root, [], missing.versions), true);
await fs.mkdir(dir);
assert.equal(await checkDirectoryVersions(root, [], missing.versions), false);
assert.equal(await checkDirectoryVersions(root, [], before.versions), false);
await fs.symlink(os.tmpdir(), path.join(root, 'external'));
assert.equal(
await checkDirectoryVersions(
root,
[],
[
['external', null],
['../escape', null],
],
),
false,
);
});