@soulbatical/tetra-dev-toolkit 1.20.24 → 1.20.26

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.
@@ -0,0 +1,237 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * tetra-init-smoke
5
+ *
6
+ * Scaffolds a Playwright smoke test suite into e2e/smoke.spec.ts.
7
+ *
8
+ * What it does:
9
+ * 1. Reads frontend/src/app/app/app-config.tsx (if present) and extracts
10
+ * all href values from the navigation config.
11
+ * 2. Reads doppler.yaml to discover the Doppler project name.
12
+ * 3. Reads .ralph/ports.json (or package.json scripts) to find the frontend port.
13
+ * 4. Copies the template from ../templates/playwright-smoke.spec.ts.template
14
+ * into e2e/smoke.spec.ts, substituting:
15
+ * {{NAV_ROUTES}} -> extracted route array
16
+ * {{FRONTEND_PORT}} -> detected frontend port
17
+ * {{DOPPLER_PROJECT}} -> doppler project name
18
+ * 5. Prints next steps: run with --update-snapshots, commit the file + baseline.
19
+ *
20
+ * Usage:
21
+ * npx tetra-init-smoke
22
+ * npx tetra-init-smoke --force (overwrite existing e2e/smoke.spec.ts)
23
+ * npx tetra-init-smoke --dry-run (preview without writing)
24
+ */
25
+
26
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs'
27
+ import { join, dirname } from 'path'
28
+ import { fileURLToPath } from 'url'
29
+
30
+ const projectRoot = process.cwd()
31
+ const args = process.argv.slice(2)
32
+ const force = args.includes('--force') || args.includes('-f')
33
+ const dryRun = args.includes('--dry-run')
34
+
35
+ const __filename = fileURLToPath(import.meta.url)
36
+ const __dirname = dirname(__filename)
37
+
38
+ // -- Helpers ------------------------------------------------------------------
39
+
40
+ function readFileSafe(absPath) {
41
+ try { return readFileSync(absPath, 'utf-8') } catch { return null }
42
+ }
43
+
44
+ function log(msg) { console.log(msg) }
45
+ function warn(msg) { console.warn(' WARNING: ' + msg) }
46
+ function ok(msg) { console.log(' OK: ' + msg) }
47
+
48
+ // -- Step 1: discover navigation routes ---------------------------------------
49
+
50
+ function extractNavRoutes(projectRoot) {
51
+ const candidates = [
52
+ join(projectRoot, 'frontend', 'src', 'app', 'app', 'app-config.tsx'),
53
+ join(projectRoot, 'frontend', 'src', 'app', 'app', 'app-config.ts'),
54
+ join(projectRoot, 'src', 'app', 'app', 'app-config.tsx'),
55
+ join(projectRoot, 'src', 'app', 'app', 'app-config.ts'),
56
+ join(projectRoot, 'app', 'app', 'app-config.tsx'),
57
+ ]
58
+
59
+ for (const candidate of candidates) {
60
+ const content = readFileSafe(candidate)
61
+ if (!content) continue
62
+
63
+ const routes = []
64
+ const hrefRe = /href\s*:\s*['"]([^'"]+)['"]/g
65
+ let m
66
+ while ((m = hrefRe.exec(content)) !== null) {
67
+ const href = m[1]
68
+ if (href.startsWith('/') && !href.includes('://')) {
69
+ routes.push(href)
70
+ }
71
+ }
72
+
73
+ if (routes.length > 0) {
74
+ ok('Found ' + routes.length + ' navigation routes in ' + candidate.replace(projectRoot + '/', ''))
75
+ return routes
76
+ }
77
+ }
78
+
79
+ warn('Could not find app-config.tsx -- NAV_ROUTES will be empty. Add routes manually.')
80
+ return []
81
+ }
82
+
83
+ // -- Step 2: discover Doppler project -----------------------------------------
84
+
85
+ function readDopplerProject(projectRoot) {
86
+ const dopplerPath = join(projectRoot, 'doppler.yaml')
87
+ const content = readFileSafe(dopplerPath)
88
+ if (!content) return null
89
+ const m = content.match(/project:\s*(\S+)/)
90
+ return m ? m[1] : null
91
+ }
92
+
93
+ // -- Step 3: discover frontend port -------------------------------------------
94
+
95
+ function detectFrontendPort(projectRoot) {
96
+ const portsPath = join(projectRoot, '.ralph', 'ports.json')
97
+ const portsContent = readFileSafe(portsPath)
98
+ if (portsContent) {
99
+ try {
100
+ const ports = JSON.parse(portsContent)
101
+ if (ports.frontend_port) return String(ports.frontend_port)
102
+ } catch { /* ignore */ }
103
+ }
104
+
105
+ const envLocal = readFileSafe(join(projectRoot, '.env.local'))
106
+ if (envLocal) {
107
+ const m = envLocal.match(/VITE_PORT\s*=\s*(\d+)/)
108
+ if (m) return m[1]
109
+ }
110
+
111
+ const pkgPath = join(projectRoot, 'frontend', 'package.json')
112
+ const pkg = readFileSafe(pkgPath)
113
+ if (pkg) {
114
+ try {
115
+ const parsed = JSON.parse(pkg)
116
+ const scripts = Object.values(parsed.scripts || {}).join(' ')
117
+ const m = scripts.match(/-p\s+(\d+)/)
118
+ if (m) return m[1]
119
+ } catch { /* ignore */ }
120
+ }
121
+
122
+ return '3001'
123
+ }
124
+
125
+ // -- Step 4: load and fill template -------------------------------------------
126
+
127
+ function findTemplate() {
128
+ const templatePath = join(__dirname, '..', 'templates', 'playwright-smoke.spec.ts.template')
129
+ if (existsSync(templatePath)) return templatePath
130
+
131
+ const nmPath = join(projectRoot, 'node_modules', '@soulbatical', 'tetra-dev-toolkit', 'templates', 'playwright-smoke.spec.ts.template')
132
+ if (existsSync(nmPath)) return nmPath
133
+
134
+ return null
135
+ }
136
+
137
+ function buildOutput(template, opts) {
138
+ const navRoutes = opts.navRoutes
139
+ const frontendPort = opts.frontendPort
140
+ const dopplerProject = opts.dopplerProject
141
+
142
+ const routesArray = navRoutes.length > 0
143
+ ? navRoutes.map(function(r) { return " '" + r + "'," }).join('\n')
144
+ : " // No routes detected -- add them manually, e.g. '/app/dashboard',"
145
+
146
+ return template
147
+ .replace(' // {{NAV_ROUTES}}', routesArray)
148
+ .split('{{NAV_ROUTES}}').join(routesArray)
149
+ .split('{{FRONTEND_PORT}}').join(frontendPort)
150
+ .split('{{DOPPLER_PROJECT}}').join(dopplerProject || '<your-doppler-project>')
151
+ }
152
+
153
+ // -- Main ---------------------------------------------------------------------
154
+
155
+ log('')
156
+ log('Tetra Smoke Test Scaffolder')
157
+ log('='.repeat(50))
158
+ log('')
159
+
160
+ const navRoutes = extractNavRoutes(projectRoot)
161
+ const frontendPort = detectFrontendPort(projectRoot)
162
+ const dopplerProject = readDopplerProject(projectRoot)
163
+
164
+ log(' Routes: ' + navRoutes.length + ' detected')
165
+ log(' Port: ' + frontendPort)
166
+ log(' Doppler: ' + (dopplerProject || '(not found -- doppler.yaml missing)'))
167
+ log('')
168
+
169
+ const templatePath = findTemplate()
170
+ if (!templatePath) {
171
+ console.error(' ERROR: Template not found. Is @soulbatical/tetra-dev-toolkit installed?')
172
+ process.exit(1)
173
+ }
174
+
175
+ const template = readFileSafe(templatePath)
176
+ if (!template) {
177
+ console.error(' ERROR: Could not read template: ' + templatePath)
178
+ process.exit(1)
179
+ }
180
+
181
+ const output = buildOutput(template, { navRoutes, frontendPort, dopplerProject })
182
+ const outputPath = join(projectRoot, 'e2e', 'smoke.spec.ts')
183
+
184
+ if (dryRun) {
185
+ log('DRY RUN -- would write:')
186
+ log(' ' + outputPath)
187
+ log('')
188
+ log('Preview (first 40 lines):')
189
+ log('-'.repeat(50))
190
+ output.split('\n').slice(0, 40).forEach(function(l) { log(' ' + l) })
191
+ log(' ...')
192
+ log('')
193
+ process.exit(0)
194
+ }
195
+
196
+ if (existsSync(outputPath) && !force) {
197
+ console.error(' ERROR: ' + outputPath + ' already exists.')
198
+ console.error(' Use --force to overwrite, or --dry-run to preview.')
199
+ process.exit(1)
200
+ }
201
+
202
+ const e2eDir = join(projectRoot, 'e2e')
203
+ if (!existsSync(e2eDir)) {
204
+ mkdirSync(e2eDir, { recursive: true })
205
+ ok('Created e2e/')
206
+ }
207
+
208
+ writeFileSync(outputPath, output, 'utf-8')
209
+ ok('Written ' + outputPath.replace(projectRoot + '/', ''))
210
+
211
+ log('')
212
+ log('Next steps:')
213
+ log('='.repeat(50))
214
+ log('')
215
+ log(' 1. Install Playwright if not already:')
216
+ log(' npm install --save-dev @playwright/test')
217
+ log(' npx playwright install chromium')
218
+ log('')
219
+ log(' 2. Make sure DEV_TEST_EMAIL and DEV_TEST_PASSWORD exist in Doppler:')
220
+ if (dopplerProject) {
221
+ log(' doppler secrets set DEV_TEST_EMAIL=... --project ' + dopplerProject + ' --config dev_backend')
222
+ log(' doppler secrets set DEV_TEST_PASSWORD=... --project ' + dopplerProject + ' --config dev_backend')
223
+ } else {
224
+ log(' doppler secrets set DEV_TEST_EMAIL=... --project <p> --config dev_backend')
225
+ log(' doppler secrets set DEV_TEST_PASSWORD=... --project <p> --config dev_backend')
226
+ }
227
+ log('')
228
+ log(' 3. Start the dev server, then generate baseline screenshots:')
229
+ log(' doppler run -- npx playwright test e2e/smoke.spec.ts --update-snapshots')
230
+ log('')
231
+ log(' 4. Commit the spec and baseline:')
232
+ log(' git add e2e/smoke.spec.ts e2e/smoke.spec.ts-snapshots/')
233
+ log(' git commit -m "test: add Playwright smoke tests"')
234
+ log('')
235
+ log(' 5. Subsequent runs (CI):')
236
+ log(' doppler run -- npx playwright test e2e/smoke.spec.ts')
237
+ log('')
File without changes