// Load the JIT compiler BEFORE any Angular router/common imports import '@angular/compiler'; import { describe, it, expect, vi, beforeEach } from 'vitest'; // Mock the @angular/ssr/node imports that pull in Node.js http server code // — we only want to test the request handler logic here. vi.mock('@angular/ssr/node', () => ({ AngularNodeAppEngine: class {}, createNodeRequestHandler: (fn: Function) => fn, isMainModule: () => false, writeResponseToNodeResponse: vi.fn(), })); vi.mock('node:http', () => ({ default: { createServer: vi.fn() }, })); describe('SSR server — server.ts', () => { it('should export a default request handler', async () => { const mod = await import('./server'); expect(mod.default).toBeDefined(); }); it('should not export PRERENDERED_ROUTES (all routes now use RenderMode.Server)', async () => { const mod = await import('./server'); // The server.ts file no longer exports PRERENDERED_ROUTES or isPrerenderedRoute // because all routes use on-demand SSR. Verify the file doesn't reference them. const fs = require('fs'); const content = fs.readFileSync('./server.ts', 'utf-8'); expect(content).not.toContain('PRERENDERED_ROUTES'); expect(content).not.toContain('isPrerenderedRoute'); expect(content).not.toContain('AngularAppEngine'); }); it('should have a clean server with lazy engine bootstrapping', async () => { const fs = require('fs'); const content = fs.readFileSync('./server.ts', 'utf-8'); expect(content).toContain('getEngine'); expect(content).toContain('AngularNodeAppEngine'); expect(content).toContain('listen(4200'); }); }); describe('app.server.routes.ts — RenderMode.Server for all routes', () => { it('should use RenderMode.Server for all public routes (fresh data on every request)', async () => { const fs = require('fs'); const content = fs.readFileSync('./src/app/app.server.routes.ts', 'utf-8'); // Every route should use RenderMode.Server const prerenderMatches = content.match(/RenderMode\.Prerender/g); expect(prerenderMatches).toBeNull(); // Should have RenderMode.Server entries const serverMatches = content.match(/RenderMode\.Server/g); expect(serverMatches).not.toBeNull(); expect(serverMatches.length).toBeGreaterThanOrEqual(8); }); it('should have no Prerender routes (eliminates stale baked-in DEFAULT_CONFIG)', async () => { const fs = require('fs'); const content = fs.readFileSync('./src/app/app.server.routes.ts', 'utf-8'); expect(content).not.toContain('RenderMode.Prerender'); }); }); describe('docker-entrypoint.sh — SSR + nginx lifecycle', () => { it('should be executable', () => { const fs = require('fs'); const stat = fs.statSync('./docker-entrypoint.sh'); expect(stat.isFile()).toBe(true); }); it('should start the SSR node process before nginx', () => { const fs = require('fs'); const content = fs.readFileSync('./docker-entrypoint.sh', 'utf-8'); // SSR must start before nginx const ssrStart = content.indexOf('node /app/server/server.mjs'); const nginxStart = content.indexOf('exec nginx'); expect(ssrStart).toBeGreaterThan(-1); expect(nginxStart).toBeGreaterThan(-1); expect(ssrStart).toBeLessThan(nginxStart); }); it('should set SSR_API_BASE_URL for the SSR server', () => { const fs = require('fs'); const content = fs.readFileSync('./docker-entrypoint.sh', 'utf-8'); expect(content).toContain('SSR_API_BASE_URL'); expect(content).toContain('http://localhost:80'); }); it('should wait for SSR server readiness before starting nginx', () => { const fs = require('fs'); const content = fs.readFileSync('./docker-entrypoint.sh', 'utf-8'); expect(content).toContain('Waiting for SSR server to be ready'); expect(content).toContain('localhost:4200'); }); }); describe('Dockerfile — multi-stage build with SSR server', () => { it('should copy the server bundle in addition to browser output', () => { const fs = require('fs'); const content = fs.readFileSync('./Dockerfile', 'utf-8'); expect(content).toMatch(/COPY.*server.*\/app\/server/); }); it('should use a Node.js base image for the serve stage', () => { const fs = require('fs'); const content = fs.readFileSync('./Dockerfile', 'utf-8'); // Second FROM should be Node-based (not plain nginx) const fromLines = content.split('\n').filter(l => l.startsWith('FROM ')); expect(fromLines.length).toBeGreaterThanOrEqual(2); expect(fromLines[1]).toContain('node'); }); it('should install nginx in the serve stage', () => { const fs = require('fs'); const content = fs.readFileSync('./Dockerfile', 'utf-8'); expect(content).toContain('apk add --no-cache nginx'); }); it('should expose port 80', () => { const fs = require('fs'); const content = fs.readFileSync('./Dockerfile', 'utf-8'); expect(content).toContain('EXPOSE 80'); }); }); describe('nginx.conf.template — SSR proxy chain', () => { it('should define an upstream pointing to SSR server on port 4200', () => { const fs = require('fs'); const content = fs.readFileSync('./nginx.conf.template', 'utf-8'); expect(content).toContain('upstream ssr_backend'); expect(content).toContain('127.0.0.1:4200'); }); it('should proxy page requests to the SSR backend', () => { const fs = require('fs'); const content = fs.readFileSync('./nginx.conf.template', 'utf-8'); expect(content).toContain('proxy_pass http://ssr_backend'); }); it('should serve static assets directly from disk with long cache', () => { const fs = require('fs'); const content = fs.readFileSync('./nginx.conf.template', 'utf-8'); expect(content).toContain('location ~*'); expect(content).toContain('js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot|map'); expect(content).toContain('expires 1y'); expect(content).toContain('public, immutable'); }); it('should still proxy /api/ to the upstream backend', () => { const fs = require('fs'); const content = fs.readFileSync('./nginx.conf.template', 'utf-8'); expect(content).toContain('location /api/'); expect(content).toContain('__API_UPSTREAM__'); }); }); describe('main.server.ts — SSR API base URL injection', () => { it('should override APP_CONFIG with SSR-aware apiBaseUrl', () => { const fs = require('fs'); const content = fs.readFileSync('./src/main.server.ts', 'utf-8'); expect(content).toContain('SSR_API_BASE_URL'); expect(content).toContain('ssrAppConfig'); expect(content).toContain('provide: APP_CONFIG'); }); it('should read SSR_API_BASE_URL from environment with fallback', () => { const fs = require('fs'); const content = fs.readFileSync('./src/main.server.ts', 'utf-8'); expect(content).toContain("process.env['SSR_API_BASE_URL']"); expect(content).toContain("http://localhost:80"); }); });