Files
marqdo-app-harness/packages/marqdo/harness/tests/ecosystem.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

70 lines
3.2 KiB
TypeScript

/**
* Ecosystem policy tests that do not require network access: authority fences,
* empty-state projection, workbench asset safety and same-origin trust.
*/
import assert from 'node:assert/strict'
import { mkdtemp } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { test } from 'node:test'
import { ensureMdhHome } from '../src/core/home.ts'
import { EcosystemManager } from '../src/ecosystem/manager.ts'
import { downloadAsset, ReleaseError } from '../src/ecosystem/sources.ts'
import type { ReleaseCandidate } from '../src/ecosystem/sources.ts'
import { isLoopbackAuthority, isTrustedAuthority } from '../src/web/trust.ts'
import { APP_HTML, APP_JS, APP_CSS } from '../src/web/assets.ts'
test('an empty ecosystem reports no runtime and no tool', async () => {
const root = await mkdtemp(join(tmpdir(), 'mdh-eco-'))
const layout = await ensureMdhHome(join(root, 'home'))
const manager = new EcosystemManager(layout)
const status = await manager.status()
assert.equal(status.runtime.installed, false)
assert.equal(status.std.installed, false)
assert.deepEqual(status.environments, [])
assert.deepEqual(status.tools, [])
assert.equal(await manager.current(), undefined)
await assert.rejects(manager.rollback(), /no previous environment/)
})
test('downloads outside the trusted authority are refused', async () => {
const root = await mkdtemp(join(tmpdir(), 'mdh-dl-'))
const candidate: ReleaseCandidate = {
component: 'cli', repo: 'marqdo-cli', tag: 'v1', sourceSha: null,
assetName: 'x.tar.gz', assetUrl: 'https://evil.invalid/x.tar.gz',
assetSize: 1, assetSha256: '0'.repeat(64), platform: 'x86_64-unknown-linux-gnu',
browserUrl: '', ci: null, dependencies: [],
}
await assert.rejects(downloadAsset(candidate, join(root, 'x.tar.gz')), ReleaseError)
})
test('same-origin trust accepts loopback and explicit authorities only', () => {
assert.equal(isLoopbackAuthority('127.0.0.1:3080'), true)
assert.equal(isLoopbackAuthority('localhost:3080'), true)
assert.equal(isLoopbackAuthority('[::1]:3080'), true)
assert.equal(isLoopbackAuthority('127.0.0.2'), true)
assert.equal(isLoopbackAuthority('10.1.2.3:3080'), false)
assert.equal(isLoopbackAuthority('0.0.0.0:3080'), false)
assert.equal(isLoopbackAuthority('evil.example'), false)
assert.equal(isTrustedAuthority('10.1.2.3:3080', ['10.1.2.3'], undefined), true)
assert.equal(isTrustedAuthority('10.1.2.3:9999', ['10.1.2.3'], undefined), true)
assert.equal(isTrustedAuthority('10.1.2.3:9999', ['10.1.2.3:3080'], undefined), false)
assert.equal(isTrustedAuthority('10.1.2.3:3080', [], '10.1.2.3'), true)
assert.equal(isTrustedAuthority('evil.example', ['10.1.2.3']), false)
})
test('the workbench loads no external asset and no inline script', () => {
assert.doesNotMatch(APP_HTML, /https?:\/\//)
assert.doesNotMatch(APP_HTML, /<script(?![^>]*src=)/)
assert.doesNotMatch(APP_HTML, /on(click|submit|load)=/i)
assert.match(APP_HTML, /script type="module" src="\/mdh\/app\.js"/)
assert.doesNotMatch(APP_CSS, /@import|url\(/i)
assert.doesNotMatch(APP_JS, /eval\(|new Function|document\.write/)
assert.match(APP_JS, /textContent/)
// Knowledge text must never be interpolated as HTML.
assert.doesNotMatch(APP_JS, /innerHTML\s*=/)
})