|
| 1 | +import { spawnSync } from 'node:child_process' |
| 2 | +import { fileURLToPath } from 'node:url' |
| 3 | +import { describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +const repoDir = fileURLToPath(new URL('..', import.meta.url)) |
| 6 | + |
| 7 | +function createNonTestEnv(): NodeJS.ProcessEnv { |
| 8 | + const env: NodeJS.ProcessEnv = { ...process.env } |
| 9 | + |
| 10 | + // Make std-env treat this as non-test/non-ci so we exercise the non-interactive path. |
| 11 | + delete env.CI |
| 12 | + delete env.VITEST |
| 13 | + delete env.VITEST_WORKER_ID |
| 14 | + delete env.JEST_WORKER_ID |
| 15 | + delete env.AVA_PATH |
| 16 | + delete env.TAP |
| 17 | + delete env.TEST |
| 18 | + env.NODE_ENV = 'production' |
| 19 | + delete env.npm_lifecycle_event |
| 20 | + |
| 21 | + // Ensure no secret is configured via env. |
| 22 | + delete env.BETTER_AUTH_SECRET |
| 23 | + delete env.NUXT_BETTER_AUTH_SECRET |
| 24 | + |
| 25 | + env.FORCE_COLOR = '0' |
| 26 | + |
| 27 | + return env |
| 28 | +} |
| 29 | + |
| 30 | +describe('promptForSecret', () => { |
| 31 | + it('skips prompting in non-interactive/prepare mode', () => { |
| 32 | + const env = createNonTestEnv() |
| 33 | + |
| 34 | + const script = ` |
| 35 | +import { createJiti } from 'jiti' |
| 36 | +import fs from 'node:fs' |
| 37 | +import os from 'node:os' |
| 38 | +import path from 'node:path' |
| 39 | +
|
| 40 | +const jiti = createJiti(process.cwd(), { interopDefault: true, moduleCache: false }) |
| 41 | +const { promptForSecret } = await jiti.import('./src/module/secret.ts') |
| 42 | +
|
| 43 | +let promptCalls = 0 |
| 44 | +const consola = { |
| 45 | + warn: (...args) => console.log(String(args[0] ?? '')), |
| 46 | + info: () => {}, |
| 47 | + success: () => {}, |
| 48 | + box: () => {}, |
| 49 | + prompt: async () => { |
| 50 | + promptCalls++ |
| 51 | + throw new Error('prompt called') |
| 52 | + }, |
| 53 | +} |
| 54 | +
|
| 55 | +const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), 'nuxt-better-auth-secret-')) |
| 56 | +await promptForSecret(rootDir, consola, { prepare: true }) |
| 57 | +console.log('PROMPT_CALLS=' + promptCalls) |
| 58 | +` |
| 59 | + |
| 60 | + const run = spawnSync(process.execPath, ['--input-type=module', '-e', script], { |
| 61 | + cwd: repoDir, |
| 62 | + env, |
| 63 | + encoding: 'utf8', |
| 64 | + timeout: 60_000, |
| 65 | + }) |
| 66 | + |
| 67 | + expect(run.status, `node script failed:\n${run.stdout}\n${run.stderr}`).toBe(0) |
| 68 | + const output = `${run.stdout}\n${run.stderr}` |
| 69 | + expect(output).toContain('Skipping BETTER_AUTH_SECRET prompt') |
| 70 | + expect(output).toContain('PROMPT_CALLS=0') |
| 71 | + }) |
| 72 | +}) |
0 commit comments