knowpool 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE +21 -0
- package/README.md +101 -2
- package/bin/knowpool.mjs +150 -0
- package/docs/DESIGN.md +111 -0
- package/docs/NAMING.md +65 -0
- package/docs/PROTOCOL.md +146 -0
- package/docs/ROADMAP.md +33 -0
- package/docs/architecture/BUILD.md +36 -0
- package/docs/architecture/README.md +125 -0
- package/docs/architecture/mermaid.json +18 -0
- package/docs/architecture/print.css +58 -0
- package/docs/architecture/print.py +121 -0
- package/docs/architecture/sources/evidence.mmd +13 -0
- package/docs/architecture/sources/export.mmd +11 -0
- package/docs/architecture/sources/review.mmd +12 -0
- package/docs/architecture/sources/sync.mmd +18 -0
- package/docs/architecture/sources/system.mmd +13 -0
- package/docs/architecture/sources/team.mmd +14 -0
- package/docs/architecture/sources/vaults.mmd +14 -0
- package/docs/architecture/svg/evidence.svg +1 -0
- package/docs/architecture/svg/export.svg +1 -0
- package/docs/architecture/svg/review.svg +1 -0
- package/docs/architecture/svg/sync.svg +1 -0
- package/docs/architecture/svg/system.svg +1 -0
- package/docs/architecture/svg/team.svg +1 -0
- package/docs/architecture/svg/vaults.svg +1 -0
- package/docs/design-history/README.md +25 -0
- package/docs/design-history/icons/kbloom.svg +1 -0
- package/docs/design-history/icons/knowary.svg +1 -0
- package/docs/design-history/icons/knowcommit.svg +1 -0
- package/docs/design-history/icons/knowcrate.svg +1 -0
- package/docs/design-history/icons/knowmill.svg +1 -0
- package/docs/design-history/icons/lorekiln.svg +1 -0
- package/docs/design-history/icons/loreparcel.svg +1 -0
- package/docs/design-history/icons/loreyard.svg +1 -0
- package/docs/design-history/icons/zhicang.svg +1 -0
- package/docs/design-history/original-layouts.md +100 -0
- package/examples/demo.mjs +54 -0
- package/examples/review.html +10 -0
- package/package.json +13 -4
- package/skills/knowpool/SKILL.md +16 -0
- package/src/contexts.mjs +44 -0
- package/src/export.mjs +107 -0
- package/src/extract.py +68 -0
- package/src/io.mjs +129 -0
- package/src/notes.mjs +132 -0
- package/src/pr.mjs +32 -0
- package/src/setup.mjs +63 -0
- package/src/sources.mjs +136 -0
- package/src/storage.mjs +87 -0
- package/src/vault.mjs +78 -0
- package/src/versioning.mjs +198 -0
- package/test/adapters.test.mjs +60 -0
- package/test/audit.test.mjs +129 -0
- package/test/fixtures/two-pages.pdf +87 -0
- package/test/release.test.mjs +126 -0
- package/test/support.mjs +56 -0
- package/test/workflow.test.mjs +193 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import * as fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { dir, git, inside, requireThat, withLock, branch } from './io.mjs';
|
|
4
|
+
import { external, openStore } from './storage.mjs';
|
|
5
|
+
import { configFile, idTypes, parseConfig } from './vault.mjs';
|
|
6
|
+
import { manifests, parseManifest, verifiedPath } from './sources.mjs';
|
|
7
|
+
import { checkReferences, parseNote, workingNotes } from './notes.mjs';
|
|
8
|
+
|
|
9
|
+
function role(file) {
|
|
10
|
+
if (file === configFile || file === '.gitignore') return 'config';
|
|
11
|
+
if (['README.md', '.github/CODEOWNERS', '.github/pull_request_template.md',
|
|
12
|
+
'.github/workflows/knowpool-verify.yml'].includes(file)) return 'policy';
|
|
13
|
+
const [folder, name, ...nested] = file.split('/');
|
|
14
|
+
if (!name || nested.length) return null;
|
|
15
|
+
if (folder === 'sources' && name.endsWith('.json') && idTypes.source.test(name.slice(0, -5))) return 'source';
|
|
16
|
+
if (folder === 'knowledge' && name.endsWith('.md') && idTypes.note.test(name.slice(0, -3))) return 'note';
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function changes(vault) {
|
|
21
|
+
return git(vault.root, 'status', '--porcelain=v1', '-z', '--no-renames', '--untracked-files=all')
|
|
22
|
+
.split('\0').filter(Boolean).map(row => ({ state: row.slice(0, 2), file: row.slice(3) }));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function verify(vault, { metadataOnly = false } = {}) {
|
|
26
|
+
if (metadataOnly) {
|
|
27
|
+
for (const row of git(vault.root, 'ls-files', '--stage', '-z').split('\0').filter(Boolean)) {
|
|
28
|
+
const file = row.slice(row.indexOf('\t') + 1);
|
|
29
|
+
requireThat(row.startsWith('100644 ') && role(file), 'HISTORY', 'Unexpected tracked path or mode: ' + file);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const folder of ['knowledge', 'sources']) {
|
|
33
|
+
for (const file of await fs.readdir(await dir(vault.root, folder))) {
|
|
34
|
+
requireThat(role(folder + '/' + file), 'FORMAT', 'Unexpected vault file: ' + folder + '/' + file);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const sources = await manifests(vault);
|
|
38
|
+
const map = new Map(sources.map(m => [m.id, m]));
|
|
39
|
+
if (!metadataOnly) for (const m of sources) await verifiedPath(vault, m);
|
|
40
|
+
const notes = await workingNotes(vault);
|
|
41
|
+
for (const n of notes) checkReferences(n, map);
|
|
42
|
+
return { vault: vault.config.id, sources: sources.length, notes: notes.length, verified: true,
|
|
43
|
+
checks: metadataOnly ? 'metadata' : 'metadata-and-objects' };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function commit(vault, message) {
|
|
47
|
+
requireThat(message.trim(), 'INPUT', 'Supply a commit message');
|
|
48
|
+
const pending = changes(vault);
|
|
49
|
+
const files = pending.filter(item => role(item.file)).map(item => item.file);
|
|
50
|
+
requireThat(!pending.some(item => !role(item.file) && item.state[0] !== '?' && item.state[0] !== ' '),
|
|
51
|
+
'STAGED', 'The index contains other staged paths. Finish or unstage them first');
|
|
52
|
+
if (vault.config.mode === 'team' && branch(vault.root) === 'main') {
|
|
53
|
+
requireThat(files.every(file => role(file) === 'config' || role(file) === 'policy'),
|
|
54
|
+
'BRANCH', 'Submit team content from a contribution branch');
|
|
55
|
+
}
|
|
56
|
+
await verify(vault);
|
|
57
|
+
if (!files.length) return { changed: false };
|
|
58
|
+
git(vault.root, 'add', '-A', '--', ...files);
|
|
59
|
+
git(vault.root, 'commit', '--only', '-m', message, '--', ...files);
|
|
60
|
+
return { changed: true, commit: git(vault.root, 'rev-parse', 'HEAD').trim(), files };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Validate exactly the reachable commits which Git could send, including earlier
|
|
64
|
+
// versions whose notes or sources have since been removed. Reuse identical blobs.
|
|
65
|
+
export function snapshot(vault, rev, cache = new Map()) {
|
|
66
|
+
const sources = new Map();
|
|
67
|
+
const notes = [];
|
|
68
|
+
let sameVault = false;
|
|
69
|
+
const records = git(vault.root, 'ls-tree', '-r', '-z', rev).split('\0').filter(Boolean);
|
|
70
|
+
for (const record of records) {
|
|
71
|
+
const split = record.indexOf('\t');
|
|
72
|
+
const [mode, , oid] = record.slice(0, split).split(' ');
|
|
73
|
+
const file = record.slice(split + 1);
|
|
74
|
+
const kind = role(file);
|
|
75
|
+
requireThat(mode === '100644' && kind, 'HISTORY', 'Unexpected path or mode in ' + rev + ': ' + file);
|
|
76
|
+
if (kind === 'policy' || file === '.gitignore') continue;
|
|
77
|
+
if (!cache.has(oid)) cache.set(oid, git(vault.root, 'cat-file', 'blob', oid));
|
|
78
|
+
const contents = cache.get(oid);
|
|
79
|
+
if (file === configFile) {
|
|
80
|
+
const config = parseConfig(contents);
|
|
81
|
+
sameVault = config.id === vault.config.id && config.mode === vault.config.mode;
|
|
82
|
+
} else if (kind === 'source') {
|
|
83
|
+
const m = parseManifest(contents, path.basename(file, '.json'));
|
|
84
|
+
sources.set(m.id, m);
|
|
85
|
+
} else if (kind === 'note') notes.push(parseNote(contents, path.basename(file, '.md')));
|
|
86
|
+
}
|
|
87
|
+
requireThat(sameVault, 'VAULT', 'History contains a different or unrecognized vault: ' + rev);
|
|
88
|
+
for (const note of notes) checkReferences(note, sources);
|
|
89
|
+
return { sources, notes };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function historySources(vault, head) {
|
|
93
|
+
const all = new Map();
|
|
94
|
+
const cache = new Map();
|
|
95
|
+
for (const rev of git(vault.root, 'rev-list', head).trim().split('\n')) {
|
|
96
|
+
for (const m of snapshot(vault, rev, cache).sources.values()) {
|
|
97
|
+
const earlier = all.get(m.id);
|
|
98
|
+
requireThat(!earlier || earlier.size === m.size, 'HISTORY', 'Conflicting size for ' + m.id);
|
|
99
|
+
all.set(m.id, m);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return [...all.values()];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function clean(vault) {
|
|
106
|
+
const pending = changes(vault).filter(item => item.state !== '??' || role(item.file));
|
|
107
|
+
requireThat(!pending.length, 'DIRTY', 'Commit pending vault changes: ' + pending.map(item => item.file).join(', '));
|
|
108
|
+
requireThat(git(vault.root, 'rev-parse', '--is-shallow-repository').trim() === 'false', 'HISTORY', 'Use a complete clone');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function sameConfig(vault, text) {
|
|
112
|
+
const other = parseConfig(text);
|
|
113
|
+
requireThat(other.id === vault.config.id && other.mode === vault.config.mode, 'VAULT', 'Destination belongs to a different vault');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function remoteUrl(vault, name, push) {
|
|
117
|
+
requireThat(/^[a-zA-Z0-9][\w./-]*$/.test(name) && git(vault.root, 'remote').trim().split('\n').includes(name),
|
|
118
|
+
'REMOTE', 'Choose a configured Git remote');
|
|
119
|
+
const urls = git(vault.root, 'remote', 'get-url', ...(push ? ['--push'] : []), '--all', name).trim().split('\n');
|
|
120
|
+
requireThat(urls.length === 1 && !urls[0].startsWith('-') && !urls[0].includes('::'), 'REMOTE', 'Expected a single ordinary Git endpoint');
|
|
121
|
+
return urls[0];
|
|
122
|
+
}
|
|
123
|
+
function fetchTip(vault, url, ref, expected) {
|
|
124
|
+
git(vault.root, 'fetch', '--no-tags', '--', url, ref);
|
|
125
|
+
const actual = git(vault.root, 'rev-parse', 'FETCH_HEAD').trim();
|
|
126
|
+
requireThat(!expected || actual === expected, 'REMOTE_CHANGED', 'Remote changed during verification. Retry sync.');
|
|
127
|
+
sameConfig(vault, git(vault.root, 'show', actual + ':' + configFile));
|
|
128
|
+
return actual;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function sync(vault, { gitDir, remote, storeDir, cloud }) {
|
|
132
|
+
requireThat(Boolean(gitDir) !== Boolean(remote), 'USAGE', 'Select a Git target using --git-dir or --remote');
|
|
133
|
+
clean(vault);
|
|
134
|
+
const target = branch(vault.root);
|
|
135
|
+
const head = git(vault.root, 'rev-parse', 'HEAD').trim();
|
|
136
|
+
const sources = historySources(vault, head);
|
|
137
|
+
if (vault.config.mode === 'team' && target === vault.config.defaultBranch)
|
|
138
|
+
requireThat(!sources.length, 'BRANCH', 'Push team contributions to their own branch');
|
|
139
|
+
|
|
140
|
+
async function publish(url, bare) {
|
|
141
|
+
const listing = git(vault.root, 'ls-remote', '--refs', '--', url).trim();
|
|
142
|
+
const tips = new Map(listing ? listing.split('\n').map(line => line.split('\t').reverse()) : []);
|
|
143
|
+
const mainRef = 'refs/heads/' + vault.config.defaultBranch;
|
|
144
|
+
const ref = 'refs/heads/' + target;
|
|
145
|
+
if (!tips.size) requireThat(ref === mainRef, 'BRANCH', 'Bootstrap the empty repository from the default branch first');
|
|
146
|
+
else {
|
|
147
|
+
requireThat(tips.has(mainRef), 'VAULT', 'Destination has no recognized default branch');
|
|
148
|
+
if (bare) for (const tip of new Set(tips.values())) sameConfig(vault, git(bare, 'show', tip + ':' + configFile));
|
|
149
|
+
for (const r of new Set([mainRef, ref])) {
|
|
150
|
+
if (tips.has(r)) fetchTip(vault, url, r, tips.get(r));
|
|
151
|
+
}
|
|
152
|
+
// Reject divergent work before uploading its evidence. Git also rechecks at push time.
|
|
153
|
+
if (tips.has(ref)) {
|
|
154
|
+
try { git(vault.root, 'merge-base', '--is-ancestor', tips.get(ref), head); }
|
|
155
|
+
catch { requireThat(false, 'TOOL', 'The remote branch has diverged. Fetch its changes before retrying.'); }
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const store = await openStore(vault, { storeDir, cloud }, true);
|
|
159
|
+
if (bare && store.root) requireThat(!inside(bare, store.root) && !inside(store.root, bare),
|
|
160
|
+
'PATH', 'Keep Git and the object store separate');
|
|
161
|
+
for (const m of sources) await store.push(m);
|
|
162
|
+
git(vault.root, '-c', 'push.followTags=false', 'push', '--no-follow-tags', '--', url, head + ':' + ref);
|
|
163
|
+
if (bare && !tips.size) git(bare, 'symbolic-ref', 'HEAD', ref);
|
|
164
|
+
const networkGit = /^(?:https?|ssh|git):\/\//i.test(url) || /^[\w.-]+@[\w.-]+:/.test(url);
|
|
165
|
+
return { state: store.type === 'drive' && networkGit ? 'synced' : 'synced-local', backend: store.type,
|
|
166
|
+
vault: vault.config.id, branch: target, commit: head, verifiedObjects: sources.length };
|
|
167
|
+
}
|
|
168
|
+
if (remote) return publish(remoteUrl(vault, remote, true));
|
|
169
|
+
const bare = await external(vault, gitDir);
|
|
170
|
+
requireThat(git(bare, 'rev-parse', '--is-bare-repository').trim() === 'true'
|
|
171
|
+
&& await fs.realpath(git(bare, 'rev-parse', '--absolute-git-dir').trim()) === bare, 'GIT', 'Expected a bare repository root');
|
|
172
|
+
return withLock(path.join(bare, 'knowpool-sync.lock'), () => publish(bare, bare));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function restore(vault, options) {
|
|
176
|
+
const store = await openStore(vault, options);
|
|
177
|
+
for (const m of await manifests(vault)) await store.pull(m);
|
|
178
|
+
return verify(vault);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export async function pull(vault, { remote, storeDir, cloud }) {
|
|
182
|
+
clean(vault);
|
|
183
|
+
const head = git(vault.root, 'rev-parse', 'HEAD').trim();
|
|
184
|
+
const target = branch(vault.root);
|
|
185
|
+
const tip = fetchTip(vault, remoteUrl(vault, remote, false), 'refs/heads/' + target);
|
|
186
|
+
if (tip !== head) {
|
|
187
|
+
const base = git(vault.root, 'merge-base', head, tip).trim();
|
|
188
|
+
requireThat(base === head || base === tip, 'DIVERGED', 'Fetch the other change and resolve the branch divergence');
|
|
189
|
+
if (base === tip) return { state: 'ahead', commit: head, remoteCommit: tip };
|
|
190
|
+
}
|
|
191
|
+
historySources(vault, tip);
|
|
192
|
+
const store = await openStore(vault, { storeDir, cloud });
|
|
193
|
+
for (const m of snapshot(vault, tip).sources.values()) await store.pull(m);
|
|
194
|
+
requireThat(branch(vault.root) === target && git(vault.root, 'rev-parse', 'HEAD').trim() === head,
|
|
195
|
+
'STALE', 'Local branch moved during the transfer');
|
|
196
|
+
git(vault.root, 'merge', '--ff-only', tip);
|
|
197
|
+
return { state: 'restored', commit: tip, ...await verify(vault) };
|
|
198
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { readFileSync } from 'node:fs';
|
|
6
|
+
import path from 'node:path';
|
|
7
|
+
import { cli, context, env } from './support.mjs';
|
|
8
|
+
|
|
9
|
+
const pdfTool = process.env.KNOWPOOL_PDFTOTEXT || 'pdftotext';
|
|
10
|
+
const hasPdfTool = spawnSync(pdfTool, ['-v']).status === 0;
|
|
11
|
+
test('PDF anchors use physical pages and export preserves the original', { skip: !hasPdfTool }, t => {
|
|
12
|
+
const c = context(t);
|
|
13
|
+
const fixture = fileURLToPath(new URL('./fixtures/two-pages.pdf', import.meta.url));
|
|
14
|
+
const pdf = c.ok('source', 'add', fixture);
|
|
15
|
+
assert.equal(pdf.kind, 'pdf');
|
|
16
|
+
const evidence = c.ok('source', 'read', pdf.id, '--at', 'page:2');
|
|
17
|
+
assert.match(evidence.text, /32 sample recordings/);
|
|
18
|
+
assert.doesNotMatch(evidence.text, /background/);
|
|
19
|
+
assert(c.fail('source', 'read', pdf.id, '--at', 'page:8'));
|
|
20
|
+
const draft = c.ok('draft', 'create', '--title', 'PDF evidence', '--body-file', c.file('proposed.md', '32 recordings'),
|
|
21
|
+
'--source', pdf.id, '--at', 'page:2');
|
|
22
|
+
c.ok('approve', draft.draft, '--digest', c.ok('draft', 'show', draft.draft).digest);
|
|
23
|
+
c.ok('commit', '--message', 'record PDF evidence');
|
|
24
|
+
c.ok(...c.sync);
|
|
25
|
+
const clone = path.join(c.root, 'pdf-clone');
|
|
26
|
+
c.git('clone', c.bare, clone);
|
|
27
|
+
c.at(clone, 'restore', '--store', c.store);
|
|
28
|
+
const ref = c.at(clone, 'search', 'recordings').matches[0].sources[0];
|
|
29
|
+
assert.equal(c.at(clone, 'source', 'read', ref.id, '--at', ref.at).text, evidence.text);
|
|
30
|
+
const out = path.join(c.root, 'human-readable.pdf');
|
|
31
|
+
c.at(clone, 'source', 'get', pdf.id, '--out', out);
|
|
32
|
+
assert.deepEqual(readFileSync(fixture), readFileSync(out));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('online SQLite backup includes committed rows while the WAL is still open', t => {
|
|
36
|
+
const c = context(t);
|
|
37
|
+
const script = `
|
|
38
|
+
import json, pathlib, sqlite3, subprocess, sys
|
|
39
|
+
node, cli, vault, root = sys.argv[1:]
|
|
40
|
+
db = pathlib.Path(root, "live.db")
|
|
41
|
+
conn = sqlite3.connect(db)
|
|
42
|
+
conn.execute("pragma journal_mode=wal")
|
|
43
|
+
conn.execute("pragma wal_autocheckpoint=0")
|
|
44
|
+
conn.execute("create table facts(value)")
|
|
45
|
+
conn.execute("insert into facts values ('still-in-wal')")
|
|
46
|
+
conn.commit()
|
|
47
|
+
assert pathlib.Path(str(db) + "-wal").stat().st_size > 0
|
|
48
|
+
args = [node, cli, "--vault", vault]
|
|
49
|
+
s = json.loads(subprocess.check_output(args + ["source", "add", str(db)]))
|
|
50
|
+
assert s["kind"] == "sqlite"
|
|
51
|
+
out = pathlib.Path(root, "snapshot.sqlite")
|
|
52
|
+
subprocess.check_call(args + ["source", "get", s["id"], "--out", str(out)], stdout=subprocess.DEVNULL)
|
|
53
|
+
with sqlite3.connect(out.as_uri() + "?mode=ro", uri=True) as restored:
|
|
54
|
+
assert restored.execute("select value from facts").fetchone() == ("still-in-wal",)
|
|
55
|
+
conn.close()
|
|
56
|
+
`;
|
|
57
|
+
const python = process.env.KNOWPOOL_PYTHON || 'python3';
|
|
58
|
+
const p = spawnSync(python, ['-c', script, process.execPath, cli, c.vault, c.root], { encoding: 'utf8', env });
|
|
59
|
+
assert.equal(p.status, 0, p.stderr);
|
|
60
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { spawnSync } from 'node:child_process';
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, realpathSync, rmSync, writeFileSync } from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { cli, context, env, propose } from './support.mjs';
|
|
7
|
+
|
|
8
|
+
test('setup resolves a relative clone source from the caller and can reopen it', t => {
|
|
9
|
+
const c = context(t);
|
|
10
|
+
c.ok('commit', '--message', 'bootstrap');
|
|
11
|
+
c.ok(...c.sync);
|
|
12
|
+
const reader = path.join('readers', 'fresh');
|
|
13
|
+
for (const url of ['./remote.git', 'remote.git', c.bare]) {
|
|
14
|
+
const p = spawnSync(process.execPath, [cli, 'setup', '--clone', url, '--vault', reader],
|
|
15
|
+
{ cwd: c.root, env, encoding: 'utf8' });
|
|
16
|
+
assert.equal(p.status, 0, p.stderr);
|
|
17
|
+
assert.equal(JSON.parse(p.stdout).vault, c.id);
|
|
18
|
+
}
|
|
19
|
+
assert(existsSync(path.join(c.root, reader, '.agents/skills/knowpool/SKILL.md')));
|
|
20
|
+
assert.equal(c.git('-C', reader, 'remote', 'get-url', 'origin'), realpathSync(c.bare));
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test('export preserves extensions and citation links for long Chinese filenames and colliding titles', t => {
|
|
24
|
+
const c = context(t);
|
|
25
|
+
const filename = '资'.repeat(75) + '.txt';
|
|
26
|
+
const title = '晨'.repeat(100);
|
|
27
|
+
for (const folder of ['first', 'second']) {
|
|
28
|
+
mkdirSync(path.join(c.root, folder));
|
|
29
|
+
const src = c.ok('source', 'add', c.file(path.join(folder, filename), folder + '\n'));
|
|
30
|
+
const draft = c.ok('draft', 'create', '--title', title, '--body-file', c.file('body.md', folder),
|
|
31
|
+
'--source', src.id, '--at', 'line:1');
|
|
32
|
+
c.ok('approve', draft.draft, '--digest', draft.digest);
|
|
33
|
+
}
|
|
34
|
+
const out = path.join(c.root, 'export');
|
|
35
|
+
c.ok('export', '--out', out);
|
|
36
|
+
const notes = readdirSync(path.join(out, 'notes'));
|
|
37
|
+
const originals = readdirSync(path.join(out, 'originals'));
|
|
38
|
+
assert.equal(notes.length, 2);
|
|
39
|
+
assert.equal(originals.length, 2);
|
|
40
|
+
for (const name of originals) assert.equal(path.extname(name), '.txt');
|
|
41
|
+
const linkedText = [];
|
|
42
|
+
for (const name of notes) {
|
|
43
|
+
assert.equal(path.extname(name), '.md');
|
|
44
|
+
assert(Buffer.byteLength(name) <= 255);
|
|
45
|
+
const md = readFileSync(path.join(out, 'notes', name), 'utf8');
|
|
46
|
+
const link = /\[原件\]\(([^)]+)\)/.exec(md)[1];
|
|
47
|
+
linkedText.push(readFileSync(path.resolve(out, 'notes', decodeURIComponent(link)), 'utf8').trim());
|
|
48
|
+
}
|
|
49
|
+
assert.deepEqual(linkedText.sort(), ['first', 'second']);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('team search citations resolve from local main even in an older contribution branch', t => {
|
|
53
|
+
const c = context(t, 'team');
|
|
54
|
+
c.ok('commit', '--message', 'bootstrap');
|
|
55
|
+
c.git('-C', c.vault, 'branch', 'knowledge/older');
|
|
56
|
+
c.git('-C', c.vault, 'switch', '-c', 'knowledge/evidence');
|
|
57
|
+
const data = 'primary source\nquoted passage\n';
|
|
58
|
+
const src = c.ok('source', 'add', c.file('review.txt', data));
|
|
59
|
+
const draft = propose(c, src);
|
|
60
|
+
c.ok('approve', draft.draft, '--digest', draft.digest);
|
|
61
|
+
c.ok('commit', '--message', 'review evidence');
|
|
62
|
+
c.git('-C', c.vault, 'switch', 'main');
|
|
63
|
+
c.git('-C', c.vault, 'merge', '--ff-only', 'knowledge/evidence');
|
|
64
|
+
c.git('-C', c.vault, 'switch', 'knowledge/older');
|
|
65
|
+
const hit = c.ok('search', '语音').matches[0];
|
|
66
|
+
assert.equal(hit.id, draft.note);
|
|
67
|
+
const [ref] = c.ok('show', hit.id).note.sources;
|
|
68
|
+
assert.equal(c.ok('source', 'read', ref.id, '--at', ref.at).text, 'quoted passage');
|
|
69
|
+
const out = path.join(c.root, 'retrieved.txt');
|
|
70
|
+
c.ok('source', 'get', ref.id, '--out', out);
|
|
71
|
+
assert.equal(readFileSync(out, 'utf8'), data);
|
|
72
|
+
assert.equal(c.fail('draft', 'create', '--title', 'old branch', '--body-file', c.file('draft.md', 'body'),
|
|
73
|
+
'--source', ref.id, '--at', ref.at), 'FILE');
|
|
74
|
+
// This branch assigns a different filename and adapter to the same bytes.
|
|
75
|
+
const local = c.ok('source', 'add', c.file('copy.bin', data));
|
|
76
|
+
assert.equal(local.id, ref.id);
|
|
77
|
+
assert.equal(c.ok('source', 'read', ref.id, '--at', ref.at).filename, 'review.txt');
|
|
78
|
+
const fresh = c.ok('source', 'add', c.file('branch.txt', 'branch-only'));
|
|
79
|
+
assert.equal(c.ok('source', 'read', fresh.id, '--at', 'line:1').text, 'branch-only');
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('doctor reports the GitHub CLI used by the team PR workflow', t => {
|
|
83
|
+
const c = context(t);
|
|
84
|
+
const p = spawnSync('gh', ['--version'], { env, encoding: 'utf8' });
|
|
85
|
+
const expected = p.status === 0 ? p.stdout.split('\n')[0].trim() : null;
|
|
86
|
+
assert.equal(c.ok('doctor').gh, expected);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('a partial pull can retry after a missing object and a divergent pull preserves both heads', t => {
|
|
90
|
+
const c = context(t);
|
|
91
|
+
c.ok('commit', '--message', 'bootstrap');
|
|
92
|
+
c.ok(...c.sync);
|
|
93
|
+
const reader = path.join(c.root, 'reader');
|
|
94
|
+
c.at(reader, 'setup', '--clone', c.bare);
|
|
95
|
+
const before = c.git('-C', reader, 'rev-parse', 'HEAD');
|
|
96
|
+
const added = ['one', 'two'].map(name => c.ok('source', 'add', c.file(name + '.txt', name)));
|
|
97
|
+
const commit = c.ok('commit', '--message', 'two sources').commit;
|
|
98
|
+
c.ok(...c.sync);
|
|
99
|
+
const sorted = added.sort((a, b) => a.id.localeCompare(b.id));
|
|
100
|
+
const object = (base, m) => path.join(base, 'objects', 'sha256', m.sha256.slice(0, 2), m.sha256);
|
|
101
|
+
const cloudObject = object(path.join(c.store, c.id), sorted[1]);
|
|
102
|
+
rmSync(cloudObject);
|
|
103
|
+
const pull = ['pull', '--remote', 'origin', '--store', c.store];
|
|
104
|
+
assert.equal(c.invoke(reader, ...pull).status, 1);
|
|
105
|
+
assert.equal(c.git('-C', reader, 'rev-parse', 'HEAD'), before);
|
|
106
|
+
assert(existsSync(object(path.join(reader, '.knowpool'), sorted[0])));
|
|
107
|
+
writeFileSync(cloudObject, readFileSync(path.join(c.root, sorted[1].filename)));
|
|
108
|
+
assert.equal(c.at(reader, ...pull).commit, commit);
|
|
109
|
+
assert.equal(c.at(reader, 'verify').sources, 2);
|
|
110
|
+
c.at(reader, 'source', 'add', c.file('reader.txt', 'reader contribution'));
|
|
111
|
+
const local = c.at(reader, 'commit', '--message', 'reader change').commit;
|
|
112
|
+
c.ok('source', 'add', c.file('author.txt', 'author contribution'));
|
|
113
|
+
const remote = c.ok('commit', '--message', 'author change').commit;
|
|
114
|
+
c.ok(...c.sync);
|
|
115
|
+
assert.equal(JSON.parse(c.invoke(reader, ...pull).stderr).error, 'DIVERGED');
|
|
116
|
+
assert.equal(c.git('-C', reader, 'rev-parse', 'HEAD'), local);
|
|
117
|
+
assert.equal(c.git('-C', c.bare, 'rev-parse', 'refs/heads/main'), remote);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test('the published package includes the PDF source and tests linked from the architecture atlas', () => {
|
|
121
|
+
const root = path.dirname(path.dirname(cli));
|
|
122
|
+
const pack = spawnSync('npm', ['pack', '--dry-run', '--ignore-scripts', '--json'],
|
|
123
|
+
{ cwd: root, env, encoding: 'utf8' });
|
|
124
|
+
assert.equal(pack.status, 0, pack.stderr);
|
|
125
|
+
const [result] = JSON.parse(pack.stdout);
|
|
126
|
+
const files = new Set(result.files.map(file => file.path));
|
|
127
|
+
for (const file of ['test/fixtures/two-pages.pdf', 'test/workflow.test.mjs', 'test/release.test.mjs'])
|
|
128
|
+
assert(files.has(file), 'Missing linked file ' + file);
|
|
129
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
%PDF-1.3
|
|
2
|
+
%���� ReportLab Generated PDF document (opensource)
|
|
3
|
+
1 0 obj
|
|
4
|
+
<<
|
|
5
|
+
/F1 2 0 R
|
|
6
|
+
>>
|
|
7
|
+
endobj
|
|
8
|
+
2 0 obj
|
|
9
|
+
<<
|
|
10
|
+
/BaseFont /Helvetica /Encoding /WinAnsiEncoding /Name /F1 /Subtype /Type1 /Type /Font
|
|
11
|
+
>>
|
|
12
|
+
endobj
|
|
13
|
+
3 0 obj
|
|
14
|
+
<<
|
|
15
|
+
/Contents 8 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 7 0 R /Resources <<
|
|
16
|
+
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
|
|
17
|
+
>> /Rotate 0 /Trans <<
|
|
18
|
+
|
|
19
|
+
>>
|
|
20
|
+
/Type /Page
|
|
21
|
+
>>
|
|
22
|
+
endobj
|
|
23
|
+
4 0 obj
|
|
24
|
+
<<
|
|
25
|
+
/Contents 9 0 R /MediaBox [ 0 0 595.2756 841.8898 ] /Parent 7 0 R /Resources <<
|
|
26
|
+
/Font 1 0 R /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ]
|
|
27
|
+
>> /Rotate 0 /Trans <<
|
|
28
|
+
|
|
29
|
+
>>
|
|
30
|
+
/Type /Page
|
|
31
|
+
>>
|
|
32
|
+
endobj
|
|
33
|
+
5 0 obj
|
|
34
|
+
<<
|
|
35
|
+
/PageMode /UseNone /Pages 7 0 R /Type /Catalog
|
|
36
|
+
>>
|
|
37
|
+
endobj
|
|
38
|
+
6 0 obj
|
|
39
|
+
<<
|
|
40
|
+
/Author (anonymous) /CreationDate (D:20000101000000+00'00') /Creator (anonymous) /Keywords () /ModDate (D:20000101000000+00'00') /Producer (ReportLab PDF Library - \(opensource\))
|
|
41
|
+
/Subject (unspecified) /Title (untitled) /Trapped /False
|
|
42
|
+
>>
|
|
43
|
+
endobj
|
|
44
|
+
7 0 obj
|
|
45
|
+
<<
|
|
46
|
+
/Count 2 /Kids [ 3 0 R 4 0 R ] /Type /Pages
|
|
47
|
+
>>
|
|
48
|
+
endobj
|
|
49
|
+
8 0 obj
|
|
50
|
+
<<
|
|
51
|
+
/Filter [ /ASCII85Decode /FlateDecode ] /Length 116
|
|
52
|
+
>>
|
|
53
|
+
stream
|
|
54
|
+
GapQh0E=F,0U\H3T\pNYT^QKk?tc>IP,;W#U1^23ihPEM_?CW4KISi<![7`#OB_qu\'1T+&qtqUoMd>pUr;?\oV293]G*M'Kodj>&1ru-"YKd"[22p~>endstream
|
|
55
|
+
endobj
|
|
56
|
+
9 0 obj
|
|
57
|
+
<<
|
|
58
|
+
/Filter [ /ASCII85Decode /FlateDecode ] /Length 129
|
|
59
|
+
>>
|
|
60
|
+
stream
|
|
61
|
+
GapQh0E=F,0U\H3T\pNYT^QKk?tc>IP,;W#U1^23ihPEM_?CW4KISi<![7`#OB_sKi7-ic]G-'O`:R"Z"_/d2il'W(S5b1dTQ!b#AmTegSu+hFQ9pBCa9Vr%!($i-ir~>endstream
|
|
62
|
+
endobj
|
|
63
|
+
xref
|
|
64
|
+
0 10
|
|
65
|
+
0000000000 65535 f
|
|
66
|
+
0000000061 00000 n
|
|
67
|
+
0000000092 00000 n
|
|
68
|
+
0000000199 00000 n
|
|
69
|
+
0000000402 00000 n
|
|
70
|
+
0000000605 00000 n
|
|
71
|
+
0000000673 00000 n
|
|
72
|
+
0000000934 00000 n
|
|
73
|
+
0000000999 00000 n
|
|
74
|
+
0000001205 00000 n
|
|
75
|
+
trailer
|
|
76
|
+
<<
|
|
77
|
+
/ID
|
|
78
|
+
[<1c178198fbdfa51b25995d89d4102043><1c178198fbdfa51b25995d89d4102043>]
|
|
79
|
+
% ReportLab generated PDF document -- digest (opensource)
|
|
80
|
+
|
|
81
|
+
/Info 6 0 R
|
|
82
|
+
/Root 5 0 R
|
|
83
|
+
/Size 10
|
|
84
|
+
>>
|
|
85
|
+
startxref
|
|
86
|
+
1424
|
|
87
|
+
%%EOF
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import test from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import os from 'node:os';
|
|
5
|
+
import { mkdtempSync, mkdirSync, existsSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import { context, propose, cli, env } from './support.mjs';
|
|
8
|
+
|
|
9
|
+
test('portable export links notes to originals, disambiguates filenames and preserves existing files', t => {
|
|
10
|
+
const c = context(t);
|
|
11
|
+
const sources = [];
|
|
12
|
+
for (const prefix of ['first', 'second']) {
|
|
13
|
+
mkdirSync(path.join(c.root, prefix));
|
|
14
|
+
const file = c.file(prefix + '/参考(一).md', prefix + '\nevidence\n');
|
|
15
|
+
sources.push(c.ok('source', 'add', file));
|
|
16
|
+
}
|
|
17
|
+
const draft = propose(c, sources[0]);
|
|
18
|
+
c.ok('approve', draft.draft, '--digest', draft.digest);
|
|
19
|
+
const out = path.join(c.root, 'readable');
|
|
20
|
+
const result = c.ok('export', '--out', out);
|
|
21
|
+
assert.equal(result.sources, 2);
|
|
22
|
+
assert.equal(result.notes, 1);
|
|
23
|
+
const files = readdirSync(path.join(out, 'originals'));
|
|
24
|
+
assert.equal(files.length, 2);
|
|
25
|
+
assert.deepEqual(files.map(p => readFileSync(path.join(out, 'originals', p), 'utf8')).sort(), ['first\nevidence\n','second\nevidence\n']);
|
|
26
|
+
const note = readFileSync(path.join(out, 'notes', readdirSync(path.join(out, 'notes'))[0]), 'utf8');
|
|
27
|
+
const link = /\[原件\]\(([^)]+)\)/.exec(note)[1];
|
|
28
|
+
assert.equal(readFileSync(path.resolve(out, 'notes', decodeURIComponent(link)), 'utf8'), 'first\nevidence\n');
|
|
29
|
+
assert.match(readFileSync(result.index, 'utf8'), /line:2/);
|
|
30
|
+
const catalog = readFileSync(path.join(out, 'catalog.csv'), 'utf8');
|
|
31
|
+
for (const m of sources) assert(catalog.includes(m.sha256));
|
|
32
|
+
writeFileSync(path.join(out, 'mine.txt'), 'keep');
|
|
33
|
+
assert.equal(c.fail('export', '--out', out), 'EXPORT_EXISTS');
|
|
34
|
+
assert.equal(readFileSync(path.join(out, 'mine.txt'), 'utf8'), 'keep');
|
|
35
|
+
const m = sources[0];
|
|
36
|
+
rmSync(path.join(c.vault, '.knowpool', 'objects', 'sha256', m.sha256.slice(0,2), m.sha256));
|
|
37
|
+
const incomplete = path.join(c.root, 'incomplete');
|
|
38
|
+
const failed = c.invoke(c.vault, 'export', '--out', incomplete);
|
|
39
|
+
assert.equal(failed.status, 1);
|
|
40
|
+
assert(failed.stderr.includes(m.id));
|
|
41
|
+
assert(!existsSync(incomplete));
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('team export uses only local main, including its original-file manifest set', t => {
|
|
45
|
+
const c = context(t, 'team');
|
|
46
|
+
c.ok('commit', '--message', 'bootstrap');
|
|
47
|
+
c.git('-C', c.vault, 'switch', '-c', 'knowledge/test');
|
|
48
|
+
const src = c.ok('source', 'add', c.file('private.txt', 'report\nsecond paragraph'));
|
|
49
|
+
const draft = propose(c, src);
|
|
50
|
+
c.ok('approve', draft.draft, '--digest', draft.digest);
|
|
51
|
+
c.ok('commit', '--message', 'unmerged knowledge');
|
|
52
|
+
const e = c.ok('export', '--out', path.join(c.root, 'export'));
|
|
53
|
+
assert.deepEqual([e.sources, e.notes], [0,0]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('rclone readback precedes push and pull restores objects before advancing HEAD',
|
|
57
|
+
{skip: spawnSync('rclone', ['version']).status !== 0}, t => {
|
|
58
|
+
const tmp = mkdtempSync(path.join(os.tmpdir(), 'knowpool-backend-'));
|
|
59
|
+
t.after(() => rmSync(tmp, {recursive:true,force:true}));
|
|
60
|
+
const config = path.join(tmp,'rclone.conf');
|
|
61
|
+
writeFileSync(config, '[fixture]\ntype = local\n');
|
|
62
|
+
const c = context(t, 'personal', { RCLONE_CONFIG: config });
|
|
63
|
+
c.git('-C', c.vault, 'remote', 'add', 'origin', c.bare);
|
|
64
|
+
const target = ['--remote','origin','--cloud','fixture:' + c.store];
|
|
65
|
+
c.ok('commit','--message','empty vault');
|
|
66
|
+
c.ok('sync',...target);
|
|
67
|
+
const reader = path.join(c.root,'fresh');
|
|
68
|
+
assert.equal(c.at(reader,'setup','--clone',c.bare).vault,c.id);
|
|
69
|
+
assert.equal(c.at(reader,'setup','--clone',c.bare).vault,c.id);
|
|
70
|
+
const before = c.git('-C',reader,'rev-parse','HEAD');
|
|
71
|
+
const file = c.file('report.txt','evidence\nline two\n');
|
|
72
|
+
const src = c.ok('source','add',file);
|
|
73
|
+
const draft = propose(c,src);
|
|
74
|
+
c.ok('approve',draft.draft,'--digest',draft.digest);
|
|
75
|
+
c.ok('commit','--message','publish report');
|
|
76
|
+
const sent = c.ok('sync',...target);
|
|
77
|
+
assert.equal(sent.state,'synced-local');
|
|
78
|
+
const remoteObject = path.join(c.store,c.id,'objects','sha256',src.sha256.slice(0,2),src.sha256);
|
|
79
|
+
writeFileSync(remoteObject,'broken\n');
|
|
80
|
+
assert.equal(JSON.parse(c.invoke(reader,'pull',...target).stderr).error,'INTEGRITY');
|
|
81
|
+
assert.equal(c.git('-C',reader,'rev-parse','HEAD'),before);
|
|
82
|
+
writeFileSync(remoteObject,readFileSync(file));
|
|
83
|
+
assert.equal(c.at(reader,'pull',...target).commit,sent.commit);
|
|
84
|
+
assert.equal(c.at(reader,'source','read',src.id,'--at','line:2').text,'line two');
|
|
85
|
+
assert.equal(c.at(reader,'verify','--metadata-only').checks,'metadata');
|
|
86
|
+
assert.equal(c.at(reader,'pull',...target).commit,sent.commit);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test('metadata-only verification runs on a fresh Git clone and rejects unexpected tracked data', t => {
|
|
90
|
+
const c = context(t);
|
|
91
|
+
c.ok('source','add',c.file('report.txt','source'));
|
|
92
|
+
c.ok('commit','--message','source');
|
|
93
|
+
c.ok(...c.sync);
|
|
94
|
+
const b = path.join(c.root,'clone');
|
|
95
|
+
c.git('clone',c.bare,b);
|
|
96
|
+
assert.equal(c.at(b,'verify','--metadata-only').sources,1);
|
|
97
|
+
assert.equal(c.invoke(b,'verify').status,1);
|
|
98
|
+
writeFileSync(path.join(b,'raw.dat'),'unintended');
|
|
99
|
+
c.git('-C',b,'add','raw.dat');
|
|
100
|
+
assert.equal(JSON.parse(c.invoke(b,'verify','--metadata-only').stderr).error,'HISTORY');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('multiple registered vaults are identified by ID and cross-vault search reports unavailable entries', t => {
|
|
104
|
+
const config = mkdtempSync(path.join(os.tmpdir(),'knowpool-registry-'));
|
|
105
|
+
t.after(() => rmSync(config,{recursive:true,force:true}));
|
|
106
|
+
const overrides = {KNOWPOOL_CONFIG_DIR:config};
|
|
107
|
+
const a = context(t,'personal',overrides);
|
|
108
|
+
const b = context(t,'personal',overrides);
|
|
109
|
+
a.ok('vault','register','personal');
|
|
110
|
+
b.ok('vault','register','project');
|
|
111
|
+
assert.equal(b.fail('vault','register','personal'),'CONTEXT');
|
|
112
|
+
const src = a.ok('source','add',a.file('meeting.md','one\ntwo'));
|
|
113
|
+
const d = propose(a,src,'persisted knowledge');
|
|
114
|
+
a.ok('approve',d.draft,'--digest',d.digest);
|
|
115
|
+
const call = (...args) => {
|
|
116
|
+
const p=spawnSync(process.execPath,[cli,...args],{env:{...env,...overrides},encoding:'utf8'});
|
|
117
|
+
assert.equal(p.status,0,p.stderr);
|
|
118
|
+
return JSON.parse(p.stdout);
|
|
119
|
+
};
|
|
120
|
+
assert.equal(call('search','persisted','--context','personal').matches.length,1);
|
|
121
|
+
const found=call('search','persisted','--all');
|
|
122
|
+
assert.equal(found.complete,true);
|
|
123
|
+
assert.deepEqual(found.results.map(x=>x.matches.length),[1,0]);
|
|
124
|
+
rmSync(path.join(b.vault,'knowpool.json'));
|
|
125
|
+
assert.equal(call('search','persisted','--all').complete,false);
|
|
126
|
+
});
|
package/test/support.mjs
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import os from 'node:os';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
|
|
8
|
+
export const cli = fileURLToPath(new URL('../bin/knowpool.mjs', import.meta.url));
|
|
9
|
+
export const env = { ...process.env, GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: os.devNull,
|
|
10
|
+
GIT_AUTHOR_NAME: 'KnowPool Test', GIT_AUTHOR_EMAIL: 'test@localhost',
|
|
11
|
+
GIT_COMMITTER_NAME: 'KnowPool Test', GIT_COMMITTER_EMAIL: 'test@localhost' };
|
|
12
|
+
|
|
13
|
+
export function context(t, mode = 'personal', overrides = {}) {
|
|
14
|
+
const taskEnv = { ...env, ...overrides };
|
|
15
|
+
const root = mkdtempSync(path.join(os.tmpdir(), 'knowpool-test-'));
|
|
16
|
+
t.after(() => rmSync(root, { recursive: true, force: true }));
|
|
17
|
+
const vault = path.join(root, 'vault');
|
|
18
|
+
const store = path.join(root, 'store');
|
|
19
|
+
const bare = path.join(root, 'remote.git');
|
|
20
|
+
mkdirSync(store);
|
|
21
|
+
function invoke(where, ...args) {
|
|
22
|
+
const p = spawnSync(process.execPath, [cli, '--vault', where, ...args], { env: taskEnv, encoding: 'utf8' });
|
|
23
|
+
if (p.error) throw p.error;
|
|
24
|
+
return p;
|
|
25
|
+
}
|
|
26
|
+
function at(where, ...args) {
|
|
27
|
+
const p = invoke(where, ...args);
|
|
28
|
+
assert.equal(p.status, 0, p.stderr);
|
|
29
|
+
return JSON.parse(p.stdout);
|
|
30
|
+
}
|
|
31
|
+
const ok = (...args) => at(vault, ...args);
|
|
32
|
+
function fail(...args) {
|
|
33
|
+
const p = invoke(vault, ...args);
|
|
34
|
+
assert.equal(p.status, 1, p.stdout);
|
|
35
|
+
return JSON.parse(p.stderr).error;
|
|
36
|
+
}
|
|
37
|
+
function git(...args) {
|
|
38
|
+
const p = spawnSync('git', args, { cwd: root, env: taskEnv, encoding: 'utf8' });
|
|
39
|
+
assert.equal(p.status, 0, p.stderr);
|
|
40
|
+
return p.stdout.trim();
|
|
41
|
+
}
|
|
42
|
+
function file(name, body) {
|
|
43
|
+
const out = path.join(root, name);
|
|
44
|
+
writeFileSync(out, body);
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
git('init', '--bare', '-b', 'main', bare);
|
|
48
|
+
const id = ok('init', '--mode', mode).id;
|
|
49
|
+
return { root, vault, store, bare, id, invoke, at, ok, fail, git, file,
|
|
50
|
+
sync: ['sync', '--git-dir', bare, '--store', store] };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function propose(c, source, body = '结论甲', more = []) {
|
|
54
|
+
return c.ok('draft', 'create', '--title', '语音决策', '--body-file', c.file('body.md', body),
|
|
55
|
+
'--source', source.id, '--at', 'line:2', ...more);
|
|
56
|
+
}
|