@jc_stack/ez-library 0.1.0-beta.16 → 0.1.0-beta.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.dockerignore +2 -0
- package/CHANGELOG.md +21 -0
- package/CONTRIBUTING.md +1 -1
- package/Dockerfile +2 -2
- package/README.md +37 -3
- package/docs/github-sync.md +12 -4
- package/docs/releasing.md +1 -1
- package/ez-plugin.json +2 -1
- package/package.json +3 -2
- package/pnpm-workspace.yaml +7 -0
- package/skills/library/SKILL.md +29 -1
- package/src/cli.mjs +27 -7
- package/src/compress.mjs +47 -0
- package/src/git-mirror.mjs +15 -7
- package/src/git-sync.mjs +46 -19
- package/src/indexer.mjs +3 -3
- package/src/store.mjs +141 -13
- package/src/sync-worker.mjs +4 -3
- package/src/sync.mjs +5 -3
- package/test/compress.test.mjs +80 -0
- package/test/git-mirror.test.mjs +15 -0
- package/test/git-sync.test.mjs +82 -0
- package/test/indexer.test.mjs +16 -0
- package/test/library.test.mjs +31 -1
- package/test/replace.test.mjs +90 -0
- package/test/sync.test.mjs +12 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import * as fs from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { tmpdir } from 'node:os';
|
|
6
|
+
import { Readable } from 'node:stream';
|
|
7
|
+
import { spawnSync } from 'node:child_process';
|
|
8
|
+
import { put, replaceText, operation, hash, MAX_REPLACE_BYTES } from '../src/store.mjs';
|
|
9
|
+
|
|
10
|
+
const initial = '# Day\r\n\r\n## A\r\nstarted 🛰\r\n\r\n## B\r\nstarted\r\n';
|
|
11
|
+
const a = { before: '## A\r\nstarted 🛰\r\n', after: '## A\r\ncompleted 🛰\r\n' };
|
|
12
|
+
const b = { before: '## B\r\nstarted\r\n', after: '## B\r\ncompleted\r\n' };
|
|
13
|
+
async function fixture(t, data = initial) {
|
|
14
|
+
const root = await fs.mkdtemp(path.join(tmpdir(), 'library-replace-'));
|
|
15
|
+
t.after(() => fs.rm(root, { recursive: true, force: true }));
|
|
16
|
+
await put(root, 'runs/day.md', Readable.from([Buffer.from(data)]), { expected: 'new', key: 'initial' });
|
|
17
|
+
return root;
|
|
18
|
+
}
|
|
19
|
+
const read = root => fs.readFile(path.join(root, 'files/runs/day.md'), 'utf8');
|
|
20
|
+
|
|
21
|
+
test('scoped replacements preserve interleaved writers, exact bytes and replay', async t => {
|
|
22
|
+
const root = await fixture(t);
|
|
23
|
+
const first = await replaceText(root, 'runs/day.md', a, { key: 'a' });
|
|
24
|
+
await replaceText(root, 'runs/day.md', b, { key: 'b' });
|
|
25
|
+
const expected = initial.replace(a.before, a.after).replace(b.before, b.after);
|
|
26
|
+
assert.equal(await read(root), expected);
|
|
27
|
+
assert.equal((await operation(root, 'a')).state, 'stored');
|
|
28
|
+
assert.notEqual((await operation(root, 'a')).sha256, first.sha256);
|
|
29
|
+
assert.equal((await replaceText(root, 'runs/day.md', a, { key: 'a' })).replay, true);
|
|
30
|
+
assert.equal(await read(root), expected);
|
|
31
|
+
assert.equal(await fs.readFile(path.join(root, 'history', hash(initial)), 'utf8'), initial);
|
|
32
|
+
assert.equal((await fs.stat(path.join(root, 'files/runs/day.md'))).mode & 0o777, 0o600);
|
|
33
|
+
assert.equal('before' in await operation(root, 'a'), false);
|
|
34
|
+
// A changed owning block does not become a stale overwrite on replay.
|
|
35
|
+
await replaceText(root, 'runs/day.md', { before: a.after, after: a.after + 'new progress\r\n' }, { key: 'a2' });
|
|
36
|
+
assert.equal((await operation(root, 'a')).state, 'stored'); // old completed text remains present
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('same-block conflict, ambiguity and reused keys do not rewrite a file', async t => {
|
|
40
|
+
const root = await fixture(t);
|
|
41
|
+
await replaceText(root, 'runs/day.md', a, { key: 'a' });
|
|
42
|
+
await assert.rejects(replaceText(root, 'runs/day.md', { ...a, after: 'different' }, { key: 'other' }), { code: 'CONFLICT' });
|
|
43
|
+
await assert.rejects(replaceText(root, 'runs/day.md', b, { key: 'a' }), { code: 'KEY_REUSED' });
|
|
44
|
+
const before = await read(root);
|
|
45
|
+
await assert.rejects(replaceText(root, 'runs/day.md', { before: '## ', after: 'new' }, { key: 'ambiguous' }), { code: 'CONFLICT' });
|
|
46
|
+
await assert.rejects(replaceText(root, 'runs/day.md', { before: b.before, after: a.after }, { key: 'duplicate-after' }), { code: 'CONFLICT' });
|
|
47
|
+
assert.equal(await read(root), before);
|
|
48
|
+
await assert.rejects(operation(root, 'ambiguous'), { code: 'ENOENT' });
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('interrupted receipt reconciles against current unrelated edits', async t => {
|
|
52
|
+
const root = await fixture(t);
|
|
53
|
+
await replaceText(root, 'runs/day.md', a, { key: 'a' });
|
|
54
|
+
// Model a durable intent with its destination not yet installed.
|
|
55
|
+
await fs.writeFile(path.join(root, 'files/runs/day.md'), initial.replace(b.before, b.after));
|
|
56
|
+
assert.equal((await operation(root, 'a')).state, 'changed');
|
|
57
|
+
const result = await replaceText(root, 'runs/day.md', a, { key: 'a' });
|
|
58
|
+
assert.equal(result.state, 'stored');
|
|
59
|
+
assert.equal(await read(root), initial.replace(a.before, a.after).replace(b.before, b.after));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('replacement rejects unsafe input, source, contention and size', async t => {
|
|
63
|
+
const root = await fixture(t);
|
|
64
|
+
for (const value of [null, { before: '', after: 'a' }, { before: 'a', after: '' }, { before: 'a', after: 'a' },
|
|
65
|
+
{ ...a, extra: true }, { before: '\ud800', after: 'x' }, { before: 'x', after: 'y'.repeat(MAX_REPLACE_BYTES) }]) {
|
|
66
|
+
await assert.rejects(replaceText(root, 'runs/day.md', value, { key: 'bad' }), { code: 'INVALID' });
|
|
67
|
+
}
|
|
68
|
+
await assert.rejects(replaceText(root, '../outside', a, { key: 'bad' }), { code: 'UNSAFE_PATH' });
|
|
69
|
+
await fs.symlink(path.join(root, 'files/runs/day.md'), path.join(root, 'files/link'));
|
|
70
|
+
await assert.rejects(replaceText(root, 'link', a, { key: 'bad' }), { code: 'UNSAFE_PATH' });
|
|
71
|
+
await fs.mkdir(path.join(root, '.writer-lock'));
|
|
72
|
+
await assert.rejects(replaceText(root, 'runs/day.md', a, { key: 'bad' }), { code: 'BUSY' });
|
|
73
|
+
await fs.rmdir(path.join(root, '.writer-lock'));
|
|
74
|
+
await fs.writeFile(path.join(root, 'files/runs/day.md'), Buffer.from([255]));
|
|
75
|
+
await assert.rejects(replaceText(root, 'runs/day.md', a, { key: 'bad' }), { code: 'INVALID' });
|
|
76
|
+
await fs.truncate(path.join(root, 'files/runs/day.md'), 8 * 1024 * 1024 + 1);
|
|
77
|
+
await assert.rejects(replaceText(root, 'runs/day.md', a, { key: 'bad' }), { code: 'TOO_LARGE' });
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('replace CLI uses bounded JSON, not shell or regex interpretation', async t => {
|
|
81
|
+
const root = await fixture(t);
|
|
82
|
+
const cli = new URL('../bin/ez-library.mjs', import.meta.url).pathname;
|
|
83
|
+
const run = input => spawnSync(process.execPath, [cli, 'replace', '--path', 'runs/day.md', '--key', 'cli'],
|
|
84
|
+
{ input, env: { ...process.env, EZ_LIBRARY_STATE: root }, encoding: 'utf8' });
|
|
85
|
+
const result = run(JSON.stringify(a));
|
|
86
|
+
assert.equal(result.status, 0, result.stderr); assert.equal(JSON.parse(result.stdout).data.state, 'stored');
|
|
87
|
+
assert.equal(run('{').status, 2);
|
|
88
|
+
assert.equal(run(Buffer.from([255])).status, 2);
|
|
89
|
+
assert.equal(run(' '.repeat(MAX_REPLACE_BYTES + 1)).status, 2);
|
|
90
|
+
});
|
package/test/sync.test.mjs
CHANGED
|
@@ -78,6 +78,18 @@ test('sync is inert without binding; policy revisions and remote paths are guard
|
|
|
78
78
|
assert.equal(environment(root).RCLONE_CONFIG, path.join(root, 'sync/rclone.conf'));
|
|
79
79
|
});
|
|
80
80
|
|
|
81
|
+
test('pull-only cannot enable two-way transfers on a non-Git folder binding', async t => {
|
|
82
|
+
const { root, remote } = await fixture(t);
|
|
83
|
+
await fs.mkdir(path.join(root, 'sync'), { recursive: true });
|
|
84
|
+
await fs.writeFile(path.join(root, 'sync/config.json'), JSON.stringify({ schemaVersion: 1, remote, mode: 'two-way', intervalSeconds: 60 }));
|
|
85
|
+
const before = await syncStatus(root);
|
|
86
|
+
await assert.rejects(syncPolicy(root, { expected: before.revision, mode: 'pull-only' }), { code: 'INVALID' });
|
|
87
|
+
assert.equal((await syncStatus(root)).config.mode, 'two-way');
|
|
88
|
+
await fs.writeFile(path.join(root, 'sync/config.json'), JSON.stringify({ ...before.config, mode: 'pull-only' }));
|
|
89
|
+
await assert.rejects(syncRun(root), { code: 'INVALID' });
|
|
90
|
+
assert.equal((await syncStatus(root)).status.state, 'sync-error');
|
|
91
|
+
});
|
|
92
|
+
|
|
81
93
|
test('native adoption preserves a populated folder, empty files and directories; conflicts stop adoption', { skip: !nativeAvailable }, async t => {
|
|
82
94
|
const { root, remote } = await fixture(t);
|
|
83
95
|
await fs.mkdir(path.join(remote, 'empty-dir'));
|