@acryldev/harness-runtime 0.0.0-stage → 0.2.11
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 +49 -0
- package/lib/index.mjs +5693 -0
- package/lib/index.mjs.map +1 -0
- package/lib/types/acryl-home.d.ts +81 -0
- package/lib/types/app/new-app.d.ts +65 -0
- package/lib/types/app/start-source.d.ts +47 -0
- package/lib/types/blueprint/blueprint.d.ts +74 -0
- package/lib/types/blueprint/brand-identity.d.ts +26 -0
- package/lib/types/blueprint/compose.d.ts +53 -0
- package/lib/types/blueprint/definition.d.ts +22 -0
- package/lib/types/blueprint/index.d.ts +7 -0
- package/lib/types/blueprint/manifest.d.ts +35 -0
- package/lib/types/blueprint/selection.d.ts +19 -0
- package/lib/types/cli-market-install.d.ts +97 -0
- package/lib/types/cli-market-plugins.d.ts +81 -0
- package/lib/types/coding-capabilities.d.ts +29 -0
- package/lib/types/durable-message.d.ts +18 -0
- package/lib/types/engine-dsh.d.ts +181 -0
- package/lib/types/engine-files.d.ts +8 -0
- package/lib/types/engine-host.d.ts +32 -0
- package/lib/types/engine-tools.d.ts +11 -0
- package/lib/types/index.d.ts +67 -0
- package/lib/types/instance/announce-self.d.ts +38 -0
- package/lib/types/instance/app-instance.d.ts +125 -0
- package/lib/types/instance/index.d.ts +6 -0
- package/lib/types/instance/offline-lock.d.ts +29 -0
- package/lib/types/instance/online-secret.d.ts +29 -0
- package/lib/types/instance/registry.d.ts +26 -0
- package/lib/types/instance/select.d.ts +43 -0
- package/lib/types/module-hooks.d.ts +19 -0
- package/lib/types/module-resolution.d.ts +31 -0
- package/lib/types/package-overlay.d.ts +41 -0
- package/lib/types/pinned-pnpm.d.ts +15 -0
- package/lib/types/plugin-acryl-workspace-status.d.ts +24 -0
- package/lib/types/plugin-doctor.d.ts +49 -0
- package/lib/types/plugin-lifecycle-state.d.ts +92 -0
- package/lib/types/plugin-lifecycle.d.ts +87 -0
- package/lib/types/profile-layout.d.ts +17 -0
- package/lib/types/profile-owner.d.ts +25 -0
- package/lib/types/profile-repair/backup.d.ts +37 -0
- package/lib/types/profile-repair/findings.d.ts +29 -0
- package/lib/types/profile-repair/index.d.ts +9 -0
- package/lib/types/profile-repair/inspector.d.ts +11 -0
- package/lib/types/profile-repair/repair.d.ts +38 -0
- package/lib/types/profile-resolution/moved-framework.d.ts +21 -0
- package/lib/types/profile-resolution/resolver.d.ts +41 -0
- package/lib/types/profile-resolution/service.d.ts +22 -0
- package/lib/types/prompt-budget.d.ts +44 -0
- package/lib/types/session-bridge.d.ts +57 -0
- package/lib/types/session-log-exporter.d.ts +31 -0
- package/lib/types/system-prompt-capture.d.ts +29 -0
- package/lib/types/web-favicon-data.d.ts +5 -0
- package/lib/types/web-favicon.d.ts +23 -0
- package/lib/types/web-market-install.d.ts +114 -0
- package/lib/types/web-market-plugins.d.ts +106 -0
- package/lib/types/web-port.d.ts +15 -0
- package/package.json +211 -3
- package/src/acryl-home.ts +103 -0
- package/src/app/new-app.ts +239 -0
- package/src/app/start-source.ts +110 -0
- package/src/blueprint/blueprint.ts +141 -0
- package/src/blueprint/brand-identity.ts +75 -0
- package/src/blueprint/compose.ts +222 -0
- package/src/blueprint/definition.ts +92 -0
- package/src/blueprint/index.ts +20 -0
- package/src/blueprint/manifest.ts +145 -0
- package/src/blueprint/selection.ts +74 -0
- package/src/cli-market-install.ts +181 -0
- package/src/cli-market-plugins.ts +176 -0
- package/src/coding-capabilities.ts +61 -0
- package/src/durable-message.ts +23 -0
- package/src/engine-dsh.ts +856 -0
- package/src/engine-files.ts +9 -0
- package/src/engine-host.ts +128 -0
- package/src/engine-tools.ts +26 -0
- package/src/index.ts +436 -0
- package/src/instance/announce-self.ts +88 -0
- package/src/instance/app-instance.ts +240 -0
- package/src/instance/index.ts +45 -0
- package/src/instance/offline-lock.ts +71 -0
- package/src/instance/online-secret.ts +55 -0
- package/src/instance/registry.ts +53 -0
- package/src/instance/select.ts +134 -0
- package/src/module-hooks.ts +41 -0
- package/src/module-resolution.ts +187 -0
- package/src/package-overlay.ts +160 -0
- package/src/pinned-pnpm.ts +67 -0
- package/src/plugin-acryl-workspace-status.ts +99 -0
- package/src/plugin-doctor.ts +141 -0
- package/src/plugin-lifecycle-state.ts +347 -0
- package/src/plugin-lifecycle.ts +223 -0
- package/src/profile-layout.ts +97 -0
- package/src/profile-owner.ts +52 -0
- package/src/profile-repair/backup.ts +119 -0
- package/src/profile-repair/findings.ts +43 -0
- package/src/profile-repair/index.ts +10 -0
- package/src/profile-repair/inspector.ts +156 -0
- package/src/profile-repair/repair.ts +137 -0
- package/src/profile-resolution/moved-framework.ts +64 -0
- package/src/profile-resolution/resolver.ts +876 -0
- package/src/profile-resolution/service.ts +99 -0
- package/src/prompt-budget.ts +118 -0
- package/src/session-bridge.ts +350 -0
- package/src/session-log-exporter.ts +68 -0
- package/src/system-prompt-capture.ts +99 -0
- package/src/web-favicon-data.ts +6 -0
- package/src/web-favicon.ts +43 -0
- package/src/web-market-install.ts +206 -0
- package/src/web-market-plugins.ts +215 -0
- package/src/web-port.ts +39 -0
- package/tests/account-callback.spec.ts +173 -0
- package/tests/acryl-home.spec.ts +39 -0
- package/tests/agent-self-hosting.spec.ts +101 -0
- package/tests/announce-self.spec.ts +84 -0
- package/tests/app-lifecycle.spec.ts +99 -0
- package/tests/blank-blueprint.spec.ts +110 -0
- package/tests/blueprint-definition.spec.ts +128 -0
- package/tests/blueprint.spec.ts +138 -0
- package/tests/bulkhead.spec.ts +187 -0
- package/tests/coding-vocabulary.spec.ts +15 -0
- package/tests/dump-system-prompt.spec.ts +65 -0
- package/tests/e2e-real-model.spec.ts +71 -0
- package/tests/e2e-restart-real-model.spec.ts +77 -0
- package/tests/engine-dsh.spec.ts +340 -0
- package/tests/engine-host-mount-root-include.spec.ts +118 -0
- package/tests/engine-host.spec.ts +176 -0
- package/tests/engine-optional.spec.ts +81 -0
- package/tests/extension-examples.spec.ts +261 -0
- package/tests/hooks-thread-stack.ts +27 -0
- package/tests/instance-patches.spec.ts +25 -0
- package/tests/instance.spec.ts +235 -0
- package/tests/live/community-plugins.spec.ts +83 -0
- package/tests/module-hooks.spec.ts +35 -0
- package/tests/module-resolution.spec.ts +265 -0
- package/tests/moved-framework.spec.ts +61 -0
- package/tests/new-app.spec.ts +132 -0
- package/tests/organizer-growth.spec.ts +117 -0
- package/tests/pinned-pnpm.spec.ts +34 -0
- package/tests/plugin-acryl-workspace-status.spec.ts +63 -0
- package/tests/plugin-context.spec.ts +640 -0
- package/tests/plugin-doctor.spec.ts +183 -0
- package/tests/plugin-enablement-one-store.spec.ts +130 -0
- package/tests/plugin-lifecycle-state.spec.ts +169 -0
- package/tests/profile-layout.spec.ts +58 -0
- package/tests/profile-owner.spec.ts +38 -0
- package/tests/profile-repair/profile-repair.spec.ts +231 -0
- package/tests/profile-resolution-port.spec.ts +30 -0
- package/tests/profile-resolution-service.spec.ts +273 -0
- package/tests/profile-resolution-swap.spec.ts +69 -0
- package/tests/profile-resolution.spec.ts +2535 -0
- package/tests/profile.spec.ts +70 -0
- package/tests/project-registry.spec.ts +81 -0
- package/tests/prompt-budget.spec.ts +58 -0
- package/tests/real-home-guard.spec.ts +178 -0
- package/tests/session-bridge.spec.ts +316 -0
- package/tests/start-source.spec.ts +65 -0
- package/tests/system-prompt-shape.spec.ts +95 -0
- package/tests/three-levels.spec.ts +84 -0
- package/tests/tool-gateway.spec.ts +116 -0
- package/tests/tool-schemas.spec.ts +41 -0
- package/tests/web-favicon.spec.ts +42 -0
- package/tests/web-port.spec.ts +38 -0
- package/tests/web-startup-hold.spec.ts +79 -0
- package/tsconfig.json +24 -0
- package/tsdown.config.ts +2 -0
- package/vitest.config.ts +18 -0
- package/README.md +0 -3
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from 'node:fs'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
6
|
+
import { resolvePluginLifecycleStatePath } from '../../src/plugin-lifecycle-state.ts'
|
|
7
|
+
import {
|
|
8
|
+
applyRepairPlan, backupsDir, createBackup, describePlan, inspectProfile, listBackups, planRepairs, restoreBackup, undoRepair,
|
|
9
|
+
} from '../../src/profile-repair/index.ts'
|
|
10
|
+
|
|
11
|
+
const homes: string[] = []
|
|
12
|
+
afterEach(() => { for (const home of homes.splice(0)) rmSync(home, { recursive: true, force: true }) })
|
|
13
|
+
|
|
14
|
+
function makeHome(options: { bundles?: string[]; installed?: string[]; state?: string; log?: string; layout?: { recorded: string; declared?: string } } = {}) {
|
|
15
|
+
const dshHome = mkdtempSync(join(tmpdir(), 'acryl-repair-'))
|
|
16
|
+
homes.push(dshHome)
|
|
17
|
+
const profileDir = join(dshHome, 'profiles', 'main')
|
|
18
|
+
mkdirSync(profileDir, { recursive: true })
|
|
19
|
+
writeFileSync(join(profileDir, 'package.json'), JSON.stringify({ dsh: { profile: { bundles: ['@deepseek-ai/dsh-base', ...(options.bundles ?? [])] } } }))
|
|
20
|
+
for (const name of options.installed ?? []) {
|
|
21
|
+
mkdirSync(join(profileDir, 'node_modules', ...name.split('/')), { recursive: true })
|
|
22
|
+
writeFileSync(join(profileDir, 'node_modules', ...name.split('/'), 'package.json'), '{}')
|
|
23
|
+
}
|
|
24
|
+
if (options.state !== undefined) {
|
|
25
|
+
mkdirSync(join(dshHome, 'plugin-lifecycle'), { recursive: true })
|
|
26
|
+
writeFileSync(resolvePluginLifecycleStatePath(dshHome), options.state)
|
|
27
|
+
}
|
|
28
|
+
if (options.log !== undefined) {
|
|
29
|
+
mkdirSync(join(dshHome, 'logs'), { recursive: true })
|
|
30
|
+
writeFileSync(join(dshHome, 'logs', 'dsh-2026-09-27.error.log'), options.log)
|
|
31
|
+
}
|
|
32
|
+
if (options.layout !== undefined) {
|
|
33
|
+
mkdirSync(join(profileDir, 'node_modules'), { recursive: true })
|
|
34
|
+
writeFileSync(join(profileDir, 'node_modules', '.modules.yaml'), `nodeLinker: ${options.layout.recorded}\n`)
|
|
35
|
+
writeFileSync(join(profileDir, 'pnpm-workspace.yaml'), options.layout.declared === undefined ? 'packages: []\n' : `nodeLinker: ${options.layout.declared}\n`)
|
|
36
|
+
}
|
|
37
|
+
return { dshHome, profileDir, statePath: resolvePluginLifecycleStatePath(dshHome) }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A hash of every file under a directory, to prove a read-only operation changed nothing. */
|
|
41
|
+
function fingerprint(dir: string): string {
|
|
42
|
+
const parts: string[] = []
|
|
43
|
+
const walk = (path: string): void => {
|
|
44
|
+
for (const name of readdirSync(path).sort()) {
|
|
45
|
+
const full = join(path, name)
|
|
46
|
+
if (statSync(full).isDirectory()) walk(full)
|
|
47
|
+
else parts.push(`${full}:${createHash('sha256').update(readFileSync(full)).digest('hex')}`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
walk(dir)
|
|
51
|
+
return parts.join('\n')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const NESTED_FAILURE = 'failed to apply loader entry acryl-engine (cordis:acryl-engine-dsh): failed to apply loader entry include (cordis:include): failed to apply loader entry acryl-broken (acryl-broken): TypeError: cannot get property theme without inject'
|
|
55
|
+
const codes = (findings: readonly { code: string }[]): string[] => findings.map(f => f.code)
|
|
56
|
+
|
|
57
|
+
describe('inspectProfile', () => {
|
|
58
|
+
it('finds nothing wrong with a healthy profile, and never writes anything', () => {
|
|
59
|
+
const { dshHome } = makeHome({ bundles: ['acryl-ok'], installed: ['acryl-ok'], state: '{"version":1,"profiles":[]}', log: 'all good\n' })
|
|
60
|
+
const before = fingerprint(dshHome)
|
|
61
|
+
expect(inspectProfile({ dshHome, profileName: 'main' }).findings).toEqual([])
|
|
62
|
+
expect(fingerprint(dshHome)).toBe(before)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('reports a profile with no directory', () => {
|
|
66
|
+
const { dshHome } = makeHome()
|
|
67
|
+
expect(codes(inspectProfile({ dshHome, profileName: 'ghost' }).findings)).toEqual(['profile-missing'])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('reports a corrupt override file with the recipe that fixes it', () => {
|
|
71
|
+
const { dshHome, statePath } = makeHome({ state: '{not json' })
|
|
72
|
+
const [finding] = inspectProfile({ dshHome, profileName: 'main' }).findings
|
|
73
|
+
expect(finding).toMatchObject({ severity: 'error', code: 'state-unreadable', file: statePath, recipe: 'restore-override-file' })
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('reports a damaged profile manifest and a listed bundle that is not installed', () => {
|
|
77
|
+
const missing = makeHome({ bundles: ['acryl-not-there', '@scope/also-missing'], installed: [] })
|
|
78
|
+
expect(inspectProfile({ dshHome: missing.dshHome, profileName: 'main' }).findings.map(f => [f.code, f.entryId])).toEqual([['bundle-missing', '@scope/also-missing'], ['bundle-missing', 'acryl-not-there']])
|
|
79
|
+
const broken = makeHome()
|
|
80
|
+
writeFileSync(join(broken.profileDir, 'package.json'), '{oops')
|
|
81
|
+
expect(codes(inspectProfile({ dshHome: broken.dshHome, profileName: 'main' }).findings)).toEqual(['profile-manifest-unreadable'])
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('reports a pnpm layout that disagrees with what is installed, as a warning with guidance', () => {
|
|
85
|
+
const { dshHome } = makeHome({ layout: { recorded: 'hoisted', declared: 'isolated' } })
|
|
86
|
+
const [finding] = inspectProfile({ dshHome, profileName: 'main' }).findings
|
|
87
|
+
expect(finding).toMatchObject({ severity: 'warning', code: 'pnpm-layout-mismatch' })
|
|
88
|
+
expect(finding?.guidance).toContain('Do not reinstall by hand')
|
|
89
|
+
expect(inspectProfile({ dshHome: makeHome({ layout: { recorded: 'isolated' } }).dshHome, profileName: 'main' }).findings).toEqual([])
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('names the innermost plugin that failed to activate, not the rows that only wrap it', () => {
|
|
93
|
+
const { dshHome } = makeHome({ log: `${NESTED_FAILURE}\n` })
|
|
94
|
+
const [finding] = inspectProfile({ dshHome, profileName: 'main' }).findings
|
|
95
|
+
expect(finding).toMatchObject({ code: 'plugin-activation-failed', entryId: 'acryl-broken', recipe: 'disable-failing-row' })
|
|
96
|
+
expect(finding?.message).toContain('cannot get property theme without inject')
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
it('does not offer to disable an engine row, and says what to do instead', () => {
|
|
100
|
+
const { dshHome } = makeHome({ log: 'failed to apply loader entry acryl-engine (x): failed to apply loader entry include (y): failed to apply loader entry webserver (z): listen EADDRINUSE: address already in use 127.0.0.1:3080\n' })
|
|
101
|
+
const [finding] = inspectProfile({ dshHome, profileName: 'main' }).findings
|
|
102
|
+
expect(finding).toMatchObject({ code: 'plugin-activation-failed', entryId: 'webserver' })
|
|
103
|
+
expect(finding?.recipe).toBeUndefined()
|
|
104
|
+
expect(finding?.guidance).toContain('part of the engine')
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it('recognizes the pnpm store mismatch symptom and refuses to call it fixable automatically', () => {
|
|
108
|
+
const { dshHome } = makeHome({ log: 'the package manager did not complete successfully\n' })
|
|
109
|
+
const [finding] = inspectProfile({ dshHome, profileName: 'main' }).findings
|
|
110
|
+
expect(finding).toMatchObject({ code: 'pnpm-store-mismatch', severity: 'warning' })
|
|
111
|
+
expect(finding?.recipe).toBeUndefined()
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
describe('repair', () => {
|
|
116
|
+
it('disables a failing row, keeps a backup, and undo puts everything back', async () => {
|
|
117
|
+
const { dshHome, statePath } = makeHome({ log: `${NESTED_FAILURE}\n` })
|
|
118
|
+
expect(existsSync(statePath)).toBe(false)
|
|
119
|
+
const plan = planRepairs(inspectProfile({ dshHome, profileName: 'main' }))
|
|
120
|
+
expect(plan.steps.map(s => s.recipe)).toEqual(['disable-failing-row'])
|
|
121
|
+
expect(describePlan(plan)).toContain('acryl-broken')
|
|
122
|
+
expect(describePlan(plan)).toContain(statePath)
|
|
123
|
+
const result = await applyRepairPlan(plan)
|
|
124
|
+
expect(JSON.parse(readFileSync(statePath, 'utf8')).profiles).toEqual([{ profileName: 'main', disabledEntries: ['acryl-broken'] }])
|
|
125
|
+
expect(listBackups(dshHome).map(b => b.id)).toEqual([result.backupId])
|
|
126
|
+
await undoRepair(dshHome, result.backupId)
|
|
127
|
+
expect(existsSync(statePath)).toBe(false)
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('replaces a corrupt override file, keeping the damaged bytes in the backup for undo', async () => {
|
|
131
|
+
const { dshHome, statePath } = makeHome({ state: '{not json' })
|
|
132
|
+
const result = await applyRepairPlan(planRepairs(inspectProfile({ dshHome, profileName: 'main' })))
|
|
133
|
+
expect(JSON.parse(readFileSync(statePath, 'utf8'))).toEqual({ version: 1, profiles: [] })
|
|
134
|
+
expect(inspectProfile({ dshHome, profileName: 'main' }).findings).toEqual([])
|
|
135
|
+
await undoRepair(dshHome, result.backupId)
|
|
136
|
+
expect(readFileSync(statePath, 'utf8')).toBe('{not json')
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
it('restores the last valid override file from an earlier backup when there is one', async () => {
|
|
140
|
+
const good = JSON.stringify({ version: 1, profiles: [{ profileName: 'main', disabledEntries: ['include:keep-me'] }] })
|
|
141
|
+
const { dshHome, statePath } = makeHome({ state: good })
|
|
142
|
+
await createBackup({ dshHome, profileName: 'main', recipes: ['manual'], files: [statePath] })
|
|
143
|
+
writeFileSync(statePath, '{corrupt')
|
|
144
|
+
await applyRepairPlan(planRepairs(inspectProfile({ dshHome, profileName: 'main' })))
|
|
145
|
+
expect(JSON.parse(readFileSync(statePath, 'utf8')).profiles[0].disabledEntries).toEqual(['include:keep-me'])
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
it('fixes an unreadable override file before recording a failing row in it', async () => {
|
|
149
|
+
const { dshHome, statePath } = makeHome({ state: '{corrupt', log: `${NESTED_FAILURE}\n` })
|
|
150
|
+
const plan = planRepairs(inspectProfile({ dshHome, profileName: 'main' }))
|
|
151
|
+
expect(plan.steps.map(s => s.recipe)).toEqual(['restore-override-file', 'disable-failing-row'])
|
|
152
|
+
await applyRepairPlan(plan)
|
|
153
|
+
expect(JSON.parse(readFileSync(statePath, 'utf8')).profiles[0].disabledEntries).toEqual(['acryl-broken'])
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
it('plans only the recipes asked for, and nothing when nothing matches', () => {
|
|
157
|
+
const { dshHome } = makeHome({ state: '{corrupt', log: `${NESTED_FAILURE}\n` })
|
|
158
|
+
const diagnosis = inspectProfile({ dshHome, profileName: 'main' })
|
|
159
|
+
expect(planRepairs(diagnosis, ['disable-failing-row']).steps.map(s => s.recipe)).toEqual(['disable-failing-row'])
|
|
160
|
+
expect(planRepairs(inspectProfile({ dshHome: makeHome().dshHome, profileName: 'main' })).steps).toEqual([])
|
|
161
|
+
expect(describePlan({ profileName: 'main', dshHome, steps: [] })).toBe('Nothing to repair.')
|
|
162
|
+
})
|
|
163
|
+
|
|
164
|
+
it('rolls back and leaves the file as it was when a step fails after the backup', async () => {
|
|
165
|
+
const good = JSON.stringify({ version: 1, profiles: [{ profileName: 'main', disabledEntries: ['include:keep'] }] })
|
|
166
|
+
const { dshHome, statePath } = makeHome({ state: good })
|
|
167
|
+
const finding = { severity: 'error', code: 'plugin-activation-failed', entryId: 'not a valid id!', recipe: 'disable-failing-row' } as const
|
|
168
|
+
const plan = planRepairs({ profileName: 'main', profileDir: join(dshHome, 'profiles', 'main'), dshHome, findings: [{ ...finding, message: 'x' }] })
|
|
169
|
+
await expect(applyRepairPlan(plan)).rejects.toThrow('rolled back')
|
|
170
|
+
expect(readFileSync(statePath, 'utf8')).toBe(good)
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
it('changes nothing when the backup itself cannot be made', async () => {
|
|
174
|
+
const { dshHome, statePath } = makeHome({ state: '{corrupt' })
|
|
175
|
+
const plan = planRepairs(inspectProfile({ dshHome, profileName: 'main' }))
|
|
176
|
+
rmSync(statePath)
|
|
177
|
+
mkdirSync(statePath)
|
|
178
|
+
await expect(applyRepairPlan(plan)).rejects.toThrow()
|
|
179
|
+
expect(statSync(statePath).isDirectory()).toBe(true)
|
|
180
|
+
expect(listBackups(dshHome)).toEqual([])
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('refuses to apply with nothing to do', async () => {
|
|
184
|
+
await expect(applyRepairPlan({ profileName: 'main', dshHome: makeHome().dshHome, steps: [] })).rejects.toThrow('nothing to repair')
|
|
185
|
+
})
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
describe('backups', () => {
|
|
189
|
+
it('back up existing files and remember which did not exist, so undo removes what a repair created', async () => {
|
|
190
|
+
const { dshHome, statePath } = makeHome({ state: 'before' })
|
|
191
|
+
const created = join(dshHome, 'created-by-repair.txt')
|
|
192
|
+
const backup = await createBackup({ dshHome, profileName: 'main', recipes: ['x'], files: [statePath, created] })
|
|
193
|
+
expect(backup.entries.map(e => e.existed)).toEqual([true, false])
|
|
194
|
+
writeFileSync(statePath, 'after')
|
|
195
|
+
writeFileSync(created, 'new')
|
|
196
|
+
await restoreBackup(dshHome, backup.id)
|
|
197
|
+
expect(readFileSync(statePath, 'utf8')).toBe('before')
|
|
198
|
+
expect(existsSync(created)).toBe(false)
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
it('never backs up or restores a file outside the ACRYL home', async () => {
|
|
202
|
+
const { dshHome } = makeHome()
|
|
203
|
+
await expect(createBackup({ dshHome, profileName: 'main', recipes: [], files: ['/etc/hosts'] })).rejects.toThrow('outside the ACRYL home')
|
|
204
|
+
await expect(createBackup({ dshHome, profileName: 'main', recipes: [], files: [join(dshHome, '..', 'elsewhere.txt')] })).rejects.toThrow('outside')
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
it('does not list a backup that was interrupted before its manifest was written', async () => {
|
|
208
|
+
const { dshHome, statePath } = makeHome({ state: 'x' })
|
|
209
|
+
const good = await createBackup({ dshHome, profileName: 'main', recipes: [], files: [statePath] })
|
|
210
|
+
mkdirSync(join(backupsDir(dshHome), '20260101T000000Z-dead'), { recursive: true })
|
|
211
|
+
writeFileSync(join(backupsDir(dshHome), '20260101T000000Z-dead', '0-state.json'), 'half')
|
|
212
|
+
expect(listBackups(dshHome).map(b => b.id)).toEqual([good.id])
|
|
213
|
+
await expect(restoreBackup(dshHome, '20260101T000000Z-dead')).rejects.toThrow('no complete backup')
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('refuses a backup whose stored content was tampered with, and changes nothing', async () => {
|
|
217
|
+
const { dshHome, statePath } = makeHome({ state: 'original' })
|
|
218
|
+
const backup = await createBackup({ dshHome, profileName: 'main', recipes: [], files: [statePath] })
|
|
219
|
+
writeFileSync(join(backupsDir(dshHome), backup.id, backup.entries[0]!.stored!), 'tampered')
|
|
220
|
+
writeFileSync(statePath, 'current')
|
|
221
|
+
await expect(restoreBackup(dshHome, backup.id)).rejects.toThrow('checksum mismatch')
|
|
222
|
+
expect(readFileSync(statePath, 'utf8')).toBe('current')
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
it('lists newest first', async () => {
|
|
226
|
+
const { dshHome, statePath } = makeHome({ state: 'x' })
|
|
227
|
+
const older = await createBackup({ dshHome, profileName: 'main', recipes: [], files: [statePath], now: () => new Date('2026-01-01T00:00:00Z') })
|
|
228
|
+
const newer = await createBackup({ dshHome, profileName: 'main', recipes: [], files: [statePath], now: () => new Date('2026-02-01T00:00:00Z') })
|
|
229
|
+
expect(listBackups(dshHome).map(b => b.id)).toEqual([newer.id, older.id])
|
|
230
|
+
})
|
|
231
|
+
})
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The profile resolution in src/profile-resolution/ is ported from DeepSeek Harness 0.2.0-rc.2. When the installed DeepSeek Harness or the
|
|
3
|
+
* pinned checkout moves, this fails until the port is redone against the new originals and the hashes below are updated with it.
|
|
4
|
+
*/
|
|
5
|
+
import { createHash } from 'node:crypto'
|
|
6
|
+
import { readFileSync } from 'node:fs'
|
|
7
|
+
import { createRequire } from 'node:module'
|
|
8
|
+
import { describe, expect, it } from 'vitest'
|
|
9
|
+
|
|
10
|
+
const ORIGINALS = {
|
|
11
|
+
'packages/boot/app-boot/src/profile-resolution/resolver.ts': 'cf3c2b79646cbacf2475db7841e7bf77c312c1e934f3b42ee87d18db58d806a1',
|
|
12
|
+
'packages/boot/app-boot/src/profile-resolution/service.ts': 'a6086e1d6f91118f803055fbf5a4cfa224f8793ef8ab5539678f38ea793d6ce0',
|
|
13
|
+
'packages/boot/app-boot/tests/profile-resolution.spec.ts': '2549038eb7128f9889f9ffd6a8c2715426494681a82dac5331d875211e10b604',
|
|
14
|
+
'packages/boot/app-boot/tests/profile-resolution-service.spec.ts': 'b46b4ed66f75baddad9030f28fd8254e1c2f120598ff637d48d4ae76c19e05dc',
|
|
15
|
+
'packages/boot/app-boot/tests/hooks-thread-stack.ts': '2e7e7859dc3bff52785caa21f69270470f642d5bd2fdc44c2804f1ccef46c1ca',
|
|
16
|
+
} as const
|
|
17
|
+
|
|
18
|
+
describe('profile resolution port', () => {
|
|
19
|
+
it('runs against the DeepSeek Harness version it was ported from', () => {
|
|
20
|
+
const manifest = createRequire(import.meta.url)('@deepseek-ai/dsh-app-boot/package.json') as { version: string }
|
|
21
|
+
expect(manifest.version).toBe('0.2.0-rc.2')
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('matches the originals in the pinned DeepSeek Harness checkout', () => {
|
|
25
|
+
for (const [path, sha256] of Object.entries(ORIGINALS)) {
|
|
26
|
+
const text = readFileSync(new URL(`../../../deepseek-harness/${path}`, import.meta.url))
|
|
27
|
+
expect(createHash('sha256').update(text).digest('hex'), path).toBe(sha256)
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
})
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Ported from DeepSeek Harness, packages/boot/app-boot/tests/profile-resolution-service.spec.ts at dsh-v0.2.0-rc.2 (639ed01539, MIT, Copyright (c) 2026 DeepSeek;
|
|
3
|
+
* see THIRD_PARTY_NOTICES.md). Deviation: it tests ACRYL's `ProfilePluginPackages`, imported under the original's name, and takes the types from @deepseek-ai/dsh-app-boot.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Package metadata queries share the active runtime resolution. */
|
|
7
|
+
|
|
8
|
+
import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from 'node:fs'
|
|
9
|
+
import { createRequire } from 'node:module'
|
|
10
|
+
import { tmpdir } from 'node:os'
|
|
11
|
+
import { dirname, join } from 'node:path'
|
|
12
|
+
import { pathToFileURL } from 'node:url'
|
|
13
|
+
import { getEnvironmentData } from 'node:worker_threads'
|
|
14
|
+
import { Context } from '@deepseek-ai/cordis'
|
|
15
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
16
|
+
import type { RuntimeResolution } from '@deepseek-ai/dsh-app-boot'
|
|
17
|
+
import { ProfilePluginPackages as PluginPackages } from '../src/profile-resolution/service.ts'
|
|
18
|
+
|
|
19
|
+
const roots: string[] = []
|
|
20
|
+
const contexts: Context[] = []
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
for (const context of contexts.splice(0).reverse()) await context.fiber.dispose()
|
|
24
|
+
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true })
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
function file(path: string, text: string): void {
|
|
28
|
+
mkdirSync(dirname(path), { recursive: true })
|
|
29
|
+
writeFileSync(path, text)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function pkg(dir: string, version: string, name = 'metadata-lib'): string {
|
|
33
|
+
file(join(dir, 'package.json'), JSON.stringify({
|
|
34
|
+
name,
|
|
35
|
+
version,
|
|
36
|
+
type: 'module',
|
|
37
|
+
exports: {
|
|
38
|
+
'.': { import: './index.js', require: './index.cjs' },
|
|
39
|
+
'./feature': './index.js',
|
|
40
|
+
'./feature/locale/*.json': './feature-locale/*.json',
|
|
41
|
+
},
|
|
42
|
+
}))
|
|
43
|
+
file(join(dir, 'index.js'), `export const version = ${JSON.stringify(version)}\n`)
|
|
44
|
+
file(join(dir, 'index.cjs'), `exports.version = ${JSON.stringify(version)}\n`)
|
|
45
|
+
return join(dir, 'package.json')
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function resolution(
|
|
49
|
+
profilesDir: string, profileDir: string, packageDir: string, declarer: string, version: string,
|
|
50
|
+
): RuntimeResolution {
|
|
51
|
+
return {
|
|
52
|
+
profilesDir,
|
|
53
|
+
profileDir,
|
|
54
|
+
localPackageNames: [],
|
|
55
|
+
linkedRoots: [],
|
|
56
|
+
entries: [{ name: 'metadata-lib', packageDir, version, declarer, scope: 'installation' }],
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
describe('profile package metadata service', () => {
|
|
61
|
+
it('reads translated metadata from the selected local package without importing its entry', async () => {
|
|
62
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-localized-package-service-'))
|
|
63
|
+
roots.push(root)
|
|
64
|
+
const packageDir = join(root, 'node_modules', 'localized')
|
|
65
|
+
file(join(packageDir, 'package.json'), JSON.stringify({
|
|
66
|
+
name: 'localized', exports: { './private': './index.js', './private/locale/*.json': './locale/*.json' },
|
|
67
|
+
}))
|
|
68
|
+
file(join(packageDir, 'index.js'), 'throw new Error("must not execute")\n')
|
|
69
|
+
file(join(packageDir, 'locale', 'en.json'), '{"meta":{"title":"Local plugin"}}')
|
|
70
|
+
file(join(packageDir, 'locale', 'zh.json'), '{"meta":{"title":"本地插件"}}')
|
|
71
|
+
const ctx = new Context()
|
|
72
|
+
contexts.push(ctx)
|
|
73
|
+
await ctx.plugin(PluginPackages)
|
|
74
|
+
const parent = pathToFileURL(join(root, 'entry.mjs')).href
|
|
75
|
+
expect(ctx.pluginPackages.metaOf('localized/private', parent)).toEqual({ title: { en: 'Local plugin', zh: '本地插件' } })
|
|
76
|
+
expect(ctx.pluginPackages.metaOf('localized', parent)).toBeUndefined()
|
|
77
|
+
expect(ctx.pluginPackages.metaOf('node:fs', parent)).toBeUndefined()
|
|
78
|
+
file(join(root, 'node_modules', 'invalid', 'package.json'), '{')
|
|
79
|
+
expect(ctx.pluginPackages.metaOf('invalid', parent)?.error).toContain('invalid')
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it('resolves module URLs and package metadata through the current resolution', async () => {
|
|
83
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-profile-package-service-'))
|
|
84
|
+
roots.push(root)
|
|
85
|
+
const profilesDir = join(root, 'profiles')
|
|
86
|
+
const profileDir = join(profilesDir, 'test')
|
|
87
|
+
const first = join(root, 'first')
|
|
88
|
+
const firstAnchor = pkg(first, '1.0.0')
|
|
89
|
+
file(join(first, 'feature-locale', 'en.json'), '{"meta":{"title":"Profile feature"}}')
|
|
90
|
+
file(join(profileDir, 'entry.mjs'), '')
|
|
91
|
+
const parentURL = pathToFileURL(join(profileDir, 'entry.mjs')).href
|
|
92
|
+
|
|
93
|
+
const ctx = new Context()
|
|
94
|
+
contexts.push(ctx)
|
|
95
|
+
ctx.baseUrl = pathToFileURL(profileDir).href + '/'
|
|
96
|
+
await ctx.plugin(PluginPackages, {
|
|
97
|
+
resolution: resolution(profilesDir, profileDir, first, firstAnchor, '1.0.0'),
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
expect(ctx.pluginPackages.packageOf('metadata-lib/private', parentURL)).toMatchObject({
|
|
101
|
+
name: 'metadata-lib',
|
|
102
|
+
version: '1.0.0',
|
|
103
|
+
dir: first,
|
|
104
|
+
})
|
|
105
|
+
const require = createRequire(join(profileDir, 'entry.cjs'))
|
|
106
|
+
expect(require.resolve('metadata-lib')).toBe(realpathSync(join(first, 'index.cjs')))
|
|
107
|
+
expect(ctx.pluginPackages.metaOf('metadata-lib/feature', parentURL)).toEqual({ title: { en: 'Profile feature' } })
|
|
108
|
+
expect(ctx.pluginPackages.packageOf('node:fs', parentURL)).toBeUndefined()
|
|
109
|
+
expect(ctx.pluginPackages.packageOf('./local.js', parentURL)).toBeUndefined()
|
|
110
|
+
|
|
111
|
+
await ctx.fiber.dispose()
|
|
112
|
+
contexts.pop()
|
|
113
|
+
expect(() => { require.resolve('metadata-lib') }).toThrow(/Cannot find module/u)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('uses native package lookup without a registration and caches parsed metadata', async () => {
|
|
117
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-native-package-service-'))
|
|
118
|
+
roots.push(root)
|
|
119
|
+
const packageDir = join(root, 'node_modules', '@scope', 'metadata')
|
|
120
|
+
file(join(packageDir, 'package.json'), JSON.stringify({ name: '@scope/metadata' }))
|
|
121
|
+
const parentURL = pathToFileURL(join(root, 'entry.mjs')).href
|
|
122
|
+
const ctx = new Context()
|
|
123
|
+
contexts.push(ctx)
|
|
124
|
+
await ctx.plugin(PluginPackages)
|
|
125
|
+
|
|
126
|
+
const first = ctx.pluginPackages.packageOf('@scope/metadata/subpath', parentURL)
|
|
127
|
+
expect(first).toMatchObject({ name: '@scope/metadata', version: undefined, dir: packageDir })
|
|
128
|
+
expect(ctx.pluginPackages.packageOf('@scope/metadata', parentURL)).toBe(first)
|
|
129
|
+
expect(ctx.pluginPackages.packageOf('missing-package', parentURL)).toBeUndefined()
|
|
130
|
+
expect(ctx.pluginPackages.packageOf('@scope', parentURL)).toBeUndefined()
|
|
131
|
+
expect(() => { ctx.pluginPackages.replace({
|
|
132
|
+
profilesDir: join(root, 'profiles'), profileDir: undefined, localPackageNames: [], linkedRoots: [], entries: [],
|
|
133
|
+
}) }).toThrow(/runtime resolution is not installed/u)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('rejects malformed package metadata selected by the resolver', async () => {
|
|
137
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-invalid-package-service-'))
|
|
138
|
+
roots.push(root)
|
|
139
|
+
const anonymous = join(root, 'node_modules', 'anonymous')
|
|
140
|
+
file(join(anonymous, 'package.json'), '{}')
|
|
141
|
+
const ctx = new Context()
|
|
142
|
+
contexts.push(ctx)
|
|
143
|
+
await ctx.plugin(PluginPackages)
|
|
144
|
+
const parentURL = pathToFileURL(join(root, 'entry.mjs')).href
|
|
145
|
+
expect(ctx.pluginPackages.packageOf('missing', parentURL)).toBeUndefined()
|
|
146
|
+
expect(ctx.pluginPackages.packageOf('anonymous', parentURL)).toMatchObject({
|
|
147
|
+
name: 'anonymous',
|
|
148
|
+
version: undefined,
|
|
149
|
+
dir: anonymous,
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('returns undefined when a selected package directory has no manifest', async () => {
|
|
154
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-missing-package-service-'))
|
|
155
|
+
roots.push(root)
|
|
156
|
+
const profilesDir = join(root, 'profiles')
|
|
157
|
+
const profileDir = join(profilesDir, 'test')
|
|
158
|
+
const missing = join(root, 'missing')
|
|
159
|
+
const ctx = new Context()
|
|
160
|
+
contexts.push(ctx)
|
|
161
|
+
await ctx.plugin(PluginPackages, {
|
|
162
|
+
resolution: resolution(profilesDir, profileDir, missing, join(root, 'owner.json'), '1.0.0'),
|
|
163
|
+
})
|
|
164
|
+
expect(ctx.pluginPackages.packageOf(
|
|
165
|
+
'metadata-lib', pathToFileURL(join(profileDir, 'entry.mjs')).href,
|
|
166
|
+
)).toBeUndefined()
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
it('removes and restores linked roots while rejecting changes to their historical targets', async () => {
|
|
170
|
+
const root = realpathSync.native(mkdtempSync(join(tmpdir(), 'dsh-linked-package-service-')))
|
|
171
|
+
roots.push(root)
|
|
172
|
+
const profilesDir = join(root, 'profiles')
|
|
173
|
+
const profileDir = join(profilesDir, 'test')
|
|
174
|
+
const runtime = join(root, 'runtime')
|
|
175
|
+
const runtimeAnchor = pkg(runtime, '1.0.0')
|
|
176
|
+
const linkedA = join(root, 'work', 'a')
|
|
177
|
+
const linkedB = join(root, 'work', 'b')
|
|
178
|
+
for (const dir of [linkedA, linkedB]) {
|
|
179
|
+
file(join(dir, 'package.json'), JSON.stringify({
|
|
180
|
+
name: 'linked-plugin', peerDependencies: { 'metadata-lib': '*' },
|
|
181
|
+
}))
|
|
182
|
+
}
|
|
183
|
+
const local = join(linkedA, 'node_modules', 'metadata-lib')
|
|
184
|
+
pkg(local, '2.0.0')
|
|
185
|
+
const parentPath = join(linkedA, 'entry.mjs')
|
|
186
|
+
file(parentPath, '')
|
|
187
|
+
const parentURL = pathToFileURL(parentPath).href
|
|
188
|
+
const require = createRequire(parentURL)
|
|
189
|
+
const linked = { name: 'linked-plugin', realPath: linkedA }
|
|
190
|
+
const initial: RuntimeResolution = {
|
|
191
|
+
...resolution(profilesDir, profileDir, runtime, runtimeAnchor, '1.0.0'),
|
|
192
|
+
linkedRoots: [linked],
|
|
193
|
+
}
|
|
194
|
+
const removed: RuntimeResolution = { ...initial, linkedRoots: [] }
|
|
195
|
+
const relinked: RuntimeResolution = { ...initial, linkedRoots: [{ ...linked, realPath: linkedB }] }
|
|
196
|
+
const key = '@deepseek-ai/dsh-app-boot/profile-resolution'
|
|
197
|
+
const previous = getEnvironmentData(key)
|
|
198
|
+
const ctx = new Context()
|
|
199
|
+
contexts.push(ctx)
|
|
200
|
+
await ctx.plugin(PluginPackages, { resolution: initial })
|
|
201
|
+
|
|
202
|
+
const expectSelectedPackage = (dir: string, version: string): void => {
|
|
203
|
+
expect(ctx.pluginPackages.packageOf('metadata-lib', parentURL)).toMatchObject({
|
|
204
|
+
name: 'metadata-lib', version, dir,
|
|
205
|
+
})
|
|
206
|
+
expect(require.resolve('metadata-lib')).toBe(realpathSync.native(join(dir, 'index.cjs')))
|
|
207
|
+
}
|
|
208
|
+
expectSelectedPackage(runtime, '1.0.0')
|
|
209
|
+
expect(getEnvironmentData(key)).toEqual({ resolution: initial })
|
|
210
|
+
|
|
211
|
+
ctx.pluginPackages.replace(removed)
|
|
212
|
+
expectSelectedPackage(local, '2.0.0')
|
|
213
|
+
expect(getEnvironmentData(key)).toEqual({ resolution: removed })
|
|
214
|
+
|
|
215
|
+
ctx.pluginPackages.replace(initial)
|
|
216
|
+
expectSelectedPackage(runtime, '1.0.0')
|
|
217
|
+
expect(getEnvironmentData(key)).toEqual({ resolution: initial })
|
|
218
|
+
|
|
219
|
+
ctx.pluginPackages.replace(removed)
|
|
220
|
+
expectSelectedPackage(local, '2.0.0')
|
|
221
|
+
const removedWorkerData = getEnvironmentData(key)
|
|
222
|
+
expect(removedWorkerData).toEqual({ resolution: removed })
|
|
223
|
+
expect(() => { ctx.pluginPackages.replace(relinked) }).toThrow(
|
|
224
|
+
'profile resolution: relinking "linked-plugin" requires a process restart',
|
|
225
|
+
)
|
|
226
|
+
expectSelectedPackage(local, '2.0.0')
|
|
227
|
+
expect(getEnvironmentData(key)).toBe(removedWorkerData)
|
|
228
|
+
|
|
229
|
+
await ctx.fiber.dispose()
|
|
230
|
+
contexts.pop()
|
|
231
|
+
expect(getEnvironmentData(key)).toBe(previous)
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
it('publishes additive generations to the process and future Workers', async () => {
|
|
235
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-package-service-resolution-'))
|
|
236
|
+
roots.push(root)
|
|
237
|
+
const profilesDir = join(root, 'profiles')
|
|
238
|
+
const profileDir = join(profilesDir, 'test')
|
|
239
|
+
const first = join(root, 'first')
|
|
240
|
+
const firstAnchor = pkg(first, '1.0.0')
|
|
241
|
+
const initial = resolution(profilesDir, profileDir, first, firstAnchor, '1.0.0')
|
|
242
|
+
const key = '@deepseek-ai/dsh-app-boot/profile-resolution'
|
|
243
|
+
const previous = getEnvironmentData(key)
|
|
244
|
+
const ctx = new Context()
|
|
245
|
+
contexts.push(ctx)
|
|
246
|
+
await ctx.plugin(PluginPackages, { resolution: initial })
|
|
247
|
+
const initialWorkerData = getEnvironmentData(key) as {
|
|
248
|
+
resolution: RuntimeResolution
|
|
249
|
+
}
|
|
250
|
+
expect(initialWorkerData).toEqual({ resolution: initial })
|
|
251
|
+
|
|
252
|
+
const added = join(root, 'added')
|
|
253
|
+
const addedAnchor = pkg(added, '2.0.0', 'added-metadata')
|
|
254
|
+
const next = {
|
|
255
|
+
...initial,
|
|
256
|
+
entries: [...initial.entries, {
|
|
257
|
+
name: 'added-metadata', packageDir: added, version: '2.0.0',
|
|
258
|
+
declarer: addedAnchor, scope: 'installation' as const,
|
|
259
|
+
}],
|
|
260
|
+
}
|
|
261
|
+
ctx.pluginPackages.replace(next)
|
|
262
|
+
expect(getEnvironmentData(key)).toEqual({
|
|
263
|
+
resolution: next,
|
|
264
|
+
})
|
|
265
|
+
expect(ctx.pluginPackages.packageOf(
|
|
266
|
+
'added-metadata', pathToFileURL(join(profileDir, 'entry.mjs')).href,
|
|
267
|
+
)).toMatchObject({ name: 'added-metadata', version: '2.0.0', dir: added })
|
|
268
|
+
|
|
269
|
+
await ctx.fiber.dispose()
|
|
270
|
+
contexts.pop()
|
|
271
|
+
expect(getEnvironmentData(key)).toBe(previous)
|
|
272
|
+
})
|
|
273
|
+
})
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The profile resolution the DSH engine installs on Node's loaders (module hooks and a `Module._resolveFilename` wrapper) belongs to the
|
|
3
|
+
* engine's activation: swapping the engine away removes both, and swapping it back installs exactly one layer again. Counted through
|
|
4
|
+
* `Module.registerHooks`, which src/module-hooks.ts reads at call time, and checked by identity on `_resolveFilename`.
|
|
5
|
+
*/
|
|
6
|
+
import { mkdtemp, rm } from 'node:fs/promises'
|
|
7
|
+
import Module from 'node:module'
|
|
8
|
+
import { tmpdir } from 'node:os'
|
|
9
|
+
import { join } from 'node:path'
|
|
10
|
+
import { afterEach, describe, expect, it } from 'vitest'
|
|
11
|
+
import { createDshEngineDefinition } from '../src/engine-dsh.ts'
|
|
12
|
+
import { createAcrylEngineHost } from '../src/engine-host.ts'
|
|
13
|
+
import { codingCapabilities } from '@acryldev/coding-capabilities'
|
|
14
|
+
|
|
15
|
+
type Register = (options: unknown) => { deregister(): void }
|
|
16
|
+
const nodeModule = Module as unknown as { registerHooks: Register, _resolveFilename: unknown }
|
|
17
|
+
const originalRegister = nodeModule.registerHooks
|
|
18
|
+
const initialDshHome = process.env.DSH_HOME
|
|
19
|
+
const homes: string[] = []
|
|
20
|
+
|
|
21
|
+
afterEach(async () => {
|
|
22
|
+
nodeModule.registerHooks = originalRegister
|
|
23
|
+
if (initialDshHome === undefined) delete process.env.DSH_HOME
|
|
24
|
+
else process.env.DSH_HOME = initialDshHome
|
|
25
|
+
await Promise.all(homes.splice(0).map(home => rm(home, { force: true, recursive: true })))
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe('profile resolution across engine swaps', () => {
|
|
29
|
+
it('removes its module hooks and resolver wrapper when the DSH engine is swapped away, and installs one layer when it comes back', async () => {
|
|
30
|
+
let live = 0
|
|
31
|
+
nodeModule.registerHooks = options => {
|
|
32
|
+
const hooks = originalRegister(options)
|
|
33
|
+
live += 1
|
|
34
|
+
let deregistered = false
|
|
35
|
+
return {
|
|
36
|
+
deregister() {
|
|
37
|
+
if (!deregistered) { deregistered = true; live -= 1 }
|
|
38
|
+
hooks.deregister()
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const home = await mkdtemp(join(tmpdir(), 'acryl-resolution-swap-'))
|
|
43
|
+
homes.push(home)
|
|
44
|
+
process.env.DSH_HOME = home
|
|
45
|
+
const nativeResolveFilename = nodeModule._resolveFilename
|
|
46
|
+
|
|
47
|
+
const host = await createAcrylEngineHost({
|
|
48
|
+
engines: [createDshEngineDefinition('acryl-swap', codingCapabilities), { id: 'other', plugin: () => {} }],
|
|
49
|
+
initialEngine: 'dsh',
|
|
50
|
+
})
|
|
51
|
+
try {
|
|
52
|
+
const mounted = live
|
|
53
|
+
expect(mounted).toBeGreaterThan(0)
|
|
54
|
+
expect(nodeModule._resolveFilename).not.toBe(nativeResolveFilename)
|
|
55
|
+
for (let cycle = 0; cycle < 3; cycle += 1) {
|
|
56
|
+
await host.select('other')
|
|
57
|
+
expect(live, `hooks left after swap ${String(cycle + 1)}`).toBe(0)
|
|
58
|
+
expect(nodeModule._resolveFilename).toBe(nativeResolveFilename)
|
|
59
|
+
await host.select('dsh')
|
|
60
|
+
expect(live, `hooks after remount ${String(cycle + 1)}`).toBe(mounted)
|
|
61
|
+
expect(host.ctx.get('sessions')).toBeDefined()
|
|
62
|
+
}
|
|
63
|
+
} finally {
|
|
64
|
+
await host.dispose()
|
|
65
|
+
}
|
|
66
|
+
expect(live).toBe(0)
|
|
67
|
+
expect(nodeModule._resolveFilename).toBe(nativeResolveFilename)
|
|
68
|
+
}, 120_000)
|
|
69
|
+
})
|