7.0 KiB
7.0 KiB
Implementation Plan: Data and run
1. Architectural Reconnaissance
- Codebase style & conventions: npm-workspace TypeScript monorepo with a NestJS 10 backend and Angular 17 standalone frontend. Runtime configuration is validated centrally with Zod in
backend/src/config/env.schema.tsand consumed through NestConfigService; filesystem paths are normalized with Nodepath.resolve, and root scripts delegate to workspace scripts. The feature is not already implemented: persistent defaults still point at/data/hipctf, and the root package has nodevscript. - Data Layer: TypeORM with
better-sqlite3.DatabaseModulecreates the database parent directory before connecting,DatabaseInitServiceruns migrations and seeds, and uploads are written and served from the configured upload directory. No schema migration is required; this job changes only default filesystem locations and development process orchestration. - Test Framework & Structure: Jest 29 with
ts-jest, configured intests/jest.config.jsas backend and frontend projects. Tests live under dedicatedtests/backendandtests/frontendfolders and all run from the repository root withnpm test. Add only focused configuration/script contract tests; no browser, UI, database file, port, or long-running process is required. - Required Tools & Dependencies: Existing Node.js/npm workspace tooling, Angular CLI,
ts-node, TypeScript, Jest, andbetter-sqlite3remain required. Add the root development dependencyconcurrently(and updatepackage-lock.json) so one root command can supervise both long-running workspaces and propagate shutdown/failure.setup.shalready installs root/workspace dependencies vianpm ci; replace its obsolete absolute/data/hipctf/uploadspreparation with project-local./data/uploadspreparation. No global CLI or new system package is required.
2. Impacted Files
- To Modify:
backend/src/config/env.schema.ts— change validatedDATABASE_PATHandUPLOAD_DIRdefaults to./data/db.sqliteand./data/uploads.backend/src/database/database.module.ts— align theConfigService.getdatabase fallback with the canonical project-local database default.backend/src/database/database-init.service.ts— aligngetDbPath()fallback with the canonical project-local database default.backend/src/main.ts— align startup upload-directory fallback with the canonical project-local upload default.backend/src/modules/uploads/uploads.controller.ts— align upload controller fallback with the project-local upload default.backend/src/common/utils/upload.ts— align Multer destination fallback with the project-local upload default.backend/src/frontend/uploads-static.middleware.ts— align static-upload middleware fallback with the project-local upload default.package.json— add rootdevorchestration for backend and frontend and retainstart:devas a compatible alias if desired.package-lock.json— lock the new process-runner dependency and root package metadata.setup.sh— create./data/uploadsrelative to the repository root instead of/data/hipctf/uploads, while retaining dependency installation and builds..gitignore— ignore the root runtimedata/directory rather than the unrelatedbackend/data/path, preventing local database/uploads from entering source control.tests/backend/env-schema.spec.ts— assert the exact project-local data defaults returned by the Zod schema.
- To Create:
tests/backend/root-dev-script.spec.ts— focused static contract test for root development orchestration and both workspace targets.
3. Proposed Changes
- Database / Schema Migration:
- Do not add a TypeORM migration or alter tables. Update the canonical default database location from
/data/hipctf/db.sqliteto./data/db.sqlite. - Replace every hard-coded database fallback outside the schema with the same value so direct/unit construction cannot silently revert to
/datawhen configuration is absent. - Preserve environment overrides such as
DATABASE_PATH=:memory:in tests and custom deployment paths; only defaults change.
- Do not add a TypeORM migration or alter tables. Update the canonical default database location from
- Backend Logic & APIs:
- Change the canonical default upload location from
/data/hipctf/uploadsto./data/uploadsin the environment schema and every defensiveConfigService.getfallback used by startup, upload handling, Multer storage, and static serving. - Continue resolving relative paths through
path.resolve; when launched through the root scripts,./dataresolves from the project root. Preserve recursive directory creation inDatabaseModule,main.ts, upload helpers, and middleware. - Update
setup.shto derive/use the repository-local data directory and createdata/uploads, avoiding writes to the container’s shared/datavolume for normal application defaults. Keep setup idempotent. - Update
.gitignorefor/data/so generated SQLite and uploaded files remain local runtime artifacts. - Add
concurrentlyas a root development dependency and definenpm run devto launchnpm --workspace backend run start:devandnpm --workspace frontend run startin parallel, with clear process names and kill-on-peer-failure/shutdown behavior. Point the existing rootstart:devscript atnpm run devto avoid two conflicting development entry points.
- Change the canonical default upload location from
- Frontend UI Integration:
- No Angular source or API URL changes are needed. The Angular dev server remains on port 4200 while the Nest API remains on port 3000; both become available from one root command.
- Because the frontend uses relative
/apiand/uploadsURLs and no Angular dev-server proxy is currently configured, add a development proxy configuration (and reference it from Angular’sservetarget) routing/apiand/uploadstohttp://localhost:3000. This makes the concurrently started frontend functional rather than merely starting two disconnected processes, including credentialed API calls and SSE streams.
4. Test Strategy
- Target Unit Test File: Extend
tests/backend/env-schema.spec.tswith one success-path assertion that empty input resolves toDATABASE_PATH === './data/db.sqlite'andUPLOAD_DIR === './data/uploads'. Addtests/backend/root-dev-script.spec.tsto read rootpackage.jsonandfrontend/angular.json, asserting thatdevinvokes both workspace development commands and the Angular development serve target references the backend proxy. Both tests execute through the existing single rootnpm testcommand. - Mocking Strategy: No external boundaries need heavy mocks. Parse checked-in JSON directly with Node filesystem APIs; do not spawn
npm run dev, bind ports, launch Angular/Nest, touch/data, or create persistent files. The environment-schema test calls the pure Zod parser. During implementation, follow Red-Green-Refactor, then verify withnpm test, backend/frontend builds, and the repository’s available lint/typecheck commands (no lint script currently exists; the builds provide TypeScript compilation checks).