Files
HIPCTF2/tests/backend/theme-loader.spec.ts
T
OpenVelo Agent af3c24275d Scaffold HIPCTF platform (NestJS + Angular)
- Backend: NestJS 10 + TypeORM (better-sqlite3) feature-modular layout.
  - Entities: user, setting, category, challenge, challenge_file, solve,
    refresh_token, blog_post. Auto-run migrations + idempotent 6-system-
    category seed on startup.
  - Argon2id password hashing with policy check; JWT access + rotating
    refresh tokens (HttpOnly cookie); CSRF middleware (SameSite + custom
    X-CSRF-Token header); global JWT auth guard with @Public() opt-out;
    per-IP login backoff + per-IP registration rate limit.
  - Endpoints: auth (login/refresh/logout/csrf), users (first-admin
    registration), system (bootstrap/event status/SSE), admin (guarded
    user CRUD with last-admin invariant), frontend module (uploads +
    SPA fallback).
  - Security: helmet+CSP+HSTS-gated-by-TLS, CORS allowlist, structured
    global exception filter, Zod request validation pipes, OpenAPI 3.1
    served at /api/docs and /api/docs-json, 10 canonical themes under
    backend/themes/.
- Frontend: Angular 17 standalone components, lazy-loaded feature routes,
  signals, functional HttpInterceptorFn (csrf + auth), functional
  CanActivateFn auth guard, HttpOnly-cookie-based auth service.
- Tests: Jest + supertest, 46 tests across 13 suites covering
  migrations, env schema, theme loader, event status, login backoff,
  registration rate limit, ApiError shape, bootstrap integration,
  auth refresh rotation, admin guard, last-admin invariant, SSE flat
  payloads. Single-command runner: `npm test`.
2026-07-21 13:25:49 +00:00

42 lines
2.3 KiB
TypeScript

import * as path from 'path';
import * as fs from 'fs';
import * as os from 'os';
import { ThemeLoaderService } from '../../backend/src/common/utils/theme-loader.service';
describe('ThemeLoaderService', () => {
const config = { get: jest.fn() } as any;
it('falls back to default and warns on unknown theme id', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify({ id: 'classic', name: 'Classic', tokens: { primary: '#000', secondary: '#111', accent: '#222', surface: '#fff', text: '#000', success: '#0f0', warning: '#ff0', danger: '#f00', fontFamily: 'sans', radii: { sm: '1px', md: '2px', lg: '3px' }, spacingScale: [4, 8] } }));
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
const svc = new ThemeLoaderService(config);
svc.onModuleInit();
const t = svc.getTheme('does-not-exist');
expect(t.id).toBe('classic');
expect(t.tokens.primary).toBe('#000');
});
it('loads a valid theme by id', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
const theme = { id: 'classic', name: 'Classic', tokens: { primary: '#000', secondary: '#111', accent: '#222', surface: '#fff', text: '#000', success: '#0f0', warning: '#ff0', danger: '#f00', fontFamily: 'sans', radii: { sm: '1px', md: '2px', lg: '3px' }, spacingScale: [4, 8] } };
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify(theme));
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
const svc = new ThemeLoaderService(config);
svc.onModuleInit();
const t = svc.getTheme('classic');
expect(t.id).toBe('classic');
expect(t.tokens.spacingScale).toEqual([4, 8]);
});
it('skips invalid theme files', () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'themes-'));
fs.writeFileSync(path.join(tmp, 'classic.json'), JSON.stringify({ id: 'classic', name: 'Classic', tokens: { primary: '#000' } })); // missing required
config.get.mockImplementation((k: string, d: any) => (k === 'THEMES_DIR' ? tmp : d));
const svc = new ThemeLoaderService(config);
svc.onModuleInit();
// Falls back to builtins; classic should be findable via builtins
const t = svc.getTheme('classic');
expect(t).toBeDefined();
});
});