11import { describe , expect , it } from 'vitest' ;
2+ import { readFile , rm } from 'node:fs/promises' ;
3+ import { join } from 'node:path' ;
4+ import { tmpdir } from 'node:os' ;
25import type { GeneratorOptions } from '../interface.js' ;
36import { NodeProjectBuilder } from './projectBuilder.js' ;
47import type { BuildStep } from './projectBuilder.js' ;
@@ -10,6 +13,9 @@ const options: GeneratorOptions = {
1013 appExtensions : [ ] ,
1114} ;
1215
16+ const tmpDir = join ( tmpdir ( ) , 'cpa-project-builder-test' ) ;
17+ const read = ( p : string ) => readFile ( join ( tmpDir , p ) , 'utf-8' ) ;
18+
1319function spyStep ( tracker : string [ ] , label : string ) : BuildStep {
1420 return {
1521 execute : async ( ) => {
@@ -50,4 +56,31 @@ describe('NodeProjectBuilder', () => {
5056 expect ( builder . addStep ( spyStep ( [ ] , 'x' ) ) ) . toBe ( builder ) ;
5157 expect ( builder . when ( false , ( ) => { } ) ) . toBe ( builder ) ;
5258 } ) ;
59+
60+ it ( 'generates MySQL env example with the non-default host port' , async ( ) => {
61+ await rm ( tmpDir , { recursive : true , force : true } ) ;
62+
63+ await new NodeProjectBuilder ( tmpDir , { ...options , database : 'mysql' } ) . addEnvExample ( ) . build ( ) ;
64+
65+ const content = await read ( '.env.example' ) ;
66+ expect ( content ) . toContain ( 'DATABASE_URL=mysql://app:app@localhost:3307/test-app' ) ;
67+ expect ( content ) . not . toContain ( 'DATABASE_URL=mysql://app:app@localhost:3306/test-app' ) ;
68+
69+ await rm ( tmpDir , { recursive : true , force : true } ) ;
70+ } ) ;
71+
72+ it ( 'generates server entry that retries database startup' , async ( ) => {
73+ await rm ( tmpDir , { recursive : true , force : true } ) ;
74+
75+ await new NodeProjectBuilder ( tmpDir , options ) . addServerEntry ( ) . build ( ) ;
76+
77+ const content = await read ( 'src/index.ts' ) ;
78+ expect ( content ) . toContain ( 'STARTUP_RETRY_ATTEMPTS = 60' ) ;
79+ expect ( content ) . toContain ( 'STARTUP_RETRY_DELAY_MS = 1000' ) ;
80+ expect ( content ) . toContain ( 'async function waitForDatabase()' ) ;
81+ expect ( content ) . toContain ( 'await waitForDatabase()' ) ;
82+ expect ( content ) . toContain ( 'Database is not ready yet' ) ;
83+
84+ await rm ( tmpDir , { recursive : true , force : true } ) ;
85+ } ) ;
5386} ) ;
0 commit comments