@open-mercato/starter 0.10.1-develop.7994.1.c9f31972aa → 0.10.1-develop.8022.1.b6753de892
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 +50 -14
- package/bin/om-start.mjs +1 -1
- package/package.json +1 -1
- package/src/__tests__/database-write-guard.test.mjs +111 -0
- package/src/__tests__/port-diagnostics.test.mjs +23 -2
- package/src/__tests__/providers.test.mjs +1 -1
- package/src/__tests__/service-compose-security.test.mjs +134 -0
- package/src/__tests__/service-modes.test.mjs +298 -0
- package/src/__tests__/service-selection.test.mjs +317 -0
- package/src/__tests__/setup-streaming.test.mjs +67 -0
- package/src/__tests__/stage-progress.test.mjs +15 -2
- package/src/__tests__/starter.test.mjs +103 -3
- package/src/cli.mjs +116 -20
- package/src/company.mjs +1 -1
- package/src/compose.mjs +9 -3
- package/src/constants.mjs +1 -1
- package/src/database-write-guard.mjs +80 -0
- package/src/infra-preflight.mjs +114 -2
- package/src/init-summary.mjs +63 -0
- package/src/service-modes.mjs +358 -0
- package/src/setup-logs.mjs +1 -1
- package/src/setup-session.mjs +85 -0
- package/src/steps.mjs +87 -10
- package/src/supervise.mjs +2 -2
- package/src/ui.mjs +2 -2
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import os from 'node:os'
|
|
4
|
+
import path from 'node:path'
|
|
5
|
+
import { parseEnv } from 'node:util'
|
|
6
|
+
import { readEnvValue } from '../env-file.mjs'
|
|
7
|
+
import { resolveStackPorts } from '../constants.mjs'
|
|
8
|
+
import test from 'node:test'
|
|
9
|
+
import { configureServiceMode, resolveServiceMode, chooseServiceMode } from '../service-modes.mjs'
|
|
10
|
+
|
|
11
|
+
const fixture = () => fs.mkdtempSync(path.join(os.tmpdir(), 'om-service-modes-'))
|
|
12
|
+
const silent = () => {}
|
|
13
|
+
function containers({ failAt = -1, interruptAt = -1 } = {}) {
|
|
14
|
+
const started = []
|
|
15
|
+
const stopped = []
|
|
16
|
+
class GenericContainer {
|
|
17
|
+
constructor(image) { this.image = image }
|
|
18
|
+
withEnvironment(environment) { this.environment = environment; return this }
|
|
19
|
+
withExposedPorts(port) { this.port = port; this.hostConfig = { PortBindings: { [`${port}/tcp`]: [{ HostPort: '0' }] } }; return this }
|
|
20
|
+
withCommand(command) { this.command = command; return this }
|
|
21
|
+
withHealthCheck(health) { this.health = health; return this }
|
|
22
|
+
withWaitStrategy() { return this }
|
|
23
|
+
async start() {
|
|
24
|
+
if (started.length === failAt) throw new Error('simulated startup failure')
|
|
25
|
+
await this.beforeContainerCreated?.()
|
|
26
|
+
started.push(this)
|
|
27
|
+
if (started.length - 1 === interruptAt) process.emit('SIGINT')
|
|
28
|
+
return this
|
|
29
|
+
}
|
|
30
|
+
getHost() { return '127.0.0.1' }
|
|
31
|
+
getMappedPort() { return this.port + 20000 }
|
|
32
|
+
async stop() { stopped.push(this) }
|
|
33
|
+
}
|
|
34
|
+
return { started, stopped, loader: async () => ({ GenericContainer, Wait: { forHealthCheck() {} } }) }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
test('reuse is the default and conflicting service flags fail', async () => {
|
|
38
|
+
assert.equal(resolveServiceMode([]), 'reuse')
|
|
39
|
+
assert.equal(resolveServiceMode(['--no-infra']), 'reuse')
|
|
40
|
+
assert.equal(await chooseServiceMode({ argv: [], input: {}, output: {} }), 'reuse')
|
|
41
|
+
assert.throws(() => resolveServiceMode(['--ephemeral', '--random-ports']), /Choose only one/)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
test('reuse leaves config and env byte-identical and does not load Testcontainers', async () => {
|
|
45
|
+
const dir = fixture()
|
|
46
|
+
try {
|
|
47
|
+
const original = 'DATABASE_URL=postgres://u:p@remote.example:5432/db?sslmode=require\n'
|
|
48
|
+
fs.writeFileSync(path.join(dir, '.env'), original)
|
|
49
|
+
const env = { DATABASE_URL: original.split('=')[1] }
|
|
50
|
+
const copy = { ...env }
|
|
51
|
+
await configureServiceMode({ mode: 'reuse', projectDir: dir, env, loadTestcontainers() { assert.fail('must not load') } })
|
|
52
|
+
assert.equal(fs.readFileSync(path.join(dir, '.env'), 'utf8'), original)
|
|
53
|
+
assert.deepEqual(env, copy)
|
|
54
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
test('random mode synchronizes both env files, all Redis consumers, credentials and isolated project identity', async () => {
|
|
58
|
+
const dir = fixture()
|
|
59
|
+
try {
|
|
60
|
+
const app = path.join(dir, 'apps', 'mercato'); fs.mkdirSync(app, { recursive: true })
|
|
61
|
+
const rootEnv = path.join(dir, '.env')
|
|
62
|
+
fs.writeFileSync(rootEnv, 'MEILISEARCH_MASTER_KEY=root-key\n')
|
|
63
|
+
fs.writeFileSync(path.join(app, '.env'), 'DATABASE_URL=postgres://owner:p%24%26@remote:5432/db?sslmode=require\nREDIS_URL=rediss://user:secret@remote:6379/2\nDOCUMENTS_COLLAB_REDIS_URL=redis://remote:6380/3\nMEILISEARCH_HOST=https://remote:7700\n')
|
|
64
|
+
const env = {}; let port = 20000
|
|
65
|
+
const result = await configureServiceMode({ mode: 'random', projectDir: app, envFiles: [rootEnv], env, getPort: async () => ++port, log: silent })
|
|
66
|
+
assert.equal(result.noInfra, false)
|
|
67
|
+
assert.equal(result.cleanup, undefined)
|
|
68
|
+
const settings = parseEnv(fs.readFileSync(path.join(app, '.env'), 'utf8'))
|
|
69
|
+
assert.deepEqual({ ...parseEnv(fs.readFileSync(rootEnv, 'utf8')) }, { ...settings })
|
|
70
|
+
assert.equal(new URL(settings.DATABASE_URL).port, settings.POSTGRES_PORT)
|
|
71
|
+
assert.equal(new URL(settings.DATABASE_URL).search, '')
|
|
72
|
+
assert.equal(settings.POSTGRES_USER, 'mercato')
|
|
73
|
+
assert.match(settings.POSTGRES_PASSWORD, /^[a-f0-9]{48}$/)
|
|
74
|
+
assert.notEqual(settings.POSTGRES_PASSWORD, 'p$&')
|
|
75
|
+
assert.equal(decodeURIComponent(new URL(settings.DATABASE_URL).password), settings.POSTGRES_PASSWORD)
|
|
76
|
+
assert.equal(resolveStackPorts(dir, {}).postgres, Number(settings.POSTGRES_PORT))
|
|
77
|
+
assert.equal(new URL(readEnvValue(path.join(app, '.env'), 'DATABASE_URL')).port, settings.POSTGRES_PORT)
|
|
78
|
+
assert.match(settings.MEILISEARCH_API_KEY, /^[a-f0-9]{48}$/)
|
|
79
|
+
assert.notEqual(settings.MEILISEARCH_API_KEY, 'root-key')
|
|
80
|
+
assert.equal(new URL(settings.MEILISEARCH_HOST).protocol, 'http:')
|
|
81
|
+
for (const key of ['REDIS_URL', 'CACHE_REDIS_URL', 'QUEUE_REDIS_URL', 'EVENTS_REDIS_URL', 'DOCUMENTS_COLLAB_REDIS_URL']) {
|
|
82
|
+
const url = new URL(settings[key]); assert.equal(url.port, settings.REDIS_PORT); assert.equal(url.protocol, 'redis:'); assert.equal(url.username, ''); assert.equal(url.password, settings.REDIS_PASSWORD)
|
|
83
|
+
}
|
|
84
|
+
assert.equal(settings.MERCATO_STACK, settings.COMPOSE_PROJECT_NAME)
|
|
85
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
test('random mode rolls back earlier config writes if another env file cannot be written', async () => {
|
|
89
|
+
const dir = fixture()
|
|
90
|
+
try {
|
|
91
|
+
const file = path.join(dir, '.env'); const original = 'DATABASE_URL=postgres://user:pw@remote/db\n'; fs.writeFileSync(file, original)
|
|
92
|
+
const blocker = path.join(dir, 'read-only'); fs.mkdirSync(blocker); fs.writeFileSync(path.join(blocker, '.env'), 'UNRELATED=kept\n'); fs.chmodSync(blocker, 0o500)
|
|
93
|
+
const env = {}; let port = 30000
|
|
94
|
+
await assert.rejects(configureServiceMode({ mode: 'random', projectDir: dir, envFiles: [path.join(blocker, '.env')], env, getPort: async () => ++port, log: silent }))
|
|
95
|
+
assert.equal(fs.readFileSync(file, 'utf8'), original)
|
|
96
|
+
assert.deepEqual(env, {})
|
|
97
|
+
} finally { fs.chmodSync(path.join(dir, 'read-only'), 0o700); fs.rmSync(dir, { recursive: true, force: true }) }
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test('ephemeral services use mapped endpoints and restore original settings without clobbering later edits', async () => {
|
|
101
|
+
const dir = fixture(); const mock = containers()
|
|
102
|
+
try {
|
|
103
|
+
const file = path.join(dir, '.env'); fs.writeFileSync(file, 'DATABASE_URL=postgres://u:p@remote/db\nUNRELATED=kept\n')
|
|
104
|
+
const env = { DATABASE_URL: 'postgres://u:p@remote/db' }
|
|
105
|
+
const result = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, env, loadTestcontainers: mock.loader, log: silent })
|
|
106
|
+
assert.equal(result.noInfra, true)
|
|
107
|
+
assert.equal(new URL(env.DATABASE_URL).port, '25432')
|
|
108
|
+
assert.equal(mock.started.length, 3)
|
|
109
|
+
assert.deepEqual(mock.started[0].environment, { POSTGRES_USER: 'mercato', POSTGRES_PASSWORD: env.POSTGRES_PASSWORD, POSTGRES_DB: 'open_mercato_local' })
|
|
110
|
+
assert.notEqual(env.POSTGRES_PASSWORD, 'p')
|
|
111
|
+
assert.ok(mock.started.every(item => Object.values(item.hostConfig.PortBindings).flat().every(binding => binding.HostIp === '127.0.0.1')))
|
|
112
|
+
assert.ok(mock.started.every(item => /@sha256:[a-f0-9]{64}$/.test(item.image)))
|
|
113
|
+
fs.appendFileSync(file, 'LATER=value\n')
|
|
114
|
+
await result.cleanup(); await result.cleanup()
|
|
115
|
+
assert.equal(mock.stopped.length, 3)
|
|
116
|
+
assert.equal(env.DATABASE_URL, 'postgres://u:p@remote/db')
|
|
117
|
+
assert.equal(env.REDIS_URL, undefined)
|
|
118
|
+
const restored = parseEnv(fs.readFileSync(file, 'utf8'))
|
|
119
|
+
assert.deepEqual({ ...restored }, { DATABASE_URL: 'postgres://u:p@remote/db', UNRELATED: 'kept', LATER: 'value' })
|
|
120
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
for (const options of [{ failAt: 1 }, { interruptAt: 1 }]) {
|
|
124
|
+
test(`ephemeral startup cleans its services after ${options.failAt ? 'failure' : 'interrupt'}`, async () => {
|
|
125
|
+
const dir = fixture(); const mock = containers(options)
|
|
126
|
+
try {
|
|
127
|
+
const original = 'DATABASE_URL=postgres://u:p@remote/db\n'; fs.writeFileSync(path.join(dir, '.env'), original)
|
|
128
|
+
await assert.rejects(configureServiceMode({ mode: 'ephemeral', projectDir: dir, env: {}, loadTestcontainers: mock.loader, log: silent }))
|
|
129
|
+
assert.equal(mock.stopped.length, mock.started.length)
|
|
130
|
+
assert.equal(fs.readFileSync(path.join(dir, '.env'), 'utf8'), original)
|
|
131
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
test('shared service helper is mirrored byte-for-byte into the standalone template', () => {
|
|
136
|
+
assert.deepEqual(fs.readFileSync(new URL('../service-modes.mjs', import.meta.url)), fs.readFileSync(new URL('../../../create-app/template/scripts/dev-service-modes.mjs', import.meta.url)))
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
test('isolated database endpoints discard all remote connection overrides and synchronize fallback credentials', async () => {
|
|
141
|
+
const dir = fixture()
|
|
142
|
+
try {
|
|
143
|
+
fs.writeFileSync(path.join(dir, '.env'), 'DATABASE_URL=postgres://remote/db?host=remote.example&port=5439&user=old&password=old&dbname=remote&sslmode=require\nPOSTGRES_USER=local_user\nPOSTGRES_PASSWORD=local_password\n')
|
|
144
|
+
const env = {}; let port = 23000
|
|
145
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env, getPort: async () => ++port, log: silent })
|
|
146
|
+
const url = new URL(env.DATABASE_URL)
|
|
147
|
+
assert.equal(url.hostname, '127.0.0.1')
|
|
148
|
+
assert.equal(url.search, '')
|
|
149
|
+
assert.equal(url.username, 'mercato')
|
|
150
|
+
assert.equal(url.password, env.POSTGRES_PASSWORD)
|
|
151
|
+
assert.notEqual(url.password, 'local_password')
|
|
152
|
+
assert.equal(url.port, env.POSTGRES_PORT)
|
|
153
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test('ephemeral cleanup restores raw apostrophe and multiline assignments while preserving unrelated later edits', async () => {
|
|
157
|
+
const dir = fixture()
|
|
158
|
+
try {
|
|
159
|
+
const file = path.join(dir, '.env')
|
|
160
|
+
const original = `DATABASE_URL=postgres://u:p@remote/db\nPOSTGRES_PASSWORD="a'b"\nMEILISEARCH_API_KEY="multi\nline"\n`
|
|
161
|
+
fs.writeFileSync(file, original)
|
|
162
|
+
const result = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, env: {}, services: [], loadTestcontainers: async () => ({}), log: silent })
|
|
163
|
+
fs.appendFileSync(file, 'LATER=keep\n')
|
|
164
|
+
await result.cleanup()
|
|
165
|
+
assert.deepEqual({ ...parseEnv(fs.readFileSync(file, 'utf8')) }, { ...parseEnv(original), LATER: 'keep' })
|
|
166
|
+
assert.match(fs.readFileSync(file, 'utf8'), /POSTGRES_PASSWORD="a'b"/)
|
|
167
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
test('cleanup restores other files and process settings after a filesystem failure and can retry', async () => {
|
|
171
|
+
const dir = fixture(); const mock = containers()
|
|
172
|
+
const file = path.join(dir, '.env'); const other = path.join(dir, 'compose.env')
|
|
173
|
+
try {
|
|
174
|
+
const original = 'DATABASE_URL=postgres://u:p@remote/db\n'
|
|
175
|
+
fs.writeFileSync(file, original); fs.writeFileSync(other, 'KEEP=value\n')
|
|
176
|
+
const env = { DATABASE_URL: 'postgres://u:p@remote/db' }
|
|
177
|
+
const result = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, envFiles: [other], env, loadTestcontainers: mock.loader, log: silent })
|
|
178
|
+
fs.appendFileSync(file, 'LATER=keep\n'); fs.chmodSync(file, 0o400)
|
|
179
|
+
await assert.rejects(result.cleanup(), /Could not fully restore/)
|
|
180
|
+
assert.equal(fs.readFileSync(other, 'utf8'), 'KEEP=value\n')
|
|
181
|
+
assert.equal(env.DATABASE_URL, 'postgres://u:p@remote/db')
|
|
182
|
+
assert.equal(mock.stopped.length, 3)
|
|
183
|
+
fs.chmodSync(file, 0o600); await result.cleanup()
|
|
184
|
+
assert.deepEqual({ ...parseEnv(fs.readFileSync(file, 'utf8')) }, { DATABASE_URL: 'postgres://u:p@remote/db', LATER: 'keep' })
|
|
185
|
+
assert.equal(mock.stopped.length, 3)
|
|
186
|
+
} finally { if (fs.existsSync(file)) fs.chmodSync(file, 0o600); fs.rmSync(dir, { recursive: true, force: true }) }
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test('normalized local database identifiers fit the PostgreSQL identifier limit', async () => {
|
|
190
|
+
const dir = fixture()
|
|
191
|
+
try {
|
|
192
|
+
fs.writeFileSync(path.join(dir, '.env'), 'DATABASE_URL=postgres://user%40remote:password@remote/database%40remote\n')
|
|
193
|
+
const env = {}; let port = 24000
|
|
194
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env, getPort: async () => ++port, log: silent })
|
|
195
|
+
assert.ok(Buffer.byteLength(env.POSTGRES_USER) <= 63)
|
|
196
|
+
assert.ok(Buffer.byteLength(env.POSTGRES_DB) <= 63)
|
|
197
|
+
assert.equal(decodeURIComponent(new URL(env.DATABASE_URL).username), env.POSTGRES_USER)
|
|
198
|
+
assert.equal(decodeURIComponent(new URL(env.DATABASE_URL).pathname.slice(1)), env.POSTGRES_DB)
|
|
199
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
test('ephemeral cleanup restores its endpoints when the dev runner persists a database-name override', async () => {
|
|
203
|
+
const dir = fixture(); const mock = containers()
|
|
204
|
+
try {
|
|
205
|
+
const file = path.join(dir, '.env')
|
|
206
|
+
fs.writeFileSync(file, 'DATABASE_URL="postgres://old_user:old_password@existing.example:6543/old_db?sslmode=require"\nOTHER=kept\n')
|
|
207
|
+
const env = {}
|
|
208
|
+
const result = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, env, loadTestcontainers: mock.loader, log: silent })
|
|
209
|
+
const selected = new URL(env.DATABASE_URL); selected.pathname = '/chosen_db'
|
|
210
|
+
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace(/^DATABASE_URL=.*$/m, `DATABASE_URL=${selected.toString()}`) + 'LATER=kept\n')
|
|
211
|
+
await result.cleanup()
|
|
212
|
+
const restored = parseEnv(fs.readFileSync(file, 'utf8')); const url = new URL(restored.DATABASE_URL)
|
|
213
|
+
assert.equal(url.hostname, 'existing.example'); assert.equal(url.port, '6543')
|
|
214
|
+
assert.equal(url.username, 'old_user'); assert.equal(url.password, 'old_password')
|
|
215
|
+
assert.equal(url.pathname, '/chosen_db'); assert.equal(url.search, '?sslmode=require')
|
|
216
|
+
assert.equal(restored.OTHER, 'kept'); assert.equal(restored.LATER, 'kept')
|
|
217
|
+
assert.equal(mock.stopped.length, 3)
|
|
218
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
test('database-name cleanup preserves literal replacement tokens in original credentials', async () => {
|
|
222
|
+
const dir = fixture(); const mock = containers()
|
|
223
|
+
try {
|
|
224
|
+
const file = path.join(dir, '.env')
|
|
225
|
+
fs.writeFileSync(file, 'DATABASE_URL=postgres://u:p$&@remote.example/old_db\n')
|
|
226
|
+
const env = {}
|
|
227
|
+
const result = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, env, loadTestcontainers: mock.loader, log: silent })
|
|
228
|
+
const selected = new URL(env.DATABASE_URL); selected.pathname = '/chosen_db'
|
|
229
|
+
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace(/^DATABASE_URL=.*$/m, () => `DATABASE_URL=${selected.toString()}`))
|
|
230
|
+
await result.cleanup()
|
|
231
|
+
assert.equal(parseEnv(fs.readFileSync(file, 'utf8')).DATABASE_URL, 'postgres://u:p$&@remote.example/chosen_db')
|
|
232
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
233
|
+
})
|
|
234
|
+
|
|
235
|
+
test('random mode keeps owned local credentials across reruns despite ambient remote credentials', async () => {
|
|
236
|
+
const dir = fixture()
|
|
237
|
+
try {
|
|
238
|
+
const remote = { DATABASE_URL: 'postgres://production_user:production_secret@production.example/private', POSTGRES_PASSWORD: 'production_secret', MEILISEARCH_API_KEY: 'production_meili_secret', REDIS_URL: 'rediss://:production_redis_secret@production.example:6379/2', INFRA_BIND_HOST: '0.0.0.0' }
|
|
239
|
+
fs.writeFileSync(path.join(dir, '.env'), Object.entries(remote).map(([key, value]) => `${key}=${value}`).join('\n'))
|
|
240
|
+
let port = 30000
|
|
241
|
+
const first = { ...remote }
|
|
242
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env: first, getPort: async () => ++port, log: silent })
|
|
243
|
+
const saved = parseEnv(fs.readFileSync(path.join(dir, '.env'), 'utf8'))
|
|
244
|
+
for (const key of ['POSTGRES_PASSWORD', 'REDIS_PASSWORD', 'MEILISEARCH_API_KEY']) {
|
|
245
|
+
assert.match(first[key], /^[a-f0-9]{48}$/)
|
|
246
|
+
assert.ok(!Object.values(remote).includes(first[key]))
|
|
247
|
+
}
|
|
248
|
+
assert.equal(first.INFRA_BIND_HOST, '127.0.0.1')
|
|
249
|
+
assert.equal(new URL(first.REDIS_URL).password, first.REDIS_PASSWORD)
|
|
250
|
+
assert.equal(saved.OM_SETUP_LOCAL_POSTGRES_PASSWORD, first.POSTGRES_PASSWORD)
|
|
251
|
+
const second = { ...remote }
|
|
252
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env: second, getPort: async () => ++port, log: silent })
|
|
253
|
+
for (const key of ['POSTGRES_PASSWORD', 'REDIS_PASSWORD', 'MEILISEARCH_API_KEY', 'MERCATO_STACK', 'COMPOSE_PROJECT_NAME']) assert.equal(second[key], first[key])
|
|
254
|
+
assert.equal(new URL(second.DATABASE_URL).hostname, '127.0.0.1')
|
|
255
|
+
assert.equal(new URL(second.DATABASE_URL).password, first.POSTGRES_PASSWORD)
|
|
256
|
+
assert.equal(fs.statSync(path.join(dir, '.env')).mode & 0o777, 0o600)
|
|
257
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
258
|
+
})
|
|
259
|
+
|
|
260
|
+
test('ephemeral sessions generate independent credentials and pin every application image', async () => {
|
|
261
|
+
const dir = fixture(); const mocks = [containers(), containers()]
|
|
262
|
+
try {
|
|
263
|
+
const remote = 'DATABASE_URL=postgres://remote_user:remote_secret@remote.example/private\nMEILISEARCH_API_KEY=remote_meili_secret\n'
|
|
264
|
+
fs.writeFileSync(path.join(dir, '.env'), remote)
|
|
265
|
+
const passwords = []
|
|
266
|
+
for (const mock of mocks) {
|
|
267
|
+
const env = {}
|
|
268
|
+
const session = await configureServiceMode({ mode: 'ephemeral', projectDir: dir, env, loadTestcontainers: mock.loader, log: silent })
|
|
269
|
+
passwords.push(env.POSTGRES_PASSWORD)
|
|
270
|
+
assert.ok(mock.started.every(item => /@sha256:[a-f0-9]{64}$/.test(item.image)))
|
|
271
|
+
assert.equal(mock.started[1].command[2], env.REDIS_PASSWORD)
|
|
272
|
+
assert.equal(mock.started[1].environment.REDISCLI_AUTH, env.REDIS_PASSWORD)
|
|
273
|
+
assert.notEqual(env.POSTGRES_PASSWORD, 'remote_secret')
|
|
274
|
+
assert.notEqual(env.MEILISEARCH_API_KEY, 'remote_meili_secret')
|
|
275
|
+
await session.cleanup()
|
|
276
|
+
assert.equal(fs.readFileSync(path.join(dir, '.env'), 'utf8'), remote)
|
|
277
|
+
}
|
|
278
|
+
assert.notEqual(passwords[0], passwords[1])
|
|
279
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('owned random stacks retain a persisted database-name choice on later isolated reruns', async () => {
|
|
283
|
+
const dir = fixture()
|
|
284
|
+
try {
|
|
285
|
+
fs.writeFileSync(path.join(dir, '.env'), 'DATABASE_URL=postgres://remote:secret@remote.example/private\n')
|
|
286
|
+
let port = 31000; const env = {}
|
|
287
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env, getPort: async () => ++port, log: silent })
|
|
288
|
+
const selected = new URL(env.DATABASE_URL); selected.pathname = '/chosen_local_db'
|
|
289
|
+
const file = path.join(dir, '.env')
|
|
290
|
+
fs.writeFileSync(file, fs.readFileSync(file, 'utf8').replace(/^DATABASE_URL=.*$/m, () => `DATABASE_URL=${selected}`))
|
|
291
|
+
const rerun = { DATABASE_URL: 'postgres://production:production_secret@production.example/private' }
|
|
292
|
+
await configureServiceMode({ mode: 'random', projectDir: dir, env: rerun, getPort: async () => ++port, log: silent })
|
|
293
|
+
assert.equal(new URL(rerun.DATABASE_URL).pathname, '/chosen_local_db')
|
|
294
|
+
assert.equal(rerun.POSTGRES_DB, 'chosen_local_db')
|
|
295
|
+
assert.equal(rerun.POSTGRES_PASSWORD, env.POSTGRES_PASSWORD)
|
|
296
|
+
assert.equal(new URL(rerun.DATABASE_URL).hostname, '127.0.0.1')
|
|
297
|
+
} finally { fs.rmSync(dir, { recursive: true, force: true }) }
|
|
298
|
+
})
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { spawn } from 'node:child_process'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import { PassThrough } from 'node:stream'
|
|
7
|
+
import { test } from 'node:test'
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
9
|
+
|
|
10
|
+
import { selectDefaultServiceMode } from '../infra-preflight.mjs'
|
|
11
|
+
import { ServiceSelectionError, chooseServiceMode } from '../service-modes.mjs'
|
|
12
|
+
import { readServiceSettings, selectStarterServiceMode } from '../setup-session.mjs'
|
|
13
|
+
|
|
14
|
+
const here = path.dirname(fileURLToPath(import.meta.url))
|
|
15
|
+
const repoRoot = path.resolve(here, '..', '..', '..', '..')
|
|
16
|
+
const SERVICES = [
|
|
17
|
+
{ service: 'postgres', port: 5432 },
|
|
18
|
+
{ service: 'redis', port: 6379 },
|
|
19
|
+
{ service: 'meilisearch', port: 7700 },
|
|
20
|
+
]
|
|
21
|
+
const LOCAL_URL = 'postgres://postgres:postgres@localhost:5432/open-mercato'
|
|
22
|
+
const dockerOk = () => ({ ok: true, code: 'ok' })
|
|
23
|
+
const dockerMissing = () => ({ ok: false, code: 'cli-missing', detail: 'docker CLI not found' })
|
|
24
|
+
|
|
25
|
+
function scenario({ answering = [], detect = dockerOk, owners = [], own = new Set(), verify = async () => {}, databaseUrl = LOCAL_URL } = {}) {
|
|
26
|
+
const calls = { verify: 0, containers: 0 }
|
|
27
|
+
return {
|
|
28
|
+
calls,
|
|
29
|
+
run: () => selectDefaultServiceMode({
|
|
30
|
+
databaseUrl,
|
|
31
|
+
services: SERVICES,
|
|
32
|
+
detect,
|
|
33
|
+
portProbe: async (port) => answering.includes(port),
|
|
34
|
+
containerProbe: () => { calls.containers += 1; return owners },
|
|
35
|
+
listOwnContainers: () => own,
|
|
36
|
+
verifyConnections: async () => { calls.verify += 1; return verify() },
|
|
37
|
+
}),
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
test('(a) answering services that verify are reused', async () => {
|
|
42
|
+
const { run, calls } = scenario({ answering: [5432] })
|
|
43
|
+
const selection = await run()
|
|
44
|
+
assert.equal(selection.mode, 'reuse')
|
|
45
|
+
assert.match(selection.reason, /credentials verified; reusing them/)
|
|
46
|
+
assert.equal(calls.verify, 1)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
test('(a) this project\'s own running containers are reused when they verify', async () => {
|
|
50
|
+
const { run } = scenario({ answering: [5432], owners: [{ service: 'postgres', port: 5432, containerName: 'mercato-postgres' }], own: new Set(['mercato-postgres']) })
|
|
51
|
+
assert.equal((await run()).mode, 'reuse')
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('(b) nothing answering with a usable Docker starts Compose on the configured ports', async () => {
|
|
55
|
+
const { run, calls } = scenario()
|
|
56
|
+
const selection = await run()
|
|
57
|
+
assert.equal(selection.mode, 'compose')
|
|
58
|
+
assert.match(selection.reason, /Nothing answers on the configured ports \(postgres :5432, redis :6379, meilisearch :7700\)/)
|
|
59
|
+
assert.equal(calls.verify, 0)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
test('(c) rejected credentials on the configured port select an isolated stack', async () => {
|
|
63
|
+
const { run } = scenario({ answering: [5432], verify: async () => { throw Object.assign(new Error('[setup] PostgreSQL connection failed (DATABASE_URL). The service rejected the configured credentials.'), { kind: 'authentication' }) } })
|
|
64
|
+
const selection = await run()
|
|
65
|
+
assert.equal(selection.mode, 'random')
|
|
66
|
+
assert.match(selection.reason, /did not accept the configured settings \(PostgreSQL connection failed/)
|
|
67
|
+
assert.match(selection.reason, /isolated stack on free ports/)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('(c) another project\'s container is never verified and selects an isolated stack', async () => {
|
|
71
|
+
const { run, calls } = scenario({ answering: [5432], owners: [{ service: 'postgres', port: 5432, containerName: 'other-checkout-postgres' }], own: new Set(['mercato-postgres']) })
|
|
72
|
+
const selection = await run()
|
|
73
|
+
assert.equal(selection.mode, 'random')
|
|
74
|
+
assert.match(selection.reason, /container other-checkout-postgres of another project/)
|
|
75
|
+
assert.equal(calls.verify, 0, 'never connect to another project\'s database')
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
test('(c) a container owner is treated as foreign when this project\'s containers cannot be listed', async () => {
|
|
79
|
+
const { run } = scenario({ answering: [5432], owners: [{ service: 'postgres', port: 5432, containerName: 'mercato-postgres' }], own: null })
|
|
80
|
+
assert.equal((await run()).mode, 'random')
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('(c) a non-PostgreSQL listener on a configured port selects an isolated stack', async () => {
|
|
84
|
+
const { run } = scenario({ answering: [6379] })
|
|
85
|
+
const selection = await run()
|
|
86
|
+
assert.equal(selection.mode, 'random')
|
|
87
|
+
assert.match(selection.reason, /port 6379 \(redis\) is in use while PostgreSQL does not answer at :5432/)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('own Compose containers that do not verify yet are re-run on the configured ports', async () => {
|
|
91
|
+
const { run } = scenario({ answering: [5432], owners: [{ service: 'postgres', port: 5432, containerName: 'mercato-postgres' }], own: new Set(['mercato-postgres']), verify: async () => { throw new Error('Redis connection failed') } })
|
|
92
|
+
assert.equal((await run()).mode, 'compose')
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
test('(d) Docker unusable and nothing answering stops with the Docker problem', async () => {
|
|
96
|
+
const { run } = scenario({ detect: dockerMissing })
|
|
97
|
+
const selection = await run()
|
|
98
|
+
assert.equal(selection.mode, null)
|
|
99
|
+
assert.deepEqual(selection.problem, { code: 'cli-missing', detail: 'docker CLI not found' })
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('(d) Docker unusable and configured services rejecting credentials stops', async () => {
|
|
103
|
+
const { run } = scenario({ answering: [5432], detect: dockerMissing, verify: async () => { throw new Error('rejected') } })
|
|
104
|
+
const selection = await run()
|
|
105
|
+
assert.equal(selection.mode, null)
|
|
106
|
+
assert.match(selection.reason, /Configured services are not usable/)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('(e) Docker unusable but configured services verify: reuse', async () => {
|
|
110
|
+
const { run, calls } = scenario({ answering: [5432], detect: dockerMissing })
|
|
111
|
+
const selection = await run()
|
|
112
|
+
assert.equal(selection.mode, 'reuse')
|
|
113
|
+
assert.match(selection.reason, /Docker is not available \(docker CLI not found\), but the services configured in \.env answer/)
|
|
114
|
+
assert.equal(calls.containers, 0)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
test('a remote DATABASE_URL is the user\'s explicit choice and is reused without probes', async () => {
|
|
118
|
+
let probed = false
|
|
119
|
+
const selection = await selectDefaultServiceMode({ databaseUrl: 'postgres://me:pw@db.example.com:5432/app', services: SERVICES, detect: () => { probed = true; return dockerOk() }, portProbe: async () => { probed = true; return false }, verifyConnections: async () => {} })
|
|
120
|
+
assert.equal(selection.mode, 'reuse')
|
|
121
|
+
assert.equal(probed, false)
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
test('missing database drivers defer verification instead of replacing configured services', async () => {
|
|
125
|
+
const { run } = scenario({ answering: [5432], verify: async () => { throw Object.assign(new Error('missing'), { kind: 'dependency' }) } })
|
|
126
|
+
assert.equal((await run()).mode, 'reuse')
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('explicit service flags bypass the default selection', async () => {
|
|
130
|
+
let selected = false
|
|
131
|
+
const selectDefault = async () => { selected = true; return { mode: 'compose' } }
|
|
132
|
+
for (const [flag, mode] of [['--infra', 'compose'], ['--random-ports', 'random'], ['--ephemeral', 'ephemeral'], ['--no-infra', 'reuse'], ['--reuse-services', 'reuse']]) {
|
|
133
|
+
assert.equal(await chooseServiceMode({ argv: [flag], selectDefault, log: () => {} }), mode)
|
|
134
|
+
}
|
|
135
|
+
assert.equal(selected, false)
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
test('non-interactive runs use the selected default without prompting, even on a TTY', async () => {
|
|
139
|
+
const input = Object.assign(new PassThrough(), { isTTY: true })
|
|
140
|
+
const output = Object.assign(new PassThrough(), { isTTY: true })
|
|
141
|
+
const lines = []
|
|
142
|
+
const mode = await chooseServiceMode({ argv: ['--non-interactive'], input, output, log: (line) => lines.push(line), selectDefault: async () => ({ mode: 'random', reason: 'Configured ports are occupied.' }) })
|
|
143
|
+
assert.equal(mode, 'random')
|
|
144
|
+
assert.deepEqual(lines, ['\n🔎 Configured ports are occupied.\n'])
|
|
145
|
+
assert.equal(output.read(), null, 'no prompt is written')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('a failed default selection throws the Docker problem instead of prompting', async () => {
|
|
149
|
+
await assert.rejects(
|
|
150
|
+
chooseServiceMode({ argv: [], input: {}, output: {}, log: () => {}, selectDefault: async () => ({ mode: null, problem: { code: 'daemon-stopped' }, reason: 'Nothing answers.' }) }),
|
|
151
|
+
(error) => error instanceof ServiceSelectionError && error.problem.code === 'daemon-stopped' && /Nothing answers/.test(error.message),
|
|
152
|
+
)
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('the TTY menu marks the selected mode as default and Enter accepts it', async () => {
|
|
156
|
+
const previousCi = process.env.CI
|
|
157
|
+
delete process.env.CI
|
|
158
|
+
try {
|
|
159
|
+
const input = Object.assign(new PassThrough(), { isTTY: true })
|
|
160
|
+
const output = Object.assign(new PassThrough(), { isTTY: true })
|
|
161
|
+
let prompt = ''
|
|
162
|
+
output.on('data', (chunk) => { prompt += chunk })
|
|
163
|
+
const pending = chooseServiceMode({ argv: [], input, output, log: () => {}, selectDefault: async () => ({ mode: 'compose', reason: 'Nothing answers.' }) })
|
|
164
|
+
setTimeout(() => input.write('\n'), 20)
|
|
165
|
+
assert.equal(await pending, 'compose')
|
|
166
|
+
assert.match(prompt, /\[4\] Docker Compose on configured ports \(default\)/)
|
|
167
|
+
assert.doesNotMatch(prompt, /\[1\] reuse configured services \(default\)/)
|
|
168
|
+
|
|
169
|
+
const second = chooseServiceMode({ argv: [], input, output, log: () => {}, selectDefault: async () => ({ mode: 'compose' }) })
|
|
170
|
+
setTimeout(() => input.write('3\n'), 20)
|
|
171
|
+
assert.equal(await second, 'random')
|
|
172
|
+
} finally {
|
|
173
|
+
if (previousCi !== undefined) process.env.CI = previousCi
|
|
174
|
+
}
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('starter selection reads the app env over root env and examples, with stack ports', async () => {
|
|
178
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-starter-select-'))
|
|
179
|
+
try {
|
|
180
|
+
fs.mkdirSync(path.join(dir, 'apps', 'mercato'), { recursive: true })
|
|
181
|
+
fs.writeFileSync(path.join(dir, 'apps', 'mercato', '.env.example'), 'DATABASE_URL=postgres://example@localhost:5432/example\n')
|
|
182
|
+
fs.writeFileSync(path.join(dir, '.env'), 'POSTGRES_PORT=15432\nREDIS_PORT=16379\n')
|
|
183
|
+
assert.equal(readServiceSettings(dir, {}).DATABASE_URL, 'postgres://example@localhost:5432/example')
|
|
184
|
+
fs.writeFileSync(path.join(dir, 'apps', 'mercato', '.env'), 'DATABASE_URL=postgres://app@localhost:15432/app\n')
|
|
185
|
+
let received
|
|
186
|
+
await selectStarterServiceMode(dir, { env: {}, select: async (options) => { received = options; return { mode: 'reuse' } } })
|
|
187
|
+
assert.equal(received.databaseUrl, 'postgres://app@localhost:15432/app')
|
|
188
|
+
assert.deepEqual(received.services.map((entry) => entry.port), [15432, 16379, 7700])
|
|
189
|
+
} finally {
|
|
190
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
191
|
+
}
|
|
192
|
+
})
|
|
193
|
+
|
|
194
|
+
test('a second starter setup in a checkout with a live dev session stops before any .env write or Compose call', { skip: process.platform === 'win32' ? 'fake docker shim is a POSIX shell script' : false }, async () => {
|
|
195
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-starter-session-'))
|
|
196
|
+
const { acquireDevSession } = await import(pathToFileURL(path.join(repoRoot, 'scripts', 'dev-session-guard.mjs')).href)
|
|
197
|
+
let first
|
|
198
|
+
try {
|
|
199
|
+
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name: 'open-mercato', private: true }))
|
|
200
|
+
fs.mkdirSync(path.join(dir, 'scripts'))
|
|
201
|
+
fs.copyFileSync(path.join(repoRoot, 'scripts', 'dev-session-guard.mjs'), path.join(dir, 'scripts', 'dev-session-guard.mjs'))
|
|
202
|
+
fs.mkdirSync(path.join(dir, 'apps', 'mercato'), { recursive: true })
|
|
203
|
+
const rootEnv = 'POSTGRES_PORT=15432\nREDIS_PORT=16379\nMEILISEARCH_PORT=17700\n'
|
|
204
|
+
const appEnv = 'DATABASE_URL=postgres://mercato:secret@127.0.0.1:15432/app\n'
|
|
205
|
+
fs.writeFileSync(path.join(dir, '.env'), rootEnv)
|
|
206
|
+
fs.writeFileSync(path.join(dir, 'apps', 'mercato', '.env'), appEnv)
|
|
207
|
+
const bin = path.join(dir, 'fake-bin')
|
|
208
|
+
fs.mkdirSync(bin)
|
|
209
|
+
const dockerLog = path.join(dir, 'docker.log')
|
|
210
|
+
fs.writeFileSync(path.join(bin, 'docker'), `#!/bin/sh\nprintf '%s\\n' "$*" >> "${dockerLog}"\nexit 0\n`, { mode: 0o755 })
|
|
211
|
+
first = acquireDevSession({ workspaceRoot: dir, mode: 'setup:hybrid', role: 'starter' })
|
|
212
|
+
|
|
213
|
+
const child = spawn(process.execPath, [path.join(repoRoot, 'packages', 'starter', 'src', 'cli.mjs'), 'up', '--random-ports', '--non-interactive'], {
|
|
214
|
+
cwd: dir,
|
|
215
|
+
env: { PATH: `${bin}${path.delimiter}${path.dirname(process.execPath)}${path.delimiter}/usr/bin${path.delimiter}/bin`, HOME: dir, CI: 'true' },
|
|
216
|
+
})
|
|
217
|
+
let output = ''
|
|
218
|
+
child.stdout.on('data', (chunk) => { output += chunk })
|
|
219
|
+
child.stderr.on('data', (chunk) => { output += chunk })
|
|
220
|
+
const code = await new Promise((resolve) => child.on('close', (exitCode) => resolve(exitCode)))
|
|
221
|
+
|
|
222
|
+
assert.equal(code, 2, output)
|
|
223
|
+
assert.match(output, /Another Open Mercato dev session already owns this workspace/)
|
|
224
|
+
assert.match(output, /No service configuration, \.env file or container was changed/)
|
|
225
|
+
assert.equal(fs.existsSync(dockerLog), false, 'no docker command may run')
|
|
226
|
+
assert.equal(fs.readFileSync(path.join(dir, '.env'), 'utf8'), rootEnv)
|
|
227
|
+
assert.equal(fs.readFileSync(path.join(dir, 'apps', 'mercato', '.env'), 'utf8'), appEnv)
|
|
228
|
+
const owner = JSON.parse(fs.readFileSync(path.join(dir, '.mercato', 'dev-session.lock', 'owner.json'), 'utf8'))
|
|
229
|
+
assert.equal(owner.token, first.token)
|
|
230
|
+
} finally {
|
|
231
|
+
first?.release()
|
|
232
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
233
|
+
}
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test('the starter session hands its token to the dev runner and releases on failure', async () => {
|
|
237
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-starter-own-'))
|
|
238
|
+
try {
|
|
239
|
+
fs.mkdirSync(path.join(dir, 'scripts'))
|
|
240
|
+
fs.copyFileSync(path.join(repoRoot, 'scripts', 'dev-session-guard.mjs'), path.join(dir, 'scripts', 'dev-session-guard.mjs'))
|
|
241
|
+
const { acquireSetupSession } = await import('../setup-session.mjs')
|
|
242
|
+
const session = await acquireSetupSession(dir)
|
|
243
|
+
assert.match(session.childEnv.OM_DEV_SESSION_TOKEN, /^[0-9a-f-]{36}$/i)
|
|
244
|
+
assert.equal(session.childEnv.OM_DEV_SESSION_WORKSPACE_ROOT, fs.realpathSync(dir))
|
|
245
|
+
const { acquireDevSession } = await import(pathToFileURL(path.join(dir, 'scripts', 'dev-session-guard.mjs')).href)
|
|
246
|
+
const devRunner = acquireDevSession({ workspaceRoot: dir, token: session.token, role: 'dev-runner' })
|
|
247
|
+
assert.equal(devRunner.token, session.token, 'the dev runner joins the setup session')
|
|
248
|
+
devRunner.release()
|
|
249
|
+
session.release()
|
|
250
|
+
assert.equal(fs.existsSync(path.join(dir, '.mercato', 'dev-session.lock')), false)
|
|
251
|
+
assert.equal(await acquireSetupSession(path.join(dir, 'missing')), null)
|
|
252
|
+
} finally {
|
|
253
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
254
|
+
}
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
test('the starter setup session uses the checkout recovery and keeps the hard stop when non-interactive', async () => {
|
|
258
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-starter-recovery-'))
|
|
259
|
+
try {
|
|
260
|
+
fs.mkdirSync(path.join(dir, 'scripts'))
|
|
261
|
+
for (const file of ['dev-session-guard.mjs', 'dev-session-recovery.mjs']) {
|
|
262
|
+
fs.copyFileSync(path.join(repoRoot, 'scripts', file), path.join(dir, 'scripts', file))
|
|
263
|
+
}
|
|
264
|
+
const { acquireSetupSession } = await import('../setup-session.mjs')
|
|
265
|
+
const first = await acquireSetupSession(dir, { interactive: false })
|
|
266
|
+
try {
|
|
267
|
+
await assert.rejects(acquireSetupSession(dir, { interactive: false }), /Another Open Mercato dev session already owns this workspace/)
|
|
268
|
+
} finally {
|
|
269
|
+
first.release()
|
|
270
|
+
}
|
|
271
|
+
const lockDirectory = path.join(dir, '.mercato', 'dev-session.lock')
|
|
272
|
+
fs.mkdirSync(path.join(lockDirectory, 'members'), { recursive: true })
|
|
273
|
+
fs.writeFileSync(path.join(lockDirectory, 'owner.json'), JSON.stringify({ token: '00000000-0000-4000-8000-000000000000', pid: 2147483647, mode: 'setup:hybrid', workspaceRoot: fs.realpathSync(dir) }))
|
|
274
|
+
const recovered = await acquireSetupSession(dir, { interactive: false })
|
|
275
|
+
assert.notEqual(recovered.token, '00000000-0000-4000-8000-000000000000', 'a lock left by an exited process is replaced')
|
|
276
|
+
recovered.release()
|
|
277
|
+
} finally {
|
|
278
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
279
|
+
}
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
test('a fresh clone without Docker or services fails within seconds, before yarn install or builds', { skip: process.platform === 'win32' ? 'fake shims are POSIX shell scripts' : false }, async () => {
|
|
283
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'om-starter-fresh-'))
|
|
284
|
+
try {
|
|
285
|
+
fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify({ name: 'open-mercato', private: true }))
|
|
286
|
+
fs.mkdirSync(path.join(dir, 'scripts'))
|
|
287
|
+
fs.copyFileSync(path.join(repoRoot, 'scripts', 'dev-session-guard.mjs'), path.join(dir, 'scripts', 'dev-session-guard.mjs'))
|
|
288
|
+
fs.mkdirSync(path.join(dir, 'apps', 'mercato'), { recursive: true })
|
|
289
|
+
const net = await import('node:net')
|
|
290
|
+
const freePort = () => new Promise((resolve) => { const server = net.createServer(); server.listen(0, '127.0.0.1', () => { const { port } = server.address(); server.close(() => resolve(port)) }) })
|
|
291
|
+
const [postgres, redis, meili] = [await freePort(), await freePort(), await freePort()]
|
|
292
|
+
fs.writeFileSync(path.join(dir, '.env'), `POSTGRES_PORT=${postgres}\nREDIS_PORT=${redis}\nMEILISEARCH_PORT=${meili}\n`)
|
|
293
|
+
fs.writeFileSync(path.join(dir, 'apps', 'mercato', '.env.example'), `DATABASE_URL=postgres://postgres:postgres@localhost:${postgres}/open-mercato\n`)
|
|
294
|
+
const bin = path.join(dir, 'fake-bin')
|
|
295
|
+
fs.mkdirSync(bin)
|
|
296
|
+
const yarnLog = path.join(dir, 'yarn.log')
|
|
297
|
+
fs.writeFileSync(path.join(bin, 'yarn'), `#!/bin/sh\nprintf '%s\\n' "$*" >> "${yarnLog}"\nexit 0\n`, { mode: 0o755 })
|
|
298
|
+
const started = Date.now()
|
|
299
|
+
const child = spawn(process.execPath, [path.join(repoRoot, 'packages', 'starter', 'src', 'cli.mjs'), 'up', '--non-interactive'], {
|
|
300
|
+
cwd: dir,
|
|
301
|
+
env: { PATH: bin, HOME: dir, CI: 'true' },
|
|
302
|
+
})
|
|
303
|
+
let output = ''
|
|
304
|
+
child.stdout.on('data', (chunk) => { output += chunk })
|
|
305
|
+
child.stderr.on('data', (chunk) => { output += chunk })
|
|
306
|
+
const code = await new Promise((resolve) => child.on('close', (exitCode) => resolve(exitCode)))
|
|
307
|
+
assert.equal(code, 2, output)
|
|
308
|
+
assert.ok(Date.now() - started < 15_000, 'must fail within seconds')
|
|
309
|
+
assert.match(output, /Docker is not installed/)
|
|
310
|
+
assert.match(output, /https:\/\/github\.com\/open-mercato\/open-mercato#without-docker/)
|
|
311
|
+
assert.match(output, /node packages\/starter\/bin\/om-start\.mjs up --no-infra/)
|
|
312
|
+
assert.equal(fs.existsSync(yarnLog), false, 'no yarn install or build may run')
|
|
313
|
+
assert.equal(fs.existsSync(path.join(dir, '.mercato', 'dev-session.lock')), false, 'the session is released')
|
|
314
|
+
} finally {
|
|
315
|
+
fs.rmSync(dir, { recursive: true, force: true })
|
|
316
|
+
}
|
|
317
|
+
})
|