dsh-ris-ide 1.6.11 → 1.7.1
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/README.md +25 -22
- package/lib/client-codemirror.js +309 -35
- package/lib/client.js +3193 -392
- package/lib/index.js +1635 -54
- package/lib/standalone/window.html +89 -14
- package/package.json +1 -1
- package/skills/dsh-ris-ide-debug/SKILL.md +2 -2
- package/test/client-viewer.test.mjs +834 -63
- package/test/clipboard.test.mjs +254 -0
- package/test/host-search.test.mjs +383 -0
- package/test/host-tail.test.mjs +136 -21
- package/test/windows.test.mjs +198 -0
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tree's copy mirrored onto the OS clipboard so a file manager can paste it.
|
|
3
|
+
*
|
|
4
|
+
* A web page cannot publish a file list (Chromium refuses `text/uri-list` on
|
|
5
|
+
* `Clipboard.write`, and a synthetic `copy` event only yields Chrome's private
|
|
6
|
+
* flavour), so the Host builds the platform command. These tests pin the command
|
|
7
|
+
* shape and the route contract — never the real clipboard, which belongs to the
|
|
8
|
+
* developer's desktop, not to CI.
|
|
9
|
+
*/
|
|
10
|
+
import { test } from 'node:test'
|
|
11
|
+
import assert from 'node:assert/strict'
|
|
12
|
+
import { createServer } from 'node:http'
|
|
13
|
+
import { readFile } from 'node:fs/promises'
|
|
14
|
+
|
|
15
|
+
import { __test } from '../lib/index.js'
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
CLIPBOARD_FILES_PATH,
|
|
19
|
+
CLIPBOARD_READ_PATH,
|
|
20
|
+
CLIPBOARD_MAX_ENTRIES,
|
|
21
|
+
CLIPBOARD_MAX_PAYLOAD,
|
|
22
|
+
CLIPBOARD_STAGE_DIR,
|
|
23
|
+
clipboardScope,
|
|
24
|
+
stageClipboardCopy,
|
|
25
|
+
pruneClipboardStage,
|
|
26
|
+
clipboardStageMapping,
|
|
27
|
+
windowsClipboardScript,
|
|
28
|
+
windowsClipboardReadScript,
|
|
29
|
+
parseClipboardRead,
|
|
30
|
+
clipboardPathsToLinux,
|
|
31
|
+
readSystemFileClipboard,
|
|
32
|
+
writeSystemFileClipboard,
|
|
33
|
+
serveClipboardFiles,
|
|
34
|
+
serveClipboardRead,
|
|
35
|
+
isWsl,
|
|
36
|
+
} = __test
|
|
37
|
+
|
|
38
|
+
test('the clipboard route is one POST under the plugin prefix', () => {
|
|
39
|
+
assert.equal(CLIPBOARD_FILES_PATH, '/dsh-ris-ide/clipboard/files')
|
|
40
|
+
assert.equal(typeof serveClipboardFiles, 'function')
|
|
41
|
+
assert.equal(CLIPBOARD_MAX_ENTRIES, 200, 'one copy is a selection, not a whole disk')
|
|
42
|
+
assert.equal(CLIPBOARD_MAX_PAYLOAD, 256 * 1024, 'the stdin payload is bounded too')
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('the PowerShell script writes CF_HDROP with the intent Explorer reads', () => {
|
|
46
|
+
const copy = windowsClipboardScript(false)
|
|
47
|
+
assert.match(copy, /Add-Type -AssemblyName System\.Windows\.Forms/, 'the clipboard API comes from WinForms')
|
|
48
|
+
assert.match(copy, /\$data\.SetData\('FileDrop', \[string\[\]\]\$lines\)/, 'paths go in as a FileDrop list')
|
|
49
|
+
assert.match(copy, /\$data\.SetData\('Preferred DropEffect', \[byte\[\]\]@\(1,0,0,0\)\)/, 'copy is DROPEFFECT_COPY')
|
|
50
|
+
assert.match(copy, /\[System\.Windows\.Forms\.Clipboard\]::SetDataObject\(\$data, \$true\)/, 'the data is flushed, so it survives the PowerShell process')
|
|
51
|
+
assert.match(windowsClipboardScript(true), /\[byte\[\]\]@\(2,0,0,0\)/, 'cut is DROPEFFECT_MOVE')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('no file name can reach the PowerShell script as code', async () => {
|
|
55
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
56
|
+
// The script is a constant: the paths ride on stdin, one per line.
|
|
57
|
+
assert.match(source, /\[Console\]::OpenStandardInput\(\)/, 'the list is read from stdin')
|
|
58
|
+
assert.match(source, /\[Text\.Encoding\]::UTF8\.GetString/, 'stdin bytes are decoded as UTF-8')
|
|
59
|
+
assert.equal(/windowsPaths\.map\(powershellLiteral\)/.test(source), false, 'no path is interpolated into the script')
|
|
60
|
+
assert.equal(/function powershellLiteral/.test(source), false, 'the literal quoter is gone with the interpolation')
|
|
61
|
+
// …and the list is handed over as stdin, never as an argument vector entry.
|
|
62
|
+
assert.match(source, /'powershell\.exe', \['-NoProfile', '-NonInteractive', '-STA', '-EncodedCommand', encoded\], `\$\{windowsPaths\.join\('\\n'\)\}\\n`\)/)
|
|
63
|
+
// The route refuses names the line protocol cannot express.
|
|
64
|
+
assert.match(source, /复制到系统剪贴板不支持路径里的换行或控制字符/)
|
|
65
|
+
assert.match(source, /CLIPBOARD_MAX_PAYLOAD/)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('the Host owns the platform call, the client only asks for it', async () => {
|
|
69
|
+
assert.equal(typeof writeSystemFileClipboard, 'function')
|
|
70
|
+
assert.equal(typeof isWsl(), 'boolean', 'WSL detection is a platform fact, never request data')
|
|
71
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
72
|
+
assert.match(source, /isLoopbackRequest\(req\)\) throw new TailError\('forbidden'/, 'the route is loopback-fenced')
|
|
73
|
+
assert.match(source, /paths\.length > CLIPBOARD_MAX_ENTRIES/, 'the count is capped')
|
|
74
|
+
assert.match(source, /'wslpath'/, 'WSL paths are translated for the Windows clipboard')
|
|
75
|
+
assert.match(source, /'linux-xclip'/, 'a GNOME desktop gets the copy|cut flavour')
|
|
76
|
+
assert.match(source, /'linux-wl-copy'/, 'a plain Wayland desktop falls back to text/uri-list')
|
|
77
|
+
assert.match(source, /'macos-appkit'/, 'Finder pasteboard is written through JXA')
|
|
78
|
+
// Every child process is spawned without a shell, with a hard timeout.
|
|
79
|
+
assert.equal(/shell: true/.test(source), false, 'no child is run through a shell')
|
|
80
|
+
assert.match(source, /child\.kill\('SIGKILL'\)/, 'a stuck helper is killed')
|
|
81
|
+
// macOS gets the paths as argv, not as JXA source.
|
|
82
|
+
assert.match(source, /'osascript', \['-l', 'JavaScript', '-e', script, \.\.\.paths\]/)
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('the route rejects relative, missing, control-character and oversized lists before touching the clipboard', async () => {
|
|
86
|
+
const server = createServer((req, res) => { void serveClipboardFiles(undefined, req, res, {}) })
|
|
87
|
+
await new Promise((resolvePromise) => server.listen(0, '127.0.0.1', resolvePromise))
|
|
88
|
+
const base = `http://127.0.0.1:${server.address().port}/dsh-ris-ide/clipboard/files`
|
|
89
|
+
const post = (body) => fetch(base, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) })
|
|
90
|
+
try {
|
|
91
|
+
assert.equal((await post({ paths: ['notes.txt'] })).status, 400, 'a relative path has no meaning on the OS clipboard')
|
|
92
|
+
assert.equal((await post({ paths: ['/definitely/not/here.txt'] })).status, 404, 'a stale row must not put a ghost on the clipboard')
|
|
93
|
+
assert.equal((await post({ paths: ['/tmp/it\nis.txt'] })).status, 400, 'a line break would split one name into two entries')
|
|
94
|
+
assert.equal((await post({ paths: ['/tmp/it\u0000is.txt'] })).status, 400, 'a NUL cannot survive a text channel')
|
|
95
|
+
const tooMany = Array.from({ length: CLIPBOARD_MAX_ENTRIES + 1 }, (_, index) => `/tmp/clip-${index}.txt`)
|
|
96
|
+
assert.equal((await post({ paths: tooMany })).status, 400, 'the cap is enforced')
|
|
97
|
+
assert.equal((await post({ paths: [] })).status, 400, 'an empty list is a caller bug')
|
|
98
|
+
// A COPY must be staged first, and staging needs the workspace. Without it the
|
|
99
|
+
// route refuses rather than publishing the originals.
|
|
100
|
+
const { mkdtemp, writeFile } = await import('node:fs/promises')
|
|
101
|
+
const { tmpdir } = await import('node:os')
|
|
102
|
+
const { join } = await import('node:path')
|
|
103
|
+
const real = join(await mkdtemp(join(tmpdir(), 'dsh-clip-route-')), 'a.txt')
|
|
104
|
+
await writeFile(real, 'x', 'utf8')
|
|
105
|
+
const noWorkspace = await post({ paths: [real] })
|
|
106
|
+
assert.equal(noWorkspace.status, 400, 'a copy that cannot be staged is refused')
|
|
107
|
+
assert.match((await noWorkspace.json()).message, /sessionId/, 'the message says why')
|
|
108
|
+
} finally {
|
|
109
|
+
await new Promise((resolvePromise) => server.close(resolvePromise))
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
test('the clipboard scope is read from the query string, like every tree route', () => {
|
|
114
|
+
const query = new URL('http://dsh.internal/dsh-ris-ide/clipboard/files?sessionId=session-1&cwd=%2Fw')
|
|
115
|
+
// The page puts the scope in the QUERY (that is how `clipboardQuery` builds it).
|
|
116
|
+
// Reading only the body made copy answer "复制到系统剪贴板需要 sessionId".
|
|
117
|
+
assert.deepEqual(clipboardScope({}, query), { sessionId: 'session-1', cwd: '/w' }, 'a page-built query resolves')
|
|
118
|
+
assert.deepEqual(clipboardScope({ sessionId: 'body', cwd: '/b' }, new URL('http://dsh.internal/x')), { sessionId: 'body', cwd: '/b' }, 'a body still works')
|
|
119
|
+
assert.deepEqual(clipboardScope({ sessionId: 'body' }, query), { sessionId: 'body', cwd: '/w' }, 'the body wins per field, the query fills the gap')
|
|
120
|
+
assert.deepEqual(clipboardScope({}, new URL('http://dsh.internal/x')), { sessionId: '', cwd: '' }, 'nothing carried reads as empty scope')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
test('the clipboard can be READ back: Explorer copies reach the tree on WSL', () => {
|
|
124
|
+
assert.equal(CLIPBOARD_READ_PATH, '/dsh-ris-ide/clipboard/read')
|
|
125
|
+
assert.equal(typeof serveClipboardRead, 'function')
|
|
126
|
+
assert.equal(typeof readSystemFileClipboard, 'function')
|
|
127
|
+
// The read script is a constant too — it prints, it never interpolates a path.
|
|
128
|
+
const script = windowsClipboardReadScript()
|
|
129
|
+
assert.match(script, /Add-Type -AssemblyName System\.Windows\.Forms/)
|
|
130
|
+
assert.match(script, /GetDataObject\(\)/)
|
|
131
|
+
assert.match(script, /\$data\.GetData\("FileDrop"\)/)
|
|
132
|
+
assert.match(script, /"F:" \+ \[string\]\$file/, 'one line per file, so a name is never escaped as code')
|
|
133
|
+
assert.match(script, /"T:" \+ \[Convert\]::ToBase64String/, 'the text flavour is base64, so newlines survive')
|
|
134
|
+
const parsed = parseClipboardRead([
|
|
135
|
+
'F:' + ['C:', 'Users', 'dev', 'Desktop', 'README.md'].join('\\'),
|
|
136
|
+
'F:' + ['', '', 'wsl.localhost', 'Ubuntu-24.04', 'home', 'user', 'AGENTS.md'].join('\\'),
|
|
137
|
+
`T:${Buffer.from('hello\nworld', 'utf8').toString('base64')}`,
|
|
138
|
+
].join('\r\n'))
|
|
139
|
+
assert.deepEqual(parsed.files, [
|
|
140
|
+
['C:', 'Users', 'dev', 'Desktop', 'README.md'].join('\\'),
|
|
141
|
+
['', '', 'wsl.localhost', 'Ubuntu-24.04', 'home', 'user', 'AGENTS.md'].join('\\'),
|
|
142
|
+
])
|
|
143
|
+
assert.equal(parsed.text, 'hello\nworld')
|
|
144
|
+
})
|
|
145
|
+
|
|
146
|
+
test('Windows clipboard paths map to paths this filesystem can import', () => {
|
|
147
|
+
const wslRoot = ['', '', 'wsl.localhost', 'Ubuntu-24.04'].join('\\')
|
|
148
|
+
const otherRoot = ['', '', 'wsl.localhost', 'OtherDistro'].join('\\')
|
|
149
|
+
const serverRoot = ['', '', 'server', 'share'].join('\\')
|
|
150
|
+
const mapped = clipboardPathsToLinux([
|
|
151
|
+
wslRoot + '\\' + ['home', 'user', 'AGENTS.md'].join('\\'),
|
|
152
|
+
['', '', 'wsl$', 'Ubuntu-24.04'].join('\\') + '\\' + ['tmp', 'a b.txt'].join('\\'),
|
|
153
|
+
['C:', 'Users', 'dev', 'Desktop', 'README.md'].join('\\'),
|
|
154
|
+
otherRoot + '\\' + ['home', 'x.txt'].join('\\'),
|
|
155
|
+
serverRoot + '\\' + 'x.txt',
|
|
156
|
+
'/already/linux',
|
|
157
|
+
'/already/linux',
|
|
158
|
+
], { distro: 'Ubuntu-24.04' })
|
|
159
|
+
assert.deepEqual(mapped, [
|
|
160
|
+
'/' + ['home', 'user', 'AGENTS.md'].join('/'),
|
|
161
|
+
'/' + ['tmp', 'a b.txt'].join('/'),
|
|
162
|
+
'/' + ['mnt', 'c', 'Users', 'dev', 'Desktop', 'README.md'].join('/'),
|
|
163
|
+
'/already/linux',
|
|
164
|
+
], 'a foreign distro or an SMB share is not visible here; duplicates collapse')
|
|
165
|
+
assert.deepEqual(clipboardPathsToLinux([]), [])
|
|
166
|
+
assert.deepEqual(clipboardPathsToLinux('not a list'), [])
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test('reading the clipboard is a loopback-fenced GET, never a write', async () => {
|
|
170
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
171
|
+
assert.match(source, /const CLIPBOARD_READ_PATH = `\$\{ROUTE_PREFIX\}\/clipboard\/read`/)
|
|
172
|
+
assert.match(source, /if \(pathname === CLIPBOARD_READ_PATH\) \{/, 'the dispatch knows the route')
|
|
173
|
+
assert.match(source, /message: 'GET only'/, 'only a read is allowed on it')
|
|
174
|
+
assert.match(source, /async function serveClipboardRead\(ctx, req, res\) \{[\s\S]{0,400}isLoopbackRequest\(req\)\) throw new TailError\('forbidden'/,
|
|
175
|
+
'the same loopback fence as the write route')
|
|
176
|
+
assert.match(source, /'macos-appkit'[\s\S]{0,2000}'linux-wl-paste'/, 'each platform reads the flavour it wrote')
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
test('a COPY is published from a hidden staging area, never from the originals', async () => {
|
|
180
|
+
const { mkdtemp, mkdir, writeFile, readFile, stat, readdir } = await import('node:fs/promises')
|
|
181
|
+
const { tmpdir } = await import('node:os')
|
|
182
|
+
const { join } = await import('node:path')
|
|
183
|
+
const root = await mkdtemp(join(tmpdir(), 'dsh-clip-'))
|
|
184
|
+
const sourceFile = join(root, 'AGENTS.md')
|
|
185
|
+
await writeFile(sourceFile, '# agents\n', 'utf8')
|
|
186
|
+
const folder = join(root, 'docs')
|
|
187
|
+
await mkdir(folder)
|
|
188
|
+
await writeFile(join(folder, 'note.txt'), 'note', 'utf8')
|
|
189
|
+
|
|
190
|
+
const stage = await stageClipboardCopy(root, [sourceFile, folder])
|
|
191
|
+
assert.notEqual(stage, null, 'staging succeeds inside one filesystem')
|
|
192
|
+
assert.equal(stage.staged.length, 2)
|
|
193
|
+
assert.deepEqual(stage.originals, [sourceFile, folder])
|
|
194
|
+
assert.ok(stage.staged[0].startsWith(join(root, CLIPBOARD_STAGE_DIR)), 'the copy lives under the hidden stage')
|
|
195
|
+
assert.notEqual(stage.staged[0], sourceFile, 'the clipboard points at the copy, not the original')
|
|
196
|
+
// A plain file is a HARDLINK: same inode, no extra bytes, originals untouched.
|
|
197
|
+
assert.equal((await stat(stage.staged[0])).ino, (await stat(sourceFile)).ino, 'files are hard-linked, not duplicated')
|
|
198
|
+
assert.equal(await readFile(stage.staged[0], 'utf8'), '# agents\n')
|
|
199
|
+
assert.equal(await readFile(sourceFile, 'utf8'), '# agents\n', 'the original is still there')
|
|
200
|
+
// A directory cannot be hard-linked, but each FILE inside it is: the staged tree
|
|
201
|
+
// is a mirror of names, so a big folder costs inodes instead of bytes.
|
|
202
|
+
assert.equal((await stat(stage.staged[1])).isDirectory(), true)
|
|
203
|
+
assert.equal((await stat(join(stage.staged[1], 'note.txt'))).ino, (await stat(join(folder, 'note.txt'))).ino,
|
|
204
|
+
'files inside a staged folder are hard links too')
|
|
205
|
+
assert.equal(await readFile(join(stage.staged[1], 'note.txt'), 'utf8'), 'note')
|
|
206
|
+
assert.equal(await readFile(join(folder, 'note.txt'), 'utf8'), 'note')
|
|
207
|
+
|
|
208
|
+
// The manifest translates a staged path back to the original: Ctrl+C → Ctrl+V
|
|
209
|
+
// inside the tree must keep looking like the original selection.
|
|
210
|
+
const mapping = await clipboardStageMapping(root)
|
|
211
|
+
assert.equal(mapping.get(stage.staged[0]), sourceFile)
|
|
212
|
+
assert.equal(mapping.get(stage.staged[1]), folder)
|
|
213
|
+
|
|
214
|
+
// Old batches are pruned: the staging area cannot grow without bound.
|
|
215
|
+
for (let index = 0; index < 5; index += 1) await stageClipboardCopy(root, [sourceFile])
|
|
216
|
+
await pruneClipboardStage(root)
|
|
217
|
+
const batches = (await readdir(join(root, CLIPBOARD_STAGE_DIR), { withFileTypes: true }))
|
|
218
|
+
.filter((entry) => entry.isDirectory())
|
|
219
|
+
assert.ok(batches.length <= 3, 'only the recent batches survive')
|
|
220
|
+
})
|
|
221
|
+
|
|
222
|
+
test('only a COPY is staged; a cut still publishes the real file', async () => {
|
|
223
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
224
|
+
assert.match(source, /if \(body\.cut !== true\) \{[\s\S]{0,600}stageClipboardCopy\(root, paths\)/,
|
|
225
|
+
'copy goes through the staging area')
|
|
226
|
+
assert.match(source, /published = stage\.staged/, 'the clipboard gets the staged paths, not the originals')
|
|
227
|
+
assert.match(source, /const mapping = await clipboardStageMapping\(root\)/, 'a read translates staged paths back')
|
|
228
|
+
assert.match(source, /files\.map\(\(path\) => mapping\.get\(resolve\(path\)\) \?\? path\)/)
|
|
229
|
+
assert.match(source, /if \(dirent\.name === IDE_STATE_DIR\) continue/, 'the state folder (trash/cache/stage) is never a tree row')
|
|
230
|
+
assert.match(source, /'\.dsh-ris-ide',/, 'the watcher ignores its churn')
|
|
231
|
+
})
|
|
232
|
+
|
|
233
|
+
test('the copy route reads the workspace scope the page actually sends', async () => {
|
|
234
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
235
|
+
// The page builds `?sessionId&cwd` (see `clipboardQuery`). Reading only the JSON
|
|
236
|
+
// body made every real copy fail with "复制到系统剪贴板需要 sessionId".
|
|
237
|
+
assert.match(source, /const scope = clipboardScope\(body, url\)/, 'the route parses the query string')
|
|
238
|
+
assert.match(source, /const root = clipboardRoot\(ctx, scope\.sessionId, scope\.cwd\)/, 'the scope feeds the staging root')
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('a copy that cannot be staged fails instead of publishing the originals', async () => {
|
|
242
|
+
const source = await readFile(new URL('../lib/index.js', import.meta.url), 'utf8')
|
|
243
|
+
// The old shape — stage, and on failure quietly hand out the real paths — is the
|
|
244
|
+
// silent downgrade that lets Explorer move files out of the workspace.
|
|
245
|
+
assert.equal(/stage === null[\s\S]{0,160}published = paths/.test(source), false,
|
|
246
|
+
'no silent fallback to the originals')
|
|
247
|
+
assert.equal(/stageClipboardCopy\(root, paths\)\n\s+if \(stage === null\)/.test(source), false)
|
|
248
|
+
assert.match(source, /超过 \$\{megabytes\} MB,已取消复制到系统剪贴板/, 'the toast stays short but says the copy was cancelled')
|
|
249
|
+
assert.match(source, /复制到系统剪贴板需要 sessionId/, 'without a workspace there is nothing to stage into')
|
|
250
|
+
// A real refusal is an error the page reports, not a "copied" toast.
|
|
251
|
+
const client = await readFile(new URL('../lib/client.js', import.meta.url), 'utf8')
|
|
252
|
+
assert.match(client, /notify\('没能放到系统剪贴板:' \+ messageOf\(problem\)/)
|
|
253
|
+
assert.match(client, /if \(code === 'unsupported'\) \{/, 'only "no backend at all" still writes the paths as text')
|
|
254
|
+
})
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-half tests: workspace CONTENT search (`git grep` + the no-git
|
|
3
|
+
* filesystem fallback) and the single-module `mvn test` runner, plus the
|
|
4
|
+
* `restart: true` path of the existing start route.
|
|
5
|
+
*
|
|
6
|
+
* Like host-tail.test.mjs these run without a DSH profile: the route handler is
|
|
7
|
+
* a plain function of `(ctx, req, res, options)`, so a fake ctx with a session
|
|
8
|
+
* header and a real listener exercise exactly what the browser hits. No real
|
|
9
|
+
* Maven is needed — the spawn tests put a tiny `mvn` shim on PATH (the runner
|
|
10
|
+
* resolves its executable through PATH at call time).
|
|
11
|
+
*/
|
|
12
|
+
import { test, after } from 'node:test'
|
|
13
|
+
import assert from 'node:assert/strict'
|
|
14
|
+
import { createServer } from 'node:http'
|
|
15
|
+
import { execFileSync } from 'node:child_process'
|
|
16
|
+
import { existsSync } from 'node:fs'
|
|
17
|
+
import { chmod, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
18
|
+
import { tmpdir } from 'node:os'
|
|
19
|
+
import { join } from 'node:path'
|
|
20
|
+
|
|
21
|
+
import { __test } from '../lib/index.js'
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
handleRequest, searchWorkspaceContent,
|
|
25
|
+
CONTENT_SEARCH_PATH, RUN_TEST_PATH, START_PATH, STOP_PATH, PROGRAMS_PATH,
|
|
26
|
+
runTestOutput, RUN_TEST_HEAD_CHARS, RUN_TEST_TAIL_CHARS, RUN_TEST_MAX_RECORDS,
|
|
27
|
+
HARD_MAX_BYTES, PID_SUFFIX, IS_WINDOWS,
|
|
28
|
+
} = __test
|
|
29
|
+
|
|
30
|
+
/** One scratch directory per test process, removed when the file is done. */
|
|
31
|
+
const scratch = await mkdtemp(join(tmpdir(), 'dsh-ris-ide-search-'))
|
|
32
|
+
after(async () => { await rm(scratch, { recursive: true, force: true }) })
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Jenkins/CI 的 `bash -lc`(被测服务的启动命令就是它)会读登录 profile,可能把
|
|
36
|
+
* agent 注入的 PATH 重置掉,导致服务里的 `npm`/`node` 变成 not found(status 127)。
|
|
37
|
+
* 非交互 bash 会在 profile 之后读取 `BASH_ENV`,用它把当前可用的 PATH 再补回去。
|
|
38
|
+
*/
|
|
39
|
+
const ciBashEnv = join(scratch, 'ci-bash-env.sh')
|
|
40
|
+
await writeFile(ciBashEnv, `export PATH="${String(process.env.PATH || '')}"\n`)
|
|
41
|
+
process.env.BASH_ENV = ciBashEnv
|
|
42
|
+
|
|
43
|
+
/** Start all routes on a real listener with a fake session store. */
|
|
44
|
+
async function withServer(run, rootCwd, options = { maxBytes: HARD_MAX_BYTES, restrictToCwd: false, stopGraceMs: 5000, minFreeMemoryBytes: 1 }) {
|
|
45
|
+
const cwd = rootCwd ?? await mkdtemp(join(scratch, 'server-'))
|
|
46
|
+
const ctx = { sessions: { get: (id) => (id === 's1' ? { header: { cwd } } : undefined) } }
|
|
47
|
+
const server = createServer((req, res) => { handleRequest(ctx, req, res, options) })
|
|
48
|
+
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
49
|
+
const base = `http://127.0.0.1:${server.address().port}`
|
|
50
|
+
try {
|
|
51
|
+
return await run({ base, cwd })
|
|
52
|
+
} finally {
|
|
53
|
+
await new Promise((resolve) => server.close(resolve))
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** One JSON POST. */
|
|
58
|
+
function postJson(base, path, body) {
|
|
59
|
+
return fetch(`${base}${path}`, {
|
|
60
|
+
method: 'POST',
|
|
61
|
+
headers: { 'content-type': 'application/json' },
|
|
62
|
+
body: JSON.stringify(body),
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Poll one predicate until it holds or the deadline passes. */
|
|
67
|
+
async function waitFor(predicate, timeoutMs = 10000) {
|
|
68
|
+
const deadline = Date.now() + timeoutMs
|
|
69
|
+
while (Date.now() < deadline) {
|
|
70
|
+
if (await predicate()) return true
|
|
71
|
+
await new Promise((resolve) => setTimeout(resolve, 50))
|
|
72
|
+
}
|
|
73
|
+
return false
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A git repo in a scratch dir with a couple of matchable files (index only). */
|
|
77
|
+
async function makeRepoFixture(label) {
|
|
78
|
+
const root = await mkdtemp(join(scratch, `${label}-`))
|
|
79
|
+
await writeFile(join(root, 'a.txt'), 'alpha\nbeta\nneedle one\nneedle two\nneedle three\n')
|
|
80
|
+
await mkdir(join(root, 'sub'), { recursive: true })
|
|
81
|
+
await writeFile(join(root, 'sub', 'b.txt'), 'x needle deep\n')
|
|
82
|
+
execFileSync('git', ['init', '-q'], { cwd: root })
|
|
83
|
+
execFileSync('git', ['add', '-A'], { cwd: root })
|
|
84
|
+
return root
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ── Content search helper ───────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
test('searchWorkspaceContent returns literal git-grep hits with absolute path, line and text', async () => {
|
|
90
|
+
const root = await makeRepoFixture('content')
|
|
91
|
+
const { hits, truncated, roots, files } = await searchWorkspaceContent(root, 'needle', 300, 8)
|
|
92
|
+
assert.deepEqual(roots, [root])
|
|
93
|
+
assert.equal(truncated, false)
|
|
94
|
+
assert.equal(files, 2, 'distinct files that matched')
|
|
95
|
+
assert.deepEqual(hits, [
|
|
96
|
+
{ path: join(root, 'a.txt'), line: 3, text: 'needle one' },
|
|
97
|
+
{ path: join(root, 'a.txt'), line: 4, text: 'needle two' },
|
|
98
|
+
{ path: join(root, 'a.txt'), line: 5, text: 'needle three' },
|
|
99
|
+
{ path: join(root, 'sub', 'b.txt'), line: 1, text: 'x needle deep' },
|
|
100
|
+
])
|
|
101
|
+
// git grep is literal (-F): a regex metacharacter must not widen the search.
|
|
102
|
+
const regexHit = await searchWorkspaceContent(root, 'need.*one', 300, 8)
|
|
103
|
+
assert.deepEqual(regexHit.hits, [])
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
test('searchWorkspaceContent honours perFile and answers an empty hit list for no match', async () => {
|
|
107
|
+
const root = await makeRepoFixture('cap')
|
|
108
|
+
const capped = await searchWorkspaceContent(root, 'needle', 300, 2)
|
|
109
|
+
const fromA = capped.hits.filter((hit) => hit.path === join(root, 'a.txt'))
|
|
110
|
+
assert.equal(fromA.length, 2, 'perFile caps the matches taken from one file')
|
|
111
|
+
|
|
112
|
+
const none = await searchWorkspaceContent(root, 'zzz-not-here', 300, 8)
|
|
113
|
+
assert.deepEqual(none.hits, [])
|
|
114
|
+
assert.equal(none.truncated, false)
|
|
115
|
+
assert.equal(none.files, 0)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
test('searchWorkspaceContent falls back to a case-insensitive walk without a git repo', async () => {
|
|
119
|
+
const root = await mkdtemp(join(scratch, 'nogit-'))
|
|
120
|
+
await mkdir(join(root, 'nested'), { recursive: true })
|
|
121
|
+
await writeFile(join(root, 'nested', 'notes.md'), 'first line\nA Fallback Needle lives here\n')
|
|
122
|
+
await writeFile(join(root, 'binary.bin'), Buffer.from([0x6e, 0x65, 0x65, 0x64, 0x6c, 0x65, 0x00, 0x01]))
|
|
123
|
+
const { hits, roots } = await searchWorkspaceContent(root, 'fallback needle', 300, 8)
|
|
124
|
+
// /tmp (or wherever the scratch dir lives) carries no .git anywhere above it.
|
|
125
|
+
assert.deepEqual(roots, [], 'no repo, so the walk is the only option')
|
|
126
|
+
assert.equal(hits.length, 1)
|
|
127
|
+
assert.equal(hits[0].path, join(root, 'nested', 'notes.md'))
|
|
128
|
+
assert.equal(hits[0].line, 2)
|
|
129
|
+
assert.equal(hits[0].text, 'A Fallback Needle lives here')
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
// ── Content search route ────────────────────────────────────────────────────
|
|
133
|
+
|
|
134
|
+
test('GET /search-content serves hits and rejects a missing q or the wrong method', async () => {
|
|
135
|
+
const root = await makeRepoFixture('content-route')
|
|
136
|
+
await withServer(async ({ base }) => {
|
|
137
|
+
const response = await fetch(`${base}${CONTENT_SEARCH_PATH}?sessionId=s1&q=needle&perFile=2&limit=50`)
|
|
138
|
+
assert.equal(response.status, 200)
|
|
139
|
+
const body = await response.json()
|
|
140
|
+
assert.equal(body.ok, true)
|
|
141
|
+
assert.equal(body.root, root)
|
|
142
|
+
assert.equal(body.hits.length, 3, 'perFile=2 from a.txt, plus one from sub/b.txt')
|
|
143
|
+
assert.deepEqual(body.hits[0], { path: join(root, 'a.txt'), line: 3, text: 'needle one' })
|
|
144
|
+
assert.equal(body.truncated, false)
|
|
145
|
+
assert.deepEqual(body.roots, [root])
|
|
146
|
+
assert.equal(typeof body.files, 'number')
|
|
147
|
+
|
|
148
|
+
const empty = await fetch(`${base}${CONTENT_SEARCH_PATH}?sessionId=s1&q=${encodeURIComponent(' ')}`)
|
|
149
|
+
assert.equal(empty.status, 400)
|
|
150
|
+
assert.equal((await empty.json()).code, 'bad-request')
|
|
151
|
+
|
|
152
|
+
const noSession = await fetch(`${base}${CONTENT_SEARCH_PATH}?q=needle`)
|
|
153
|
+
assert.equal(noSession.status, 400)
|
|
154
|
+
|
|
155
|
+
const wrongMethod = await postJson(base, CONTENT_SEARCH_PATH, { sessionId: 's1', q: 'needle' })
|
|
156
|
+
assert.equal(wrongMethod.status, 405)
|
|
157
|
+
}, root)
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
// ── Run-test route: validation ──────────────────────────────────────────────
|
|
161
|
+
|
|
162
|
+
/** A cwd holding one real Maven module (`mod/pom.xml`). */
|
|
163
|
+
async function makeMavenFixture(label) {
|
|
164
|
+
const root = await mkdtemp(join(scratch, `${label}-`))
|
|
165
|
+
await mkdir(join(root, 'mod'), { recursive: true })
|
|
166
|
+
await writeFile(join(root, 'mod', 'pom.xml'), '<project><modelVersion>4.0.0</modelVersion></project>\n')
|
|
167
|
+
return root
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
test('POST /run-test validates sessionId, module containment and the -Dtest selector', async () => {
|
|
171
|
+
const root = await makeMavenFixture('run-test-args')
|
|
172
|
+
await withServer(async ({ base }) => {
|
|
173
|
+
const noSession = await postJson(base, RUN_TEST_PATH, { module: 'mod', test: 'Demo' })
|
|
174
|
+
assert.equal(noSession.status, 400)
|
|
175
|
+
|
|
176
|
+
// No pom.xml under that directory: a clear 404, not a spawn.
|
|
177
|
+
const unknown = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'nope', test: 'Demo' })
|
|
178
|
+
assert.equal(unknown.status, 404)
|
|
179
|
+
assert.equal((await unknown.json()).code, 'unknown-module')
|
|
180
|
+
|
|
181
|
+
// `..` never escapes the session cwd; absolute module paths are refused too.
|
|
182
|
+
const escape = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: '../mod', test: 'Demo' })
|
|
183
|
+
assert.equal(escape.status, 400)
|
|
184
|
+
assert.equal((await escape.json()).code, 'bad-request')
|
|
185
|
+
|
|
186
|
+
const absolute = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: join(root, 'mod'), test: 'Demo' })
|
|
187
|
+
assert.equal(absolute.status, 400)
|
|
188
|
+
|
|
189
|
+
// A selector that is not a plain Maven name is refused before any spawn.
|
|
190
|
+
const metacharacters = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'mod', test: 'Demo; rm -rf /' })
|
|
191
|
+
assert.equal(metacharacters.status, 400)
|
|
192
|
+
assert.equal((await metacharacters.json()).code, 'bad-request')
|
|
193
|
+
|
|
194
|
+
const missingTest = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'mod' })
|
|
195
|
+
assert.equal(missingTest.status, 400)
|
|
196
|
+
|
|
197
|
+
// Status: unknown / missing id are clear errors, other methods a 405.
|
|
198
|
+
const unknownId = await fetch(`${base}${RUN_TEST_PATH}?sessionId=s1&id=rt-nope`)
|
|
199
|
+
assert.equal(unknownId.status, 404)
|
|
200
|
+
assert.equal((await unknownId.json()).code, 'not-found')
|
|
201
|
+
const noId = await fetch(`${base}${RUN_TEST_PATH}?sessionId=s1`)
|
|
202
|
+
assert.equal(noId.status, 400)
|
|
203
|
+
const wrongMethod = await fetch(`${base}${RUN_TEST_PATH}`, { method: 'DELETE' })
|
|
204
|
+
assert.equal(wrongMethod.status, 405)
|
|
205
|
+
}, root)
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
// ── Run-test route: spawning (a `mvn` shim on PATH, no real Maven) ──────────
|
|
209
|
+
|
|
210
|
+
/** A directory holding an executable `mvn` shell script. */
|
|
211
|
+
async function makeMvnShim(label, script) {
|
|
212
|
+
const dir = await mkdtemp(join(scratch, `${label}-`))
|
|
213
|
+
const file = join(dir, 'mvn')
|
|
214
|
+
await writeFile(file, `#!/bin/sh\n${script}\n`)
|
|
215
|
+
await chmod(file, 0o755)
|
|
216
|
+
return dir
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Run `fn` with `prepend` first on PATH, restoring the original PATH after. */
|
|
220
|
+
async function withPath(prepend, fn) {
|
|
221
|
+
const original = process.env.PATH
|
|
222
|
+
process.env.PATH = `${prepend}:${original ?? ''}`
|
|
223
|
+
try {
|
|
224
|
+
return await fn()
|
|
225
|
+
} finally {
|
|
226
|
+
process.env.PATH = original
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Run `fn` with PATH replaced entirely (so no `mvn` can be found). */
|
|
231
|
+
async function withPathOnly(dir, fn) {
|
|
232
|
+
const original = process.env.PATH
|
|
233
|
+
process.env.PATH = dir
|
|
234
|
+
try {
|
|
235
|
+
return await fn()
|
|
236
|
+
} finally {
|
|
237
|
+
process.env.PATH = original
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Poll one run to its terminal state. */
|
|
242
|
+
async function waitForRun(base, id) {
|
|
243
|
+
let body
|
|
244
|
+
const settled = await waitFor(async () => {
|
|
245
|
+
const response = await fetch(`${base}${RUN_TEST_PATH}?sessionId=s1&id=${encodeURIComponent(id)}`)
|
|
246
|
+
assert.equal(response.status, 200)
|
|
247
|
+
body = await response.json()
|
|
248
|
+
return body.running === false
|
|
249
|
+
}, 15000)
|
|
250
|
+
assert.ok(settled, `run ${id} did not finish: ${JSON.stringify(body)}`)
|
|
251
|
+
return body
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
test('POST /run-test spawns mvn and the status poll reports interleaved output', { skip: IS_WINDOWS }, async () => {
|
|
255
|
+
const root = await makeMavenFixture('run-test-spawn')
|
|
256
|
+
const shim = await makeMvnShim('mvn-ok', [
|
|
257
|
+
'echo "STDOUT-ARGS:$*"',
|
|
258
|
+
'echo "STDERR-LINE" 1>&2',
|
|
259
|
+
'echo "STDOUT-LINE"',
|
|
260
|
+
'exit 0',
|
|
261
|
+
].join('\n'))
|
|
262
|
+
await withPath(shim, async () => {
|
|
263
|
+
await withServer(async ({ base }) => {
|
|
264
|
+
const response = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'mod', test: 'Demo#case1' })
|
|
265
|
+
assert.equal(response.status, 200)
|
|
266
|
+
const started = await response.json()
|
|
267
|
+
assert.equal(started.ok, true)
|
|
268
|
+
assert.match(started.id, /^rt-/)
|
|
269
|
+
assert.equal(typeof started.startedAt, 'number')
|
|
270
|
+
assert.equal(started.command,
|
|
271
|
+
"mvn -Pdev -q -pl mod test '-Dtest=Demo#case1' -DfailIfNoTests=false -DskipITs")
|
|
272
|
+
|
|
273
|
+
const finished = await waitForRun(base, started.id)
|
|
274
|
+
assert.equal(finished.status, 'done')
|
|
275
|
+
assert.equal(finished.exitCode, 0)
|
|
276
|
+
assert.equal(finished.running, false)
|
|
277
|
+
assert.equal(typeof finished.durationMs, 'number')
|
|
278
|
+
assert.match(finished.output, /STDOUT-ARGS:-Pdev -q -pl mod test -Dtest=Demo#case1 -DfailIfNoTests=false -DskipITs/)
|
|
279
|
+
assert.match(finished.output, /STDERR-LINE/, 'stderr is folded into the same console')
|
|
280
|
+
assert.match(finished.output, /STDOUT-LINE/)
|
|
281
|
+
|
|
282
|
+
// The profile override reaches the command line.
|
|
283
|
+
const custom = await (await postJson(base, RUN_TEST_PATH, {
|
|
284
|
+
sessionId: 's1', module: 'mod', test: 'Demo', profile: 'ci',
|
|
285
|
+
})).json()
|
|
286
|
+
assert.equal((await waitForRun(base, custom.id)).exitCode, 0)
|
|
287
|
+
assert.match(custom.command, /-Pci /)
|
|
288
|
+
}, root)
|
|
289
|
+
})
|
|
290
|
+
})
|
|
291
|
+
|
|
292
|
+
test('POST /run-test reports a failing run and a missing mvn without throwing', { skip: IS_WINDOWS }, async () => {
|
|
293
|
+
const root = await makeMavenFixture('run-test-fail')
|
|
294
|
+
const shim = await makeMvnShim('mvn-fail', ['echo "BUILD FAILURE" 1>&2', 'exit 3'].join('\n'))
|
|
295
|
+
await withPath(shim, async () => {
|
|
296
|
+
await withServer(async ({ base }) => {
|
|
297
|
+
const started = await (await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'mod', test: 'Demo' })).json()
|
|
298
|
+
const finished = await waitForRun(base, started.id)
|
|
299
|
+
assert.equal(finished.status, 'failed')
|
|
300
|
+
assert.equal(finished.exitCode, 3)
|
|
301
|
+
assert.match(finished.output, /BUILD FAILURE/)
|
|
302
|
+
}, root)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
// No mvn anywhere on PATH: the route still answers, and the run reports why.
|
|
306
|
+
const empty = await mkdtemp(join(scratch, 'empty-path-'))
|
|
307
|
+
await withPathOnly(empty, async () => {
|
|
308
|
+
await withServer(async ({ base }) => {
|
|
309
|
+
const response = await postJson(base, RUN_TEST_PATH, { sessionId: 's1', module: 'mod', test: 'Demo' })
|
|
310
|
+
assert.equal(response.status, 200)
|
|
311
|
+
const started = await response.json()
|
|
312
|
+
assert.match(started.id, /^rt-/)
|
|
313
|
+
const finished = await waitForRun(base, started.id)
|
|
314
|
+
assert.equal(finished.status, 'failed')
|
|
315
|
+
assert.equal(finished.exitCode, null)
|
|
316
|
+
assert.match(finished.output, /mvn not found on PATH/)
|
|
317
|
+
}, root)
|
|
318
|
+
})
|
|
319
|
+
})
|
|
320
|
+
|
|
321
|
+
test('runTestOutput keeps the head and the tail and marks the elided middle', () => {
|
|
322
|
+
const record = {
|
|
323
|
+
head: 'H'.repeat(RUN_TEST_HEAD_CHARS),
|
|
324
|
+
tail: `${'M'.repeat(RUN_TEST_TAIL_CHARS - 4)}TAIL`,
|
|
325
|
+
total: RUN_TEST_HEAD_CHARS + RUN_TEST_TAIL_CHARS + 1000,
|
|
326
|
+
}
|
|
327
|
+
const output = runTestOutput(record)
|
|
328
|
+
assert.ok(output.startsWith('H'.repeat(RUN_TEST_HEAD_CHARS)))
|
|
329
|
+
assert.ok(output.endsWith('TAIL'))
|
|
330
|
+
assert.match(output, /^H+…\(省略 \d+ 字符\)…M+T/)
|
|
331
|
+
// Short output is returned whole (no marker, no loss).
|
|
332
|
+
assert.equal(runTestOutput({ head: 'short', tail: 'short', total: 5 }), 'short')
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
test('the run-test records are bounded', () => {
|
|
336
|
+
assert.equal(RUN_TEST_MAX_RECORDS, 10)
|
|
337
|
+
assert.equal(RUN_TEST_HEAD_CHARS, 4096)
|
|
338
|
+
assert.equal(RUN_TEST_TAIL_CHARS, 400000)
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
// ── Start route: restart ────────────────────────────────────────────────────
|
|
342
|
+
|
|
343
|
+
/** A workspace holding one startable frontend program (a node ticker). */
|
|
344
|
+
async function makeStartableFixture() {
|
|
345
|
+
const root = await mkdtemp(join(scratch, 'restart-'))
|
|
346
|
+
const dir = join(root, 'ui')
|
|
347
|
+
await mkdir(dir, { recursive: true })
|
|
348
|
+
await writeFile(join(dir, 'package.json'), JSON.stringify({
|
|
349
|
+
name: 'tick-app',
|
|
350
|
+
scripts: { dev: 'node -e "process.on(\'SIGTERM\',()=>process.exit(0));setInterval(()=>{},1000)"' },
|
|
351
|
+
}))
|
|
352
|
+
return root
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
test('POST /start with restart:true stops the running instance before starting a fresh one', async () => {
|
|
356
|
+
const root = await makeStartableFixture()
|
|
357
|
+
await withServer(async ({ base }) => {
|
|
358
|
+
const programs = (await (await fetch(`${base}${PROGRAMS_PATH}?sessionId=s1&refresh=1`)).json()).programs
|
|
359
|
+
const program = programs.find((entry) => entry.kind === 'frontend')
|
|
360
|
+
assert.ok(program !== undefined, 'the fixture frontend program is discovered')
|
|
361
|
+
|
|
362
|
+
const first = await (await postJson(base, START_PATH, { sessionId: 's1', id: program.id })).json()
|
|
363
|
+
assert.equal(first.alreadyRunning, false)
|
|
364
|
+
assert.equal(first.program.running.alive, true)
|
|
365
|
+
const logPath = first.program.logPath
|
|
366
|
+
|
|
367
|
+
const restarted = await (await postJson(base, START_PATH, { sessionId: 's1', id: program.id, restart: true })).json()
|
|
368
|
+
assert.equal(restarted.ok, true)
|
|
369
|
+
assert.equal(restarted.restarted, true, 'the previous instance was running, so it was stopped first')
|
|
370
|
+
assert.equal(restarted.program.running.alive, true, 'a fresh instance is up')
|
|
371
|
+
assert.equal(restarted.program.logPath, logPath)
|
|
372
|
+
assert.notEqual(restarted.program.running.pid, first.program.running.pid, 'a new process, not the old one')
|
|
373
|
+
|
|
374
|
+
// Stop it, then restart the stopped row: nothing to stop, so `restarted` is false.
|
|
375
|
+
const stopped = await (await postJson(base, STOP_PATH, { sessionId: 's1', path: logPath })).json()
|
|
376
|
+
assert.equal(stopped.stopped, true)
|
|
377
|
+
const fromStopped = await (await postJson(base, START_PATH, { sessionId: 's1', id: program.id, restart: true })).json()
|
|
378
|
+
assert.equal(fromStopped.restarted, false)
|
|
379
|
+
assert.equal(fromStopped.program.running.alive, true)
|
|
380
|
+
assert.equal((await postJson(base, STOP_PATH, { sessionId: 's1', path: logPath })).status, 200)
|
|
381
|
+
assert.equal(existsSync(`${logPath}${PID_SUFFIX}`), false, 'the pidfile is gone after the last stop')
|
|
382
|
+
}, root)
|
|
383
|
+
})
|