|
| 1 | +const fs = require('node:fs'); |
| 2 | +const path = require('node:path'); |
| 3 | +const assert = require('node:assert/strict'); |
| 4 | +const { execFile } = require('node:child_process'); |
| 5 | +const exec = require('node:util').promisify(execFile); |
| 6 | +const os = require('node:os'); |
| 7 | +const test = require('node:test'); |
| 8 | +const repo = path.resolve(__dirname, '../..'); |
| 9 | +const transit = require(path.join(repo, 'cli/node_modules/transit-js')); |
| 10 | + |
| 11 | +test('CLI metadata writes preserve named links, backlinks and recurring journal refs', async t => { |
| 12 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'logseq-cli-metadata-refs-')); |
| 13 | + const graph = 'combined-metadata-refs'; |
| 14 | + const cli = path.join(repo, 'static/logseq-cli.js'); |
| 15 | + const report = { checks: 0, stages: [], failures: [] }; |
| 16 | + let server; |
| 17 | + async function command(...args) { |
| 18 | + try { |
| 19 | + const { stdout } = await exec(process.execPath, [cli, '--root-dir', root, '--graph', graph, |
| 20 | + '--output', 'json', '--timeout-ms', '120000', ...args], { timeout: 130000 }); |
| 21 | + const result = JSON.parse(stdout); |
| 22 | + assert.equal(result.status, 'ok', stdout); |
| 23 | + return result.data; |
| 24 | + } catch (error) { throw Error(`${args.join(' ')}\n${error.stdout || ''}\n${error.stderr || error.message}`); } |
| 25 | + } |
| 26 | + async function query(q) { return (await command('query', '--query', q)).result; } |
| 27 | + async function id(title) { return query(`[:find ?e . :where [?e :block/title ${JSON.stringify(title)}]]`); } |
| 28 | + async function uuid(eid) { return query(`[:find ?u . :where [${eid} :block/uuid ?u]]`); } |
| 29 | + async function titleRefs(eid) { return (await query(`[:find [?title ...] :where [${eid} :block/refs ?r] [?r :block/title ?title]]`)).sort(); } |
| 30 | + async function backlinkUUIDs(eid) { |
| 31 | + const response = await fetch(server['base-url'] + '/v1/invoke', { method: 'POST', |
| 32 | + headers: { 'Content-Type': 'application/json' }, |
| 33 | + body: JSON.stringify({ method: 'thread-api/get-block-refs', |
| 34 | + argsTransit: transit.writer('json').write([server.repo, eid]) }) }); |
| 35 | + const result = await response.json(); |
| 36 | + assert.equal(response.status, 200, JSON.stringify(result)); |
| 37 | + const blocks = transit.reader('json').read(result.resultTransit); |
| 38 | + const entries = []; |
| 39 | + if (Array.isArray(blocks)) entries.push(...blocks); |
| 40 | + else if (blocks && blocks.tag === 'list') entries.push(...blocks.rep); |
| 41 | + else if (blocks && typeof blocks.forEach === 'function') blocks.forEach(value => entries.push(value)); |
| 42 | + else if (blocks) throw Error('Backlink result shape: ' + require('node:util').inspect(blocks, {depth: 3})); |
| 43 | + return entries.map(block => String(block.get(transit.keyword('block/uuid')))); |
| 44 | + } |
| 45 | + function equal(label, actual, expected) { |
| 46 | + report.checks++; |
| 47 | + if (JSON.stringify(actual) !== JSON.stringify(expected)) report.failures.push({ label, actual, expected }); |
| 48 | + } |
| 49 | + async function checkpoint(label, block, required, absent = []) { |
| 50 | + const actual = await titleRefs(block); |
| 51 | + report.stages.push({ label, refs: actual }); |
| 52 | + for (const name of required) { |
| 53 | + equal(label + ' refs include ' + name, actual.includes(name), true); |
| 54 | + const target = await id(name); |
| 55 | + const membership = await query(`[:find ?tag . :where [${block} :block/tags ?tag] [(= ?tag ${target})]]`); |
| 56 | + equal(label + (membership ? ' tag membership excluded from linked references on ' : ' UI backlinks include source on ') + name, |
| 57 | + (await backlinkUUIDs(target)).includes(await uuid(block)), !membership); |
| 58 | + } |
| 59 | + for (const name of absent) { |
| 60 | + equal(label + ' refs exclude ' + name, actual.includes(name), false); |
| 61 | + const target = await id(name); |
| 62 | + if (target) equal(label + ' UI backlinks exclude source on ' + name, |
| 63 | + (await backlinkUUIDs(target)).includes(await uuid(block)), false); |
| 64 | + } |
| 65 | + } |
| 66 | + async function refreshServer() { |
| 67 | + server = (await command('server', 'list')).servers.find(item => item.graph === graph); |
| 68 | + equal('worker owned by CLI', server['owner-source'], 'cli'); |
| 69 | + } |
| 70 | + async function main() { |
| 71 | + await command('graph', 'create'); |
| 72 | + for (const name of ['Home', 'Alpha', 'Beta', 'T1', 'T2']) await command('upsert', 'page', '--page', name); |
| 73 | + for (const name of ['Topic', 'Topic2']) await command('upsert', 'tag', '--name', name); |
| 74 | + for (const name of ['p1', 'p2']) await command('upsert', 'property', '--name', name, '--type', 'node'); |
| 75 | + await refreshServer(); |
| 76 | + const created = await command('upsert', 'block', '--target-page', 'Home', '--content', 'initial [[Alpha]]', '--update-tags', '["Topic"]'); |
| 77 | + const block = created.result[0]; |
| 78 | + await checkpoint('create with tag', block, ['Alpha', 'Tags', 'Topic']); |
| 79 | + await command('upsert', 'block', '--id', String(block), '--content', 'updated [[Beta]] and [[Namespace/Child]]'); |
| 80 | + await checkpoint('named and namespace update', block, ['Beta', 'Child', 'Tags', 'Topic'], ['Alpha']); |
| 81 | + await command('upsert', 'block', '--id', String(block), '--update-tags', '["Topic2"]'); |
| 82 | + await checkpoint('subsequent tag operation', block, ['Beta', 'Child', 'Tags', 'Topic', 'Topic2'], ['Alpha']); |
| 83 | + const p1 = await id('p1'), p2 = await id('p2'), t1 = await id('T1'), t2 = await id('T2'); |
| 84 | + const legacy = (await command('upsert', 'block', '--target-page', 'Home', '--content', 'legacy [[Alpha]] and [[Namespace/Child]]')).result[0]; |
| 85 | + report.legacyStoredTitle = await query(`[:find ?title . :where [${legacy} :block/title ?title]]`); |
| 86 | + await checkpoint('raw named links before metadata', legacy, ['Alpha', 'Child']); |
| 87 | + await command('upsert', 'block', '--id', String(legacy), '--update-tags', '["Topic"]'); |
| 88 | + await checkpoint('raw named links after separate tag write', legacy, ['Alpha', 'Child', 'Tags', 'Topic']); |
| 89 | + await command('upsert', 'block', '--id', String(legacy), '--update-properties', `{${p1} ${t1} ${p2} ${t2}}`); |
| 90 | + await checkpoint('raw named links after separate property write', legacy, ['Alpha', 'Child', 'p1', 'p2', 'T1', 'T2']); |
| 91 | + await command('upsert', 'block', '--id', String(block), '--update-properties', `{${p1} ${t1} ${p2} ${t2}}`); |
| 92 | + await checkpoint('subsequent two-node-property operation', block, ['Beta', 'Child', 'T1', 'T2', 'p1', 'p2']); |
| 93 | + await command('upsert', 'block', '--id', String(block), '--content', 'replacement [[Alpha]] and [[Namespace/Other]]', |
| 94 | + '--remove-tags', '["Topic"]', '--update-properties', `{${p1} ${t2}}`, '--remove-properties', `[${p2}]`); |
| 95 | + await checkpoint('combined title and metadata replacement', block, ['Alpha', 'Other', 'Tags', 'Topic2', 'p1', 'T2'], ['Beta', 'Child', 'Topic', 'p2', 'T1']); |
| 96 | + await command('upsert', 'block', '--id', String(block), '--remove-tags', '["Topic2"]', '--remove-properties', `[${p1}]`); |
| 97 | + await checkpoint('metadata removed', block, ['Alpha', 'Other'], ['Tags', 'Topic2', 'p1', 'T2']); |
| 98 | + const tasks = []; |
| 99 | + for (const temporal of ['scheduled', 'deadline']) { |
| 100 | + const property = ':logseq.property/' + temporal; |
| 101 | + const task = (await command('upsert', 'block', '--target-page', 'Home', '--content', temporal + ' [[Alpha]] and [[Namespace/Child]]')).result[0]; |
| 102 | + await command('upsert', 'task', '--id', String(task), '--status', 'todo'); |
| 103 | + const stamp = new Date(2026, 10, 8, 12).getTime(); |
| 104 | + await command('upsert', 'block', '--id', String(task), '--update-properties', |
| 105 | + `{${property} ${stamp} :logseq.property.repeat/repeated? true :logseq.property.repeat/recur-frequency 1 :logseq.property.repeat/recur-unit :logseq.property.repeat/recur-unit.week :logseq.property.repeat/repeat-type :logseq.property.repeat/repeat-type.plus :logseq.property.repeat/temporal-property ${property}}`); |
| 106 | + for (const day of [20261115, 20261122]) { |
| 107 | + await command('upsert', 'task', '--id', String(task), '--status', 'done'); |
| 108 | + const actual = await query(`[:find [?day ...] :where [${task} :block/refs ?j] [?j :block/journal-day ?day]]`); |
| 109 | + equal(temporal + ' journal refs ' + day, actual, [day]); |
| 110 | + const stored = await query(`[:find ?stamp . :where [${task} ${property} ?stamp]]`); |
| 111 | + const date = new Date(stored); |
| 112 | + equal(temporal + ' stored date ' + day, Number(`${date.getFullYear()}${String(date.getMonth()+1).padStart(2,'0')}${String(date.getDate()).padStart(2,'0')}`), day); |
| 113 | + const journal = await query(`[:find ?j . :where [?j :block/journal-day ${day}]]`); |
| 114 | + equal(temporal + ' new journal UI backlinks ' + day, (await backlinkUUIDs(journal)).includes(await uuid(task)), true); |
| 115 | + const oldDay = day === 20261115 ? 20261108 : 20261115; |
| 116 | + const old = await query(`[:find ?j . :where [?j :block/journal-day ${oldDay}]]`); |
| 117 | + if (old) equal(temporal + ' old journal UI backlinks removed ' + oldDay, (await backlinkUUIDs(old)).includes(await uuid(task)), false); |
| 118 | + await checkpoint(temporal + ' completion ' + day, task, day === 20261115 ? ['Alpha', 'Child', 'Tags', 'Task', 'Status'] : ['Beta', 'Other', 'Tags', 'Task', 'Status'], day === 20261122 ? ['Alpha', 'Child'] : []); |
| 119 | + if (day === 20261115) await command('upsert', 'block', '--id', String(task), '--content', temporal + ' replacement [[Beta]] and [[Namespace/Other]]'); |
| 120 | + } |
| 121 | + tasks.push({ task, temporal }); |
| 122 | + } |
| 123 | + await command('server', 'restart'); |
| 124 | + await refreshServer(); |
| 125 | + await checkpoint('restart raw named links', legacy, ['Alpha', 'Child', 'Tags', 'Topic', 'p1', 'p2', 'T1', 'T2']); |
| 126 | + await checkpoint('restart ordinary block', block, ['Alpha', 'Other'], ['Beta', 'Child', 'Tags', 'Topic', 'Topic2', 'p1', 'p2', 'T1', 'T2']); |
| 127 | + for (const {task, temporal} of tasks) { |
| 128 | + await checkpoint('restart ' + temporal, task, ['Beta', 'Other', 'Tags', 'Task', 'Status'], ['Alpha', 'Child']); |
| 129 | + equal('restart ' + temporal + ' journal refs', await query(`[:find [?day ...] :where [${task} :block/refs ?j] [?j :block/journal-day ?day]]`), [20261122]); |
| 130 | + } |
| 131 | + const beforeRenameTitle = await query(`[:find ?title . :where [${legacy} :block/title ?title]]`); |
| 132 | + const alpha = await id('Alpha'), child = await id('Child'); |
| 133 | + equal('created named and namespace links use Desktop UUID storage', beforeRenameTitle, |
| 134 | + `legacy [[${await uuid(alpha)}]] and [[${await uuid(child)}]]`); |
| 135 | + await command('upsert', 'page', '--id', String(alpha), '--rename', 'Renamed Alpha'); |
| 136 | + await command('upsert', 'page', '--id', String(child), '--rename', 'Renamed Child'); |
| 137 | + await command('upsert', 'block', '--id', String(legacy), '--update-tags', '["Topic2"]'); |
| 138 | + await checkpoint('rename then metadata', legacy, ['Renamed Alpha', 'Renamed Child', 'Topic', 'Topic2', 'Tags', 'p1', 'p2', 'T1', 'T2']); |
| 139 | + equal('rename preserves stored UUID title', await query(`[:find ?title . :where [${legacy} :block/title ?title]]`), beforeRenameTitle); |
| 140 | + await command('server', 'restart'); |
| 141 | + await refreshServer(); |
| 142 | + await checkpoint('restart after rename', legacy, ['Renamed Alpha', 'Renamed Child', 'Topic', 'Topic2', 'Tags', 'p1', 'p2', 'T1', 'T2']); |
| 143 | + await command('graph', 'validate'); |
| 144 | + } |
| 145 | + t.after(async () => { |
| 146 | + let teardownError; |
| 147 | + try { |
| 148 | + await command('server', 'stop'); |
| 149 | + const logs = fs.readdirSync(root, { recursive: true }).filter(name => /db-worker-node-.*\.log$/.test(name)); |
| 150 | + const errors = logs.flatMap(name => fs.readFileSync(path.join(root, name), 'utf8') |
| 151 | + .split('\n').filter(line => /ERROR|Exception|invalid-data/.test(line))); |
| 152 | + t.diagnostic(`Checked ${logs.length} worker log(s): ${errors.length} error line(s)`); |
| 153 | + for (const line of errors) t.diagnostic(line); |
| 154 | + } catch (error) { |
| 155 | + teardownError = error; |
| 156 | + throw error; |
| 157 | + } finally { |
| 158 | + try { |
| 159 | + fs.rmSync(root, { recursive: true, force: true }); |
| 160 | + } catch (cleanupError) { |
| 161 | + if (teardownError) throw new AggregateError([teardownError, cleanupError], 'Teardown and directory cleanup failed', { cause: teardownError }); |
| 162 | + throw cleanupError; |
| 163 | + } |
| 164 | + } |
| 165 | + }); |
| 166 | + await main(); |
| 167 | + t.diagnostic(`${report.stages.length} stages, ${report.checks} reference and backlink checks`); |
| 168 | + assert.deepEqual(report.failures, [], JSON.stringify(report, null, 2)); |
| 169 | +}); |
0 commit comments