dsh-multi-device-config-sync 0.0.0-stage → 1.6.0
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 +105 -2
- package/assets/icon-256.png +0 -0
- package/client.js +1658 -0
- package/cordis.patch.yml +3 -0
- package/icon.svg +19 -0
- package/index.js +52 -0
- package/lib/backup.js +1087 -0
- package/lib/errors.js +67 -0
- package/lib/gist.js +343 -0
- package/lib/plugins.js +490 -0
- package/lib/routes.js +507 -0
- package/lib/store.js +73 -0
- package/lib/sync-install.js +231 -0
- package/lib/tool.js +191 -0
- package/locale/en.json +24 -0
- package/locale/zh.json +24 -0
- package/package.json +66 -4
- package/test/activation.test.mjs +141 -0
- package/test/client-decisions.test.mjs +55 -0
- package/test/client-registration.test.mjs +98 -0
- package/test/config-preview.test.mjs +454 -0
- package/test/first-run.test.mjs +104 -0
- package/test/harness.mjs +176 -0
- package/test/library.test.mjs +169 -0
- package/test/package.test.mjs +165 -0
- package/test/plugins.test.mjs +543 -0
- package/test/progress-client.test.mjs +225 -0
- package/test/progress-engine.test.mjs +235 -0
- package/test/progress-http.test.mjs +340 -0
- package/test/rename-compat.test.mjs +255 -0
- package/test/run.mjs +32 -0
- package/test/sync-install.test.mjs +338 -0
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
/** Cloud configuration-only plans: the real route handler, no sockets or package manager. */
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { PassThrough, Readable } from 'node:stream'
|
|
6
|
+
import {
|
|
7
|
+
backupDocument, createReport, makeProfile, stubGithub, tempDir, TEST_GIST_ID as ID, TEST_TOKEN, useProfile,
|
|
8
|
+
} from './harness.mjs'
|
|
9
|
+
|
|
10
|
+
const { check, finish } = createReport('configuration-only plans')
|
|
11
|
+
const home = tempDir('dpg-config-preview-')
|
|
12
|
+
useProfile(join(home, 'profiles', 'test'), home)
|
|
13
|
+
process.env.HOME = process.env.USERPROFILE = home
|
|
14
|
+
process.env.PATH = join(home, 'no-executables')
|
|
15
|
+
for (const key of ['DSH_GITHUB_TOKEN', 'GITHUB_TOKEN', 'GH_TOKEN']) delete process.env[key]
|
|
16
|
+
const realFetch = globalThis.fetch
|
|
17
|
+
globalThis.fetch = async () => { throw new Error('unexpected network access from an isolated test') }
|
|
18
|
+
const { BASE_PATH, registerRoutes } = await import('../lib/routes.js')
|
|
19
|
+
const { WORK_DIR } = await import('../lib/backup.js')
|
|
20
|
+
const { readSettings } = await import('../lib/store.js')
|
|
21
|
+
const source = { id: ID, token: TEST_TOKEN }
|
|
22
|
+
const other = 'another-upload-target'
|
|
23
|
+
const npmrc = 'registry=https://reviewed.invalid/\n'
|
|
24
|
+
const workspace = 'packages: []\n# reviewed policy\n'
|
|
25
|
+
const cloudPlugin = 'reviewed-plugin'
|
|
26
|
+
const credential = 'data/auths/credentials.sqlite'
|
|
27
|
+
|
|
28
|
+
// Compare every file, including hidden history, undo state and the dependency tree.
|
|
29
|
+
function tree(dir) {
|
|
30
|
+
return Object.fromEntries(readdirSync(dir, { withFileTypes: true }).map(entry => {
|
|
31
|
+
const path = join(dir, entry.name)
|
|
32
|
+
return [entry.name, entry.isDirectory() ? tree(path) : readFileSync(path, 'latin1')]
|
|
33
|
+
}))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Exercise the same prefix handler and admission/body/lock paths as the browser,
|
|
37
|
+
// but feed the HTTP stream in process. A PassThrough lets a test hold a write open.
|
|
38
|
+
function request(handler, path, body, headers) {
|
|
39
|
+
const req = body instanceof Readable ? body : Readable.from(body === undefined ? [] : [JSON.stringify(body)])
|
|
40
|
+
Object.assign(req, { url: `${BASE_PATH}/api/${path}`, method: body === undefined ? 'GET' : 'POST', headers: { host: 'localhost', ...headers } })
|
|
41
|
+
return new Promise(resolve => {
|
|
42
|
+
let status
|
|
43
|
+
handler(req, { writeHead(code) { status = code }, end(text) { resolve({ status, payload: JSON.parse(text) }) } })
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
async function expect(response, status, code) {
|
|
47
|
+
const result = await response
|
|
48
|
+
assert.equal(result.status, status, JSON.stringify(result.payload).slice(0, 260))
|
|
49
|
+
if (code) assert.equal(result.payload.code, code)
|
|
50
|
+
return result.payload
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let serial = 0
|
|
54
|
+
async function scenario(label, test) {
|
|
55
|
+
const profile = makeProfile(join(home, 'profiles', `case-${++serial}`))
|
|
56
|
+
useProfile(profile, home)
|
|
57
|
+
const document = backupDocument('sender', [
|
|
58
|
+
{ path: '.npmrc', lines: npmrc.split('\n') },
|
|
59
|
+
{ path: 'pnpm-workspace.yaml', lines: workspace.split('\n') },
|
|
60
|
+
{ path: 'helper.cjs', lines: ['// restored as text only', ''] },
|
|
61
|
+
{ path: credential, lines: ['fixture that the validator must omit'] },
|
|
62
|
+
])
|
|
63
|
+
Object.assign(document.files[0].json, {
|
|
64
|
+
dependencies: { [cloudPlugin]: '1.2.3' }, dsh: { profile: { bundles: [cloudPlugin] } }, scripts: { test: 'reviewed-marker' },
|
|
65
|
+
})
|
|
66
|
+
const repo = stubGithub({ gistId: ID, document })
|
|
67
|
+
let handler
|
|
68
|
+
const calls = []
|
|
69
|
+
const services = {
|
|
70
|
+
webServer: { register(route) { handler = route.handler; return () => { handler = null } } },
|
|
71
|
+
connection: { requestRejection: req => req.headers.denied ? 401 : undefined },
|
|
72
|
+
pluginManager: {
|
|
73
|
+
listPlugins() { calls.push('listPlugins'); return [] },
|
|
74
|
+
listBundles() { calls.push('listBundles'); return [{ name: cloudPlugin, availability: 'profile', installed: true }] },
|
|
75
|
+
installBundle() { calls.push('installBundle'); throw new Error('no installations in this fixture') },
|
|
76
|
+
},
|
|
77
|
+
}
|
|
78
|
+
const ctx = { get(name) { if (name === 'pluginManager') calls.push('lookup'); return services[name] } }
|
|
79
|
+
let dispose = registerRoutes(ctx)
|
|
80
|
+
const remount = () => { dispose(); dispose = registerRoutes(ctx) }
|
|
81
|
+
const api = (path, body, headers) => request(handler, path, body, headers)
|
|
82
|
+
const preview = (mode = 'config', from = source) => expect(api(`${mode}/preview`, from), 200)
|
|
83
|
+
const confirm = (planId, mode = 'config') => api(mode === 'config' ? 'config/restore' : 'sync/install', { planId, confirm: true })
|
|
84
|
+
try {
|
|
85
|
+
await test({ profile, document, repo, services, calls, remount, api, preview, confirm })
|
|
86
|
+
check(label, true)
|
|
87
|
+
} catch (error) {
|
|
88
|
+
check(label, false, error.stack)
|
|
89
|
+
} finally {
|
|
90
|
+
dispose()
|
|
91
|
+
repo.restore()
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
await scenario('read-only full-scope preview and confirmation use one exact Gist snapshot and no plugin manager', async ({ profile, document, repo, services, calls, preview, confirm }) => {
|
|
96
|
+
services.pluginManager = undefined // still confirmable, unlike the combined flow
|
|
97
|
+
const before = tree(profile)
|
|
98
|
+
const plan = await preview()
|
|
99
|
+
assert.deepEqual(Object.keys(plan).sort(), ['planId', 'preview', 'ref'])
|
|
100
|
+
assert.ok(typeof plan.planId === 'string' && plan.planId)
|
|
101
|
+
assert.equal(plan.ref.id, ID)
|
|
102
|
+
assert.deepEqual(Object.fromEntries(plan.preview.files.map(f => [f.path, f.status])), {
|
|
103
|
+
'package.json': 'merge', '.npmrc': 'overwrite', 'pnpm-workspace.yaml': 'overwrite', 'helper.cjs': 'add',
|
|
104
|
+
})
|
|
105
|
+
assert.deepEqual(plan.preview.manifest.dependenciesAdded, [cloudPlugin])
|
|
106
|
+
assert.deepEqual(plan.preview.dropped, [{ path: credential, code: 'credential' }])
|
|
107
|
+
assert.deepEqual(tree(profile), before)
|
|
108
|
+
|
|
109
|
+
document.files[0].json.dependencies[cloudPlugin] = '9.9.9'
|
|
110
|
+
document.files.find(f => f.path === '.npmrc').lines = ['changed on GitHub after preview']
|
|
111
|
+
const result = await expect(confirm(plan.planId), 200)
|
|
112
|
+
assert.deepEqual(Object.keys(result).sort(), ['ref', 'report', 'restart', 'warnings'])
|
|
113
|
+
assert.deepEqual(result.ref, plan.ref)
|
|
114
|
+
for (const field of ['files', 'counts', 'manifest', 'dropped', 'droppedEntries']) assert.deepEqual(result.report[field], plan.preview[field], field)
|
|
115
|
+
assert.deepEqual(result.warnings, [])
|
|
116
|
+
assert.equal(result.restart, true)
|
|
117
|
+
assert.equal(readFileSync(join(profile, '.npmrc'), 'utf8'), npmrc)
|
|
118
|
+
assert.equal(readFileSync(join(profile, 'pnpm-workspace.yaml'), 'utf8'), workspace)
|
|
119
|
+
assert.equal(readFileSync(join(profile, 'helper.cjs'), 'utf8'), '// restored as text only\n')
|
|
120
|
+
const manifest = JSON.parse(readFileSync(join(profile, 'package.json'), 'utf8'))
|
|
121
|
+
assert.deepEqual(manifest.dependencies, { 'some-plugin': '1.2.3', [cloudPlugin]: '1.2.3' })
|
|
122
|
+
assert.deepEqual(manifest.scripts, { test: 'reviewed-marker' })
|
|
123
|
+
assert.deepEqual(manifest.dsh.profile.bundles, ['@deepseek-ai/dsh-base', cloudPlugin])
|
|
124
|
+
const after = tree(profile)
|
|
125
|
+
assert.deepEqual(after.node_modules, before.node_modules)
|
|
126
|
+
assert.equal(after.data.auths['credentials.sqlite'], before.data.auths['credentials.sqlite'])
|
|
127
|
+
assert.ok(existsSync(join(result.report.undoDir, 'manifest.json')))
|
|
128
|
+
assert.ok(!JSON.stringify([plan, result, after]).includes(TEST_TOKEN))
|
|
129
|
+
await expect(confirm(plan.planId), 409, 'stale-plan')
|
|
130
|
+
assert.deepEqual(tree(profile), after, 'no second restore, undo point or history record')
|
|
131
|
+
assert.deepEqual(calls, [])
|
|
132
|
+
assert.deepEqual(repo.calls.map(({ method, path, headers }) => [method, path, headers.authorization]), [['GET', `/gists/${ID}`, `Bearer ${TEST_TOKEN}`]])
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
await scenario('each mode refuses the other mode’s ID and shows only its own file policy', async ({ profile, repo, calls, preview, confirm }) => {
|
|
136
|
+
const before = tree(profile)
|
|
137
|
+
const full = await preview()
|
|
138
|
+
const combined = await preview('sync')
|
|
139
|
+
assert.ok(combined.planId && combined.planId !== full.planId)
|
|
140
|
+
for (const path of ['.npmrc', 'pnpm-workspace.yaml', 'helper.cjs']) {
|
|
141
|
+
assert.ok(full.preview.files.some(file => file.path === path))
|
|
142
|
+
assert.ok(combined.preview.dropped.some(file => file.path === path && file.code === 'local-policy'))
|
|
143
|
+
assert.ok(!combined.preview.files.some(file => file.path === path))
|
|
144
|
+
}
|
|
145
|
+
const afterPreview = [...calls]
|
|
146
|
+
await expect(confirm(combined.planId), 409, 'stale-plan')
|
|
147
|
+
await expect(confirm(full.planId, 'sync'), 409, 'stale-plan')
|
|
148
|
+
assert.deepEqual(tree(profile), before)
|
|
149
|
+
await expect(confirm(full.planId), 200) // the wrong endpoint did not spend the right plan
|
|
150
|
+
assert.deepEqual(calls, afterPreview)
|
|
151
|
+
assert.equal(repo.calls.length, 2)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
await scenario('admission, invalid sources and missing explicit consent cannot write or consume a valid plan', async ({ profile, repo, document, api, preview, confirm, calls }) => {
|
|
155
|
+
const before = tree(profile)
|
|
156
|
+
await expect(api('config/preview', source, { denied: 'yes' }), 401, 'auth')
|
|
157
|
+
await expect(api('config/preview', {}), 400, 'no-cloud')
|
|
158
|
+
await expect(api('config/preview', { ...source, id: 'not a gist!' }), 400, 'invalid')
|
|
159
|
+
assert.equal(repo.calls.length, 0, 'admission and ID checks precede downloading')
|
|
160
|
+
const { planId } = await preview()
|
|
161
|
+
await expect(api('config/restore', { planId, confirm: true }, { denied: 'yes' }), 401, 'auth')
|
|
162
|
+
await expect(api('config/restore', { planId, confirm: true }, { 'sec-fetch-site': 'cross-site' }), 403, 'auth')
|
|
163
|
+
for (const value of [undefined, false, 'true']) await expect(api('config/restore', { planId, confirm: value }), 400, 'invalid')
|
|
164
|
+
await expect(confirm('unknown-plan'), 409, 'stale-plan')
|
|
165
|
+
assert.equal(repo.calls.length, 1)
|
|
166
|
+
assert.deepEqual(tree(profile), before)
|
|
167
|
+
await expect(confirm(planId), 200)
|
|
168
|
+
const after = tree(profile)
|
|
169
|
+
document.files = []
|
|
170
|
+
const invalid = await expect(api('config/preview', source), 400, 'invalid')
|
|
171
|
+
assert.equal(invalid.planId, undefined)
|
|
172
|
+
assert.deepEqual(tree(profile), after)
|
|
173
|
+
assert.deepEqual(calls, [])
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
await scenario('a new same-mode preview supersedes the old ID; expiry is enforced at exactly five minutes', async ({ profile, preview, confirm, repo }) => {
|
|
177
|
+
const before = tree(profile)
|
|
178
|
+
const clock = Date.now
|
|
179
|
+
let time = clock()
|
|
180
|
+
Date.now = () => time
|
|
181
|
+
try {
|
|
182
|
+
const old = await preview()
|
|
183
|
+
const fresh = await preview()
|
|
184
|
+
await expect(confirm(old.planId), 409, 'stale-plan')
|
|
185
|
+
time += 5 * 60_000
|
|
186
|
+
await expect(confirm(fresh.planId), 409, 'stale-plan')
|
|
187
|
+
time -= 1
|
|
188
|
+
await expect(confirm(fresh.planId), 409, 'stale-plan') // failed attempt already consumed
|
|
189
|
+
} finally { Date.now = clock }
|
|
190
|
+
assert.deepEqual(tree(profile), before)
|
|
191
|
+
assert.equal(repo.calls.length, 2)
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
for (const path of ['.npmrc', 'helper.cjs']) {
|
|
195
|
+
await scenario(`a changed policy file or newly present target invalidates the reviewed bytes: ${path}`, async ({ profile, preview, confirm, repo, calls }) => {
|
|
196
|
+
const { planId } = await preview()
|
|
197
|
+
const target = join(profile, path)
|
|
198
|
+
const bytes = existsSync(target) ? readFileSync(target, 'utf8') : null
|
|
199
|
+
writeFileSync(target, bytes === null ? 'appeared since preview' : bytes.replace('registry', 'Registry')) // same length for an existing target
|
|
200
|
+
const edited = tree(profile)
|
|
201
|
+
await expect(confirm(planId), 409, 'stale-plan')
|
|
202
|
+
assert.deepEqual(tree(profile), edited)
|
|
203
|
+
if (bytes !== null) writeFileSync(target, bytes)
|
|
204
|
+
await expect(confirm(planId), 409, 'stale-plan')
|
|
205
|
+
assert.deepEqual(calls, [])
|
|
206
|
+
assert.equal(repo.calls.length, 1)
|
|
207
|
+
})
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
await scenario('both confirmations use the existing write lock and release it on failures and success', async ({ profile, api, preview, confirm, calls }) => {
|
|
211
|
+
const { planId } = await preview()
|
|
212
|
+
const before = tree(profile)
|
|
213
|
+
const heldCombined = new PassThrough()
|
|
214
|
+
const first = api('sync/install', heldCombined)
|
|
215
|
+
await expect(confirm(planId), 409, 'busy')
|
|
216
|
+
heldCombined.end('{}')
|
|
217
|
+
await expect(first, 400, 'invalid')
|
|
218
|
+
const heldConfig = new PassThrough()
|
|
219
|
+
const second = api('config/restore', heldConfig)
|
|
220
|
+
await expect(confirm(planId, 'sync'), 409, 'busy')
|
|
221
|
+
await expect(confirm(planId), 409, 'busy')
|
|
222
|
+
await expect(api('undo', {}), 409, 'busy')
|
|
223
|
+
assert.deepEqual(tree(profile), before)
|
|
224
|
+
heldConfig.end(JSON.stringify({ planId, confirm: true }))
|
|
225
|
+
await expect(second, 200)
|
|
226
|
+
await expect(confirm(planId), 409, 'stale-plan')
|
|
227
|
+
await expect(api('undo', {}), 200)
|
|
228
|
+
assert.deepEqual(calls, [])
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
await scenario('unload invalidates all slots and prevents an already admitted, unread confirmation from writing', async ({ profile, remount, api, document, preview, confirm }) => {
|
|
232
|
+
const before = tree(profile)
|
|
233
|
+
const full = await preview()
|
|
234
|
+
const combined = await preview('sync')
|
|
235
|
+
const local = await expect(api('preview', { reviewed: true, backup: document }), 200)
|
|
236
|
+
const body = new PassThrough()
|
|
237
|
+
const inFlight = api('config/restore', body)
|
|
238
|
+
remount()
|
|
239
|
+
body.end(JSON.stringify({ planId: full.planId, confirm: true }))
|
|
240
|
+
await expect(inFlight, 409, 'stale-plan')
|
|
241
|
+
await expect(confirm(full.planId), 409, 'stale-plan')
|
|
242
|
+
await expect(confirm(combined.planId, 'sync'), 409, 'stale-plan')
|
|
243
|
+
await expect(api('restore', { planId: local.planId, confirm: true }), 409, 'stale-plan')
|
|
244
|
+
assert.deepEqual(tree(profile), before)
|
|
245
|
+
})
|
|
246
|
+
|
|
247
|
+
await scenario('a failed restore cannot update history, run the installer or reuse its ID', async ({ profile, calls, repo, preview, confirm }) => {
|
|
248
|
+
const { planId } = await preview()
|
|
249
|
+
writeFileSync(join(profile, WORK_DIR), 'fixture: cannot make an undo directory here')
|
|
250
|
+
const blocked = tree(profile)
|
|
251
|
+
await expect(confirm(planId), 500, 'other')
|
|
252
|
+
await expect(confirm(planId), 409, 'stale-plan')
|
|
253
|
+
assert.deepEqual(tree(profile), blocked)
|
|
254
|
+
assert.deepEqual(calls, [])
|
|
255
|
+
assert.equal(repo.calls.length, 1)
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
await scenario('failed history persistence returns a warning and a usable undo point, not a false restore failure', async ({ profile, api, preview, confirm, calls }) => {
|
|
259
|
+
mkdirSync(join(profile, WORK_DIR, 'settings.json'), { recursive: true }) // the history rename must fail
|
|
260
|
+
const oldPolicy = readFileSync(join(profile, '.npmrc'), 'utf8')
|
|
261
|
+
const { planId } = await preview()
|
|
262
|
+
const result = await expect(confirm(planId), 200)
|
|
263
|
+
assert.ok(result.warnings.some(w => /configuration restored.*history could not be saved/i.test(w)))
|
|
264
|
+
assert.equal(result.restart, true)
|
|
265
|
+
assert.equal(readFileSync(join(profile, '.npmrc'), 'utf8'), npmrc)
|
|
266
|
+
assert.ok(existsSync(join(result.report.undoDir, 'manifest.json')))
|
|
267
|
+
await expect(confirm(planId), 409, 'stale-plan')
|
|
268
|
+
await expect(api('undo', {}), 200)
|
|
269
|
+
assert.equal(readFileSync(join(profile, '.npmrc'), 'utf8'), oldPolicy)
|
|
270
|
+
assert.deepEqual(calls, [])
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
for (const mode of ['config', 'sync']) {
|
|
274
|
+
for (const bound of [null, other]) {
|
|
275
|
+
await scenario(`${mode} confirmation records the reviewed Gist but keeps the ${bound ? 'different' : 'unset'} upload target`, async ({ api, preview, confirm, calls }) => {
|
|
276
|
+
if (bound) await expect(api('settings', { gistId: bound }), 200)
|
|
277
|
+
const plan = await preview(mode)
|
|
278
|
+
await expect(confirm(plan.planId, mode), 200)
|
|
279
|
+
const { gistId, lastSync, lastUpload } = readSettings()
|
|
280
|
+
assert.equal(gistId, bound)
|
|
281
|
+
assert.equal(lastSync.gistId, ID)
|
|
282
|
+
assert.equal(lastSync.remoteUpdatedAt, plan.ref.updatedAt)
|
|
283
|
+
assert.equal(lastUpload, null)
|
|
284
|
+
assert.ok(!calls.includes('installBundle'))
|
|
285
|
+
})
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
await scenario('the remembered Gist and host token can preview; an identical restore needs no restart', async ({ api, preview, confirm, repo }) => {
|
|
290
|
+
await expect(api('settings', { gistId: ID }), 200)
|
|
291
|
+
await expect(confirm((await preview()).planId), 200)
|
|
292
|
+
process.env.DSH_GITHUB_TOKEN = TEST_TOKEN
|
|
293
|
+
let plan
|
|
294
|
+
try { plan = await preview('config', {}) } finally { delete process.env.DSH_GITHUB_TOKEN }
|
|
295
|
+
assert.equal(plan.preview.counts.identical, plan.preview.files.length)
|
|
296
|
+
assert.equal((await expect(confirm(plan.planId), 200)).restart, false)
|
|
297
|
+
assert.deepEqual(repo.calls.map(call => call.path), [`/gists/${ID}`, `/gists/${ID}`])
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
await scenario('a changed upload target cannot silently turn a reviewed create or update into a different GitHub write', async ({ profile, api, repo }) => {
|
|
301
|
+
await expect(api('settings', { gistId: other }), 200) // another session changed the destination
|
|
302
|
+
const before = tree(profile)
|
|
303
|
+
for (const expectedTarget of [null, ID]) {
|
|
304
|
+
await expect(api('gist/upload', { token: TEST_TOKEN, expectedTarget }), 409, 'stale-plan')
|
|
305
|
+
}
|
|
306
|
+
assert.deepEqual(repo.calls, [])
|
|
307
|
+
assert.deepEqual(tree(profile), before)
|
|
308
|
+
await expect(api('settings', { gistId: '' }), 200)
|
|
309
|
+
await expect(api('gist/upload', { token: TEST_TOKEN, expectedTarget: null }), 200)
|
|
310
|
+
await expect(api('gist/upload', { token: TEST_TOKEN, expectedTarget: ID }), 200)
|
|
311
|
+
assert.deepEqual(repo.calls.map(({ method, path }) => [method, path]), [['POST', '/gists'], ['PATCH', `/gists/${ID}`]])
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
await scenario('upload receipts name their own Gist and the legacy sync keeps its original binding and response', async ({ api }) => {
|
|
315
|
+
const version = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version
|
|
316
|
+
assert.equal((await expect(api('status'), 200)).version, version)
|
|
317
|
+
await expect(api('gist/upload', { token: TEST_TOKEN }), 200)
|
|
318
|
+
await expect(api('settings', { gistId: other }), 200)
|
|
319
|
+
assert.equal(readSettings().lastUpload.gistId, ID)
|
|
320
|
+
assert.equal(readSettings().gistId, other)
|
|
321
|
+
const legacy = await expect(api('sync', source), 200)
|
|
322
|
+
assert.deepEqual(Object.keys(legacy).sort(), ['ref', 'report', 'tokenSource', 'undoDir'])
|
|
323
|
+
assert.equal(readSettings().gistId, ID)
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
const installablePairs = inventory => inventory.missing.filter(row => row.resolvable).map(({ name, installSpec }) => ({ name, installSpec }))
|
|
327
|
+
|
|
328
|
+
await scenario('independent installation validates the reviewed pairs and rejects changed or unavailable inventories without installing', async ({ api, profile, services, calls }) => {
|
|
329
|
+
const pairs = installablePairs(await expect(api('plugins'), 200))
|
|
330
|
+
assert.deepEqual(pairs, [{ name: 'some-plugin', installSpec: 'some-plugin@1.2.3' }])
|
|
331
|
+
const install = expectedPackages => api('plugins/install', { expectedPackages })
|
|
332
|
+
const before = tree(profile)
|
|
333
|
+
for (const invalid of [null, {}, [null], [{ name: 'some-plugin', installSpec: '' }], [...pairs, ...pairs]]) {
|
|
334
|
+
await expect(install(invalid), 400, 'invalid')
|
|
335
|
+
}
|
|
336
|
+
for (const changed of [[], [{ name: 'unreviewed', installSpec: 'unreviewed@1' }], [{ ...pairs[0], installSpec: 'some-plugin@2' }]]) {
|
|
337
|
+
await expect(install(changed), 409, 'stale-plan')
|
|
338
|
+
}
|
|
339
|
+
services.pluginManager = undefined
|
|
340
|
+
await expect(install(pairs), 409, 'stale-plan')
|
|
341
|
+
assert.deepEqual(tree(profile), before)
|
|
342
|
+
assert.ok(!calls.includes('installBundle'))
|
|
343
|
+
})
|
|
344
|
+
|
|
345
|
+
await scenario('matching local packages use actual specs and no build grants; old callers still work', async ({ api, services }) => {
|
|
346
|
+
const pairs = installablePairs(await expect(api('plugins'), 200))
|
|
347
|
+
const installs = []
|
|
348
|
+
services.pluginManager.installBundle = (spec, options) => { installs.push([spec, options]); return { application: 'applied' } }
|
|
349
|
+
for (const body of [{ expectedPackages: pairs, approvedBuilds: ['not-approved'] }, {}]) {
|
|
350
|
+
const report = await expect(api('plugins/install', body), 200)
|
|
351
|
+
assert.equal(report.outcomeCounts.installed, 1)
|
|
352
|
+
}
|
|
353
|
+
assert.deepEqual(installs, [['some-plugin@1.2.3', undefined], ['some-plugin@1.2.3', undefined]])
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
await scenario('a source change or new dependency between comparison and installer recheck cannot expand consent', async ({ api, profile, services, calls }) => {
|
|
357
|
+
const pairs = installablePairs(await expect(api('plugins'), 200))
|
|
358
|
+
let changed = false
|
|
359
|
+
services.pluginManager.listPlugins = () => {
|
|
360
|
+
if (!changed) {
|
|
361
|
+
changed = true // initial inventory already read the original declarations; the installer will see the new ones
|
|
362
|
+
const file = join(profile, 'package.json')
|
|
363
|
+
const manifest = JSON.parse(readFileSync(file, 'utf8'))
|
|
364
|
+
manifest.dependencies = { 'some-plugin': '2.0.0', 'new-gap': '1.0.0' }
|
|
365
|
+
writeFileSync(file, JSON.stringify(manifest))
|
|
366
|
+
}
|
|
367
|
+
return []
|
|
368
|
+
}
|
|
369
|
+
const report = await expect(api('plugins/install', { expectedPackages: pairs }), 200)
|
|
370
|
+
for (const name of ['some-plugin', 'new-gap']) assert.ok(report.results.some(row => row.name === name && row.outcome === 'skipped'))
|
|
371
|
+
assert.ok(!calls.includes('installBundle'))
|
|
372
|
+
await expect(api('plugins/install', { expectedPackages: pairs }), 409, 'stale-plan')
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
await scenario('undo cannot target a replacement snapshot; only the reviewed latest stamp is reverted', async ({ api, profile, preview, confirm }) => {
|
|
376
|
+
const undo = expectedStamp => api('undo', { expectedStamp })
|
|
377
|
+
await expect(undo('a-previous-snapshot'), 409, 'stale-plan')
|
|
378
|
+
await expect(confirm((await preview()).planId), 200)
|
|
379
|
+
const first = (await expect(api('status'), 200)).undo.stamp
|
|
380
|
+
await expect(undo(null), 400, 'invalid')
|
|
381
|
+
assert.equal((await expect(undo(first), 200)).report.stamp, first)
|
|
382
|
+
await expect(confirm((await preview()).planId), 200)
|
|
383
|
+
const latest = (await expect(api('status'), 200)).undo.stamp
|
|
384
|
+
assert.notEqual(first, latest)
|
|
385
|
+
const before = tree(profile)
|
|
386
|
+
await expect(undo(first), 409, 'stale-plan')
|
|
387
|
+
assert.deepEqual(tree(profile), before)
|
|
388
|
+
assert.equal((await expect(undo(latest), 200)).report.stamp, latest)
|
|
389
|
+
await expect(undo(latest), 409, 'stale-plan')
|
|
390
|
+
})
|
|
391
|
+
|
|
392
|
+
await scenario('a reviewed local import applies its validated snapshot once without reading GitHub or changing any Gist binding/history', async ({ api, profile, document, repo, calls }) => {
|
|
393
|
+
const before = tree(profile)
|
|
394
|
+
const plan = await expect(api('preview', { reviewed: true, backup: document }), 200)
|
|
395
|
+
assert.deepEqual(Object.keys(plan).sort(), ['planId', 'preview'])
|
|
396
|
+
assert.equal(typeof plan.planId, 'string')
|
|
397
|
+
assert.deepEqual(plan.preview.dropped, [{ path: credential, code: 'credential' }])
|
|
398
|
+
assert.deepEqual(tree(profile), before)
|
|
399
|
+
const planId = plan.planId
|
|
400
|
+
document.files.find(f => f.path === '.npmrc').lines = ['different file supplied after review']
|
|
401
|
+
await expect(api('restore', { planId }), 400, 'invalid')
|
|
402
|
+
await expect(api('restore', { planId, confirm: true, backup: document }), 400, 'invalid')
|
|
403
|
+
assert.deepEqual(tree(profile), before)
|
|
404
|
+
const saved = await expect(api('restore', { planId, confirm: true }), 200)
|
|
405
|
+
assert.deepEqual(Object.keys(saved), ['report'])
|
|
406
|
+
assert.deepEqual(saved.report.files, plan.preview.files)
|
|
407
|
+
assert.deepEqual(saved.report.dropped, plan.preview.dropped)
|
|
408
|
+
assert.equal(readFileSync(join(profile, '.npmrc'), 'utf8'), npmrc)
|
|
409
|
+
assert.equal(existsSync(join(profile, WORK_DIR, 'settings.json')), false)
|
|
410
|
+
assert.deepEqual(repo.calls, [])
|
|
411
|
+
assert.deepEqual(calls, [])
|
|
412
|
+
await expect(api('restore', { planId, confirm: true }), 409, 'stale-plan')
|
|
413
|
+
})
|
|
414
|
+
|
|
415
|
+
await scenario('local, cloud-config and combined IDs are not interchangeable; the original non-reviewed local API remains usable', async ({ api, profile, document, preview, confirm, repo }) => {
|
|
416
|
+
const before = tree(profile)
|
|
417
|
+
const local = await expect(api('preview', { backup: document, reviewed: true }), 200)
|
|
418
|
+
const full = await preview()
|
|
419
|
+
const combined = await preview('sync')
|
|
420
|
+
for (const id of [full.planId, combined.planId, null]) await expect(api('restore', { planId: id, confirm: true }), 409, 'stale-plan')
|
|
421
|
+
for (const mode of ['config', 'sync']) await expect(confirm(local.planId, mode), 409, 'stale-plan')
|
|
422
|
+
assert.deepEqual(tree(profile), before)
|
|
423
|
+
await expect(api('restore', { planId: local.planId, confirm: true }), 200)
|
|
424
|
+
const legacy = await expect(api('preview', { backup: document }), 200)
|
|
425
|
+
assert.deepEqual(Object.keys(legacy), ['preview'])
|
|
426
|
+
assert.deepEqual(Object.keys(await expect(api('restore', { backup: document, confirm: true }), 200)), ['report'])
|
|
427
|
+
assert.equal(repo.calls.length, 2, 'only the cloud previews contacted the stub')
|
|
428
|
+
})
|
|
429
|
+
|
|
430
|
+
await scenario('local consent expires and detects edits to files the review called identical', async ({ api, profile, document, repo, calls }) => {
|
|
431
|
+
const file = join(profile, 'data/note.json')
|
|
432
|
+
const initial = readFileSync(file, 'utf8')
|
|
433
|
+
document.files.push({ path: 'data/note.json', lines: initial.split('\n') })
|
|
434
|
+
const review = () => expect(api('preview', { backup: document, reviewed: true }), 200)
|
|
435
|
+
const restore = planId => api('restore', { planId, confirm: true })
|
|
436
|
+
const plan = await review()
|
|
437
|
+
assert.equal(plan.preview.files.find(f => f.path === 'data/note.json').status, 'identical')
|
|
438
|
+
writeFileSync(file, initial.replace('world', 'earth'))
|
|
439
|
+
const edited = tree(profile)
|
|
440
|
+
await expect(restore(plan.planId), 409, 'stale-plan')
|
|
441
|
+
const fresh = await review()
|
|
442
|
+
const now = Date.now
|
|
443
|
+
try {
|
|
444
|
+
const later = now() + 5 * 60_000
|
|
445
|
+
Date.now = () => later
|
|
446
|
+
await expect(restore(fresh.planId), 409, 'stale-plan')
|
|
447
|
+
} finally { Date.now = now }
|
|
448
|
+
assert.deepEqual(tree(profile), edited)
|
|
449
|
+
assert.deepEqual(repo.calls, [])
|
|
450
|
+
assert.deepEqual(calls, [])
|
|
451
|
+
})
|
|
452
|
+
|
|
453
|
+
globalThis.fetch = realFetch
|
|
454
|
+
process.exitCode = finish()
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first run: a machine that has never had a cloud copy.
|
|
3
|
+
*
|
|
4
|
+
* This is the journey every new user takes, and the only one that cannot be
|
|
5
|
+
* exercised against a real GitHub account without creating a Gist in it. The
|
|
6
|
+
* GitHub HTTP layer is stubbed instead — which also gives the plugin a privacy
|
|
7
|
+
* assertion worth having, because the stub refuses every host but
|
|
8
|
+
* api.github.com: any request anywhere else fails this check.
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { join } from 'node:path'
|
|
12
|
+
import { createReport, makeProfile, tempDir, useProfile, callRoute, serveHandler, stubGithub, backupDocument, TEST_GIST_ID as NEW_ID, TEST_TOKEN } from './harness.mjs'
|
|
13
|
+
|
|
14
|
+
const { check, finish } = createReport('first run')
|
|
15
|
+
|
|
16
|
+
const home = tempDir('dpg-firstrun-')
|
|
17
|
+
const profile = makeProfile(join(home, 'profiles', 'test'))
|
|
18
|
+
useProfile(profile, home)
|
|
19
|
+
process.env.DSH_GITHUB_TOKEN = TEST_TOKEN
|
|
20
|
+
|
|
21
|
+
// The cloud copy this profile will find: the same v0.2 shape upstream writes.
|
|
22
|
+
const stub = stubGithub({
|
|
23
|
+
gistId: NEW_ID,
|
|
24
|
+
document: backupDocument('test', [{ path: 'data/note.json', lines: ['{"hello":"from the cloud"}'] }]),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const { registerRoutes, BASE_PATH } = await import('../lib/routes.js')
|
|
28
|
+
let handler = null
|
|
29
|
+
registerRoutes({ get: (name) => (name === 'webServer' ? { register: (route) => { handler = route.handler; return () => {} } } : undefined) })
|
|
30
|
+
const served = await serveHandler(handler)
|
|
31
|
+
const call = (path, body) => callRoute(served.base, `${BASE_PATH}${path}`, body)
|
|
32
|
+
const settingsFile = join(profile, '.dsh-config-sync-plus', 'settings.json')
|
|
33
|
+
// A machine may already run upstream. Its state is neither imported implicitly
|
|
34
|
+
// nor changed by the Plus fork, even when both point at the same profile.
|
|
35
|
+
const upstreamState = join(profile, '.dsh-config-sync')
|
|
36
|
+
mkdirSync(upstreamState)
|
|
37
|
+
const upstreamSettings = '{"gistId":"upstream-fixture"}\n'
|
|
38
|
+
writeFileSync(join(upstreamState, 'settings.json'), upstreamSettings)
|
|
39
|
+
|
|
40
|
+
check('a fresh Plus profile remembers nothing yet', !existsSync(settingsFile))
|
|
41
|
+
const before = await call('/api/status')
|
|
42
|
+
check('status reports no cloud copy', before.status === 200 && before.payload.cloud.gistId === null, JSON.stringify(before.payload.cloud))
|
|
43
|
+
|
|
44
|
+
// A fresh installation can adopt the *document* from upstream by id without
|
|
45
|
+
// importing its settings, uploading, or creating another Gist.
|
|
46
|
+
const legacy = await call('/api/gist/read', { id: NEW_ID })
|
|
47
|
+
check('Plus reads an existing upstream v0.2 Gist before the first upload',
|
|
48
|
+
legacy.status === 200 && legacy.payload.backup?.format === 'dsh-profile-backup' && legacy.payload.backup.version === 0.2
|
|
49
|
+
&& !existsSync(settingsFile) && stub.calls.every(entry => entry.method === 'GET'))
|
|
50
|
+
|
|
51
|
+
// ---- the first upload creates a secret Gist ----------------------------
|
|
52
|
+
const upload = await call('/api/gist/upload', {})
|
|
53
|
+
check('the first upload creates a Gist', upload.status === 200 && upload.payload.created === true, JSON.stringify(upload.payload).slice(0, 160))
|
|
54
|
+
const creation = stub.calls.find(entry => entry.method === 'POST' && entry.path === '/gists')
|
|
55
|
+
check('exactly one Gist was created', stub.calls.filter(entry => entry.method === 'POST' && entry.path === '/gists').length === 1)
|
|
56
|
+
check('it is created secret, never public', creation?.body?.public === false, JSON.stringify(creation?.body?.public))
|
|
57
|
+
check('it carries exactly one file, the backup',
|
|
58
|
+
Object.keys(creation?.body?.files ?? {}).join() === 'dsh-profile-backup.json', JSON.stringify(Object.keys(creation?.body?.files ?? {})))
|
|
59
|
+
check('the backup in it is a valid document', typeof creation?.body?.files['dsh-profile-backup.json'].content === 'string'
|
|
60
|
+
&& JSON.parse(creation.body.files['dsh-profile-backup.json'].content).format === 'dsh-profile-backup')
|
|
61
|
+
check('the token travels in the header only',
|
|
62
|
+
String(creation?.headers?.authorization ?? '').includes(TEST_TOKEN)
|
|
63
|
+
&& !JSON.stringify(creation?.body ?? {}).includes(TEST_TOKEN),
|
|
64
|
+
String(creation?.headers?.authorization ?? '').slice(0, 12))
|
|
65
|
+
check('the Gist description and user agent identify the fork',
|
|
66
|
+
creation?.body?.description?.includes('dsh-multi-device-config-sync') === true && creation?.headers['user-agent'] === 'dsh-multi-device-config-sync')
|
|
67
|
+
check('the created id is remembered in this profile',
|
|
68
|
+
existsSync(settingsFile) && JSON.parse(readFileSync(settingsFile, 'utf8')).gistId === NEW_ID)
|
|
69
|
+
|
|
70
|
+
const after = await call('/api/status')
|
|
71
|
+
check('status now reports the remembered Gist',
|
|
72
|
+
after.payload.cloud.gistId === NEW_ID && after.payload.cloud.lastUpload?.created === true, JSON.stringify(after.payload.cloud))
|
|
73
|
+
|
|
74
|
+
// ---- a second upload updates the same Gist -----------------------------
|
|
75
|
+
const again = await call('/api/gist/upload', {})
|
|
76
|
+
check('a second upload updates instead of creating another',
|
|
77
|
+
again.status === 200 && again.payload.created === false
|
|
78
|
+
&& stub.calls.filter(entry => entry.method === 'POST' && entry.path === '/gists').length === 1)
|
|
79
|
+
check('and it targets the remembered id', stub.calls.some(entry => entry.method === 'PATCH' && entry.path === `/gists/${NEW_ID}`))
|
|
80
|
+
|
|
81
|
+
// ---- reading and syncing that cloud copy -------------------------------
|
|
82
|
+
const read = await call('/api/gist/read', {})
|
|
83
|
+
check('the cloud copy reads back', read.status === 200 && read.payload.ref.id === NEW_ID, JSON.stringify(read.payload).slice(0, 160))
|
|
84
|
+
check('and the plan names what a restore would change', read.payload.preview.files.length > 0, JSON.stringify(read.payload.preview.counts))
|
|
85
|
+
|
|
86
|
+
const sync = await call('/api/sync', {})
|
|
87
|
+
check('sync applies the cloud copy here', sync.status === 200 && typeof sync.payload.undoDir === 'string', JSON.stringify(sync.payload).slice(0, 160))
|
|
88
|
+
check('the file it wrote holds the cloud content',
|
|
89
|
+
readFileSync(join(profile, 'data', 'note.json'), 'utf8').includes('from the cloud'),
|
|
90
|
+
readFileSync(join(profile, 'data', 'note.json'), 'utf8'))
|
|
91
|
+
check('an undo is offered', (await call('/api/status')).payload.undo !== null)
|
|
92
|
+
|
|
93
|
+
// ---- what the plugin never does ----------------------------------------
|
|
94
|
+
const paths = stub.calls.map(entry => entry.path)
|
|
95
|
+
check('every call went to api.github.com', stub.calls.length > 0 && paths.every(path => path.startsWith('/')))
|
|
96
|
+
check('no request carried a credential store', !JSON.stringify(stub.calls).includes('credentials'))
|
|
97
|
+
check('no request carried local usage counters', !JSON.stringify(stub.calls).includes('usage.json'))
|
|
98
|
+
check('the token is never written into the profile', !readFileSync(settingsFile, 'utf8').includes(TEST_TOKEN))
|
|
99
|
+
check('upstream settings were neither uploaded nor changed', !JSON.stringify(stub.calls).includes('upstream-fixture')
|
|
100
|
+
&& readFileSync(join(upstreamState, 'settings.json'), 'utf8') === upstreamSettings)
|
|
101
|
+
|
|
102
|
+
stub.restore()
|
|
103
|
+
served.close()
|
|
104
|
+
process.exit(finish())
|