dsh-ris-ide 1.6.11 → 1.7.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/README.md +24 -21
- package/lib/client-codemirror.js +309 -35
- package/lib/client.js +2515 -366
- package/lib/index.js +545 -6
- package/package.json +1 -1
- package/test/client-viewer.test.mjs +540 -37
- package/test/host-search.test.mjs +383 -0
|
@@ -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
|
+
})
|