Files
marqdo-app-harness/packages/marqdo/harness/tests/execution.test.ts
T
cflmy 25fc24eea4 feat: add Marqdo Harness product services and workbench
Implement the downstream product in `packages/marqdo`:

- `@marqdo/mdh-harness`: application home layout, atomic writes, locks,
  bounded fixed-argv processes, safe tar extraction, Observation 1 and
  ValueWire validation;
- trusted Gitea release discovery, SHA-256/platform/Bundle verification,
  transactional environment install, rollback and cleanup, CLI Tool changes;
- private knowledge collections with strict import policy, the official
  `marqdo-knowledge` compile/query adapter and a disclosed locator fallback;
- Analysis 3 parsing, approved run/call execution, evidence and staleness;
- the six model operations (find/read/check/program/ecosystem/knowledge_search);
- the `/mdh` workbench over the existing web server with a same-origin fence;
- `mdh` launcher, CI/package/upstream scripts and a Woodpecker pipeline.

Tests: 28 node:test cases (27 PASS, one network-gated install test also PASS
with MDH_TEST_INSTALL=1). Product install/workbench evidence is recorded in
docs/mdh/evidence.

Part of marqdo#25 (MDH-03..MDH-08).
2026-11-28 10:41:39 +08:00

123 lines
5.0 KiB
TypeScript

/**
* Real CLI execution: Analysis 3, fixed argv, Observation 1 validation,
* evidence binding and staleness. The test needs `MARQDO_BIN` pointing at a
* released CLI; CI downloads a pinned archive and sets it.
*/
import assert from 'node:assert/strict'
import { mkdtemp, readFile, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { MdhRuntime } from '../src/runtime.ts'
import { checkArgument, grantArguments } from '../src/execution/cli.ts'
const bin = process.env.MARQDO_BIN
const FACTORIAL = `# @knowledge 数学
## @exec 阶乘
\`if n <= 1\`
\`return 1\`
\`else\`
\`结果 = n * 阶乘(n=n-1)\`
\`return 结果\`
\`end\`
`
test('execution requires a released CLI', { skip: bin ? false : 'MARQDO_BIN is not set; release integration is UNVERIFIED' }, async () => {
const root = await mkdtemp(join(tmpdir(), 'mdh-exec-'))
const workspace = join(root, 'workspace')
const home = join(root, 'home')
await import('node:fs/promises').then(fs => fs.mkdir(workspace, { recursive: true }))
await writeFile(join(workspace, 'factorial.mq.md'), FACTORIAL)
const previous = process.env.MDH_CLI_BIN
process.env.MDH_CLI_BIN = bin
try {
const runtime = await MdhRuntime.create({ home, workspace })
const checked = await runtime.check('factorial.mq.md')
assert.equal(checked.analysis.schemaVersion, 3)
assert.match(checked.analysis.digest, /^sha256:[0-9a-f]{64}$/)
const factorial = checked.analysis.functions.find(fn => fn.localName === '阶乘')
assert.ok(factorial)
assert.equal(factorial.namespace, '数学')
const executed = await runtime.execute({
relativePath: 'factorial.mq.md',
action: 'call',
functionName: '数学.阶乘',
args: ['n=5'],
mode: 'pure',
grants: {},
checked,
})
assert.equal(executed.executed.observation.status, 'completed')
assert.deepEqual(executed.executed.observation.value, { type: 'int', decimal: '120' })
assert.equal(executed.evidence.document_digest, checked.sourceDigest)
assert.equal(executed.evidence.analysis_digest, checked.analysis.digest)
assert.equal(executed.record.freshness, 'fresh')
const stored = JSON.parse(await readFile(join(home, 'evidence', 'runs', `${executed.evidence.run_id}.json`), 'utf8'))
assert.equal(stored.run_id, executed.evidence.run_id)
assert.equal(stored.result.value_render, '120')
// A changed source marks the previous evidence stale; it never becomes current.
await writeFile(join(workspace, 'factorial.mq.md'), `${FACTORIAL}\n普通说明。\n`)
const stale = await runtime.evidence.read(executed.evidence.run_id, workspace)
assert.equal(stale?.freshness, 'stale')
// A mismatched Analysis identity is never accepted as success.
const mismatched = await runtime.execute({
relativePath: 'factorial.mq.md',
action: 'call',
functionName: '数学.阶乘',
args: ['n=5'],
mode: 'pure',
grants: {},
checked: { ...checked, analysis: { ...checked.analysis, digest: 'sha256:wrong' } },
})
assert.notEqual(mismatched.executed.observation.status, 'completed')
assert.match(mismatched.executed.observation.violations.join(' '), /Analysis digest mismatch/)
// A runtime failure is recorded as failed with its structured error.
const failing = '# @knowledge 测试\n\n## @exec 失败\n\n`return 1 / 0`\n'
await writeFile(join(workspace, 'fail.mq.md'), failing)
const failChecked = await runtime.check('fail.mq.md')
const failed = await runtime.execute({
relativePath: 'fail.mq.md',
action: 'call',
functionName: '测试.失败',
mode: 'pure',
grants: {},
checked: failChecked,
})
assert.equal(failed.executed.observation.status, 'failed')
assert.equal(failed.evidence.status, 'failed')
assert.match(JSON.stringify(failed.executed.observation.error), /division_by_zero/)
await assert.rejects(runtime.check('../escape.mq.md'), /escapes|ENOENT|must be workspace-relative/)
} finally {
if (previous === undefined) delete process.env.MDH_CLI_BIN
else process.env.MDH_CLI_BIN = previous
}
})
test('argv construction refuses flags, escapes and pure grants', () => {
assert.throws(() => checkArgument('--output=json'), /argument must be name=value|invalid argument name/)
assert.throws(() => checkArgument('n'), /name=value/)
assert.throws(() => checkArgument('n=5', 1), /size limit/)
assert.deepEqual(grantArguments('pure', {}), ['--mode', 'pure'])
assert.throws(() => grantArguments('pure', { read: ['data/**'] }), /pure mode cannot carry/)
assert.throws(() => grantArguments('project', { read: ['/etc/passwd'] }), /project-relative/)
assert.throws(() => grantArguments('project', { read: ['../secret'] }), /escapes the project/)
assert.deepEqual(grantArguments('project', { read: ['data/**'], write: ['out/**'], random: true }), [
'--mode', 'project', '--allow-read', 'data/**', '--allow-write', 'out/**', '--allow-random',
])
})