@jc_stack/ez-library 0.1.0-beta.15 → 0.1.0-beta.18

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.
@@ -71,3 +71,19 @@ test('per-library indexing policy controls native QMD embedding with explicit sh
71
71
  } finally { await fs.rm(base, { recursive: true, force: true }); }
72
72
  `], { cwd: new URL('..', import.meta.url), stdio: 'pipe' });
73
73
  });
74
+
75
+ test('scan digests are reused per process but never hide same-size rewrites with a restored mtime', async t => {
76
+ const fs = await import('node:fs/promises'), path = (await import('node:path')).default, { tmpdir } = await import('node:os');
77
+ const { setTimeout } = await import('node:timers/promises'), assert = (await import('node:assert/strict')).default;
78
+ const { scanDigest, hash } = await import('../src/store.mjs');
79
+ const base = await fs.realpath(await fs.mkdtemp(path.join(tmpdir(), 'library-scan-')));
80
+ t.after(() => fs.rm(base, { recursive: true, force: true }));
81
+ const file = path.join(base, 'note.md');
82
+ await fs.writeFile(file, 'first');
83
+ const { mtime } = await fs.stat(file);
84
+ await setTimeout(2100);
85
+ assert.deepEqual(await scanDigest(file), { bytes: 5, sha256: hash('first') });
86
+ assert.deepEqual(await scanDigest(file), { bytes: 5, sha256: hash('first') });
87
+ await fs.writeFile(file, 'other'); await fs.utimes(file, mtime, mtime);
88
+ assert.deepEqual(await scanDigest(file), { bytes: 5, sha256: hash('other') });
89
+ });
@@ -5,7 +5,7 @@ import path from 'node:path';
5
5
  import { tmpdir } from 'node:os';
6
6
  import { Readable } from 'node:stream';
7
7
  import { spawnSync } from 'node:child_process';
8
- import { put, operation, settings, defaults, validateSettings, list, digest, hash } from '../src/store.mjs';
8
+ import { put, operation, settings, defaults, validateSettings, list, digest, readRange, MAX_READ_BYTES, hash } from '../src/store.mjs';
9
9
  import { qmdEnvironment } from '../src/cli.mjs';
10
10
 
11
11
  const cli = new URL('../bin/ez-library.mjs', import.meta.url).pathname;
@@ -29,6 +29,55 @@ test('intake preserves binary bytes, private modes, literal paths and replay acr
29
29
  assert.equal(read.status, 0); assert.deepEqual(read.stdout, data);
30
30
  });
31
31
 
32
+ test('bounded reads reconstruct large binary sources with contiguous hashes and EOF', async t => {
33
+ const root = await fixture(t), data = Buffer.alloc(MAX_READ_BYTES * 2 + 137);
34
+ for (let i = 0; i < data.length; i++) data[i] = i % 256;
35
+ const stored = await save(root, data), file = path.join(root, 'files/notes/test.md');
36
+ const chunks = []; let offset = 0;
37
+ do {
38
+ const range = await readRange(file, { offset: String(offset), length: String(MAX_READ_BYTES), expected: stored.sha256 });
39
+ const chunk = Buffer.from(range.content, range.encoding);
40
+ assert.equal(range.offset, offset); assert.equal(range.length, chunk.length);
41
+ assert.equal(range.bytes, data.length); assert.equal(range.sha256, stored.sha256);
42
+ assert.equal(range.chunkSha256, hash(chunk)); assert.equal(range.nextOffset, offset + chunk.length);
43
+ assert(Buffer.byteLength(JSON.stringify(range)) < 1024 * 1024);
44
+ chunks.push(chunk); offset = range.nextOffset;
45
+ if (range.eof) break;
46
+ } while (true);
47
+ assert.deepEqual(Buffer.concat(chunks), data);
48
+ assert.equal(hash(Buffer.concat(chunks)), stored.sha256);
49
+ const eof = await readRange(file, { offset: String(data.length), length: '1', expected: stored.sha256 });
50
+ assert.equal(eof.content, ''); assert.equal(eof.eof, true); assert.equal(eof.length, 0);
51
+ });
52
+
53
+ test('range contract rejects stale hashes and invalid bounds before emitting bytes', async t => {
54
+ const root = await fixture(t), stored = await save(root, 'one 🛰 two'), file = path.join(root, 'files/notes/test.md');
55
+ const valid = { offset: '0', length: '5', expected: stored.sha256 };
56
+ for (const invalid of [{ offset: undefined }, { length: undefined }, { expected: undefined }, { length: '0' },
57
+ { offset: '-1' }, { offset: '1.5' }, { offset: '9007199254740992' }, { length: String(MAX_READ_BYTES + 1) },
58
+ { offset: '999' }, { expected: 'new' }]) {
59
+ await assert.rejects(readRange(file, { ...valid, ...invalid }), code('INVALID'));
60
+ }
61
+ await fs.writeFile(file, 'changed');
62
+ await assert.rejects(readRange(file, valid), code('CONFLICT'));
63
+ const read = spawnSync(process.execPath, [cli, 'get', '--path', 'notes/test.md', '--offset', '0', '--length', '5', '--expected', stored.sha256],
64
+ { env: { ...process.env, EZ_LIBRARY_STATE: root }, encoding: 'utf8' });
65
+ assert.equal(read.status, 3); assert.equal(read.stdout, ''); assert.match(read.stderr, /CONFLICT/);
66
+ });
67
+
68
+ test('range CLI returns binary-safe JSON and preserves path and raw boundaries', async t => {
69
+ const root = await fixture(t), data = Buffer.from([0, 255, 10, 128, 1]), stored = await save(root, data);
70
+ const args = ['get', '--path', 'notes/test.md', '--offset', '1', '--length', '3', '--expected', stored.sha256];
71
+ const invoke = argv => spawnSync(process.execPath, [cli, ...argv], { env: { ...process.env, EZ_LIBRARY_STATE: root }, encoding: 'utf8' });
72
+ const read = invoke(args); assert.equal(read.status, 0);
73
+ const range = JSON.parse(read.stdout).data;
74
+ assert.equal(range.path, 'notes/test.md'); assert.deepEqual(Buffer.from(range.content, 'base64'), data.subarray(1, 4));
75
+ assert.equal(invoke([...args, '--raw']).status, 2);
76
+ assert.equal(invoke(['get', '--path', '../escape', '--offset', '0', '--length', '1', '--expected', stored.sha256]).status, 2);
77
+ await fs.symlink(path.join(root, 'files/notes/test.md'), path.join(root, 'files/link'));
78
+ assert.equal(invoke(['get', '--path', 'link', '--offset', '0', '--length', '1', '--expected', stored.sha256]).status, 2);
79
+ });
80
+
32
81
  test('replacement requires current revision; preserves history; stale replay never overwrites new content', async t => {
33
82
  const root = await fixture(t); const first = await save(root, 'original');
34
83
  await assert.rejects(save(root, 'bad', { key: 'second' }), code('CONFLICT'));
@@ -59,6 +108,8 @@ test('bounds and interrupted input leave no partial target or active lock', asyn
59
108
  const broken = Readable.from((async function* () { yield 'part'; throw Error('interrupted'); })());
60
109
  await assert.rejects(put(root, 'partial.md', broken, { key: 'broken', expected: 'new' }), /interrupted/);
61
110
  assert.deepEqual(await fs.readdir(path.join(root, 'tmp')), []);
111
+ assert.deepEqual(await fs.readdir(path.join(root, 'files')), ['notes']);
112
+ assert.deepEqual(await fs.readdir(path.join(root, 'files/notes')), []);
62
113
  await assert.rejects(fs.stat(path.join(root, 'files/partial.md')), code('ENOENT'));
63
114
  await assert.rejects(fs.stat(path.join(root, '.writer-lock')), code('ENOENT'));
64
115
  });
@@ -161,3 +212,12 @@ test('PDF extraction rejects traversal, symlinks, directories and missing paths
161
212
  const result = spawnSync(process.execPath, [cli, 'pdf-text'], { env: { ...process.env, EZ_LIBRARY_STATE: root }, encoding: 'utf8' });
162
213
  assert.equal(result.status, 2); assert.match(result.stderr, /Supply --path/);
163
214
  });
215
+
216
+ test('local subcommand help succeeds before configuration or write intake',async t=>{
217
+ const root=await fixture(t);
218
+ for(const command of ['put','get','sources','sync-run']) {
219
+ const result=spawnSync(process.execPath,[cli,command,'--library','absent','--help'],{env:{...process.env,EZ_LIBRARY_STATE:root},input:'must not write',encoding:'utf8'});
220
+ assert.equal(result.status,0,result.stderr);assert.match(result.stdout,/put --path/);
221
+ }
222
+ assert.deepEqual(await fs.readdir(root),[]);
223
+ });
@@ -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
+ });
@@ -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'));