refactor(structure): promote app to repo root + remove redundant workspace cruft

Standard production layout: the OKR app (was nested under AINative_OKR_CASAN5/) is now
the repository root. No more wrapper directory.

- Promote AINative_OKR_CASAN5/* -> repo root (backend/ frontend/ packages/ apps/
  .specify/ docs/ infra/ nginx/ scripts/ + configs). Merge tool dirs: .gitea (kept the
  active deploy ci.yml, added harness-ci.yml + runbooks), .claude (agents/commands +
  launch.json), .github moved up.
- Remove redundant: 00_SUBMISSION_PACKAGE, scattered root notes (FPT_CASAN_Full.md,
  tu-tuong-casan.md, casan-tu-sinh..., casan_harness_assessment.md, source-review...,
  README_CASAN5_REFINED.md), casan-next-plans/ and optimize-docs/ (competition/planning
  artifacts — roadmap + design history preserved in git log / commit messages).
- Update all references to the old layout:
  - .gitea/workflows/{ci,harness-ci}.yml, .github/workflows/{ci,deploy}.yml:
    working-directory .; drop AINative_OKR_CASAN5/ prefix; .specify/{tests,scripts}
    -> packages/casan-harness/... (.specify/logs state kept)
  - .claude/launch.json, .gitea/*-runbook.md: path prefixes
  - CLAUDE.md, README.md: docs/input -> apps/okr/domain/input
  - policy-bundle.yaml: 8 policy paths -> packages/casan-harness/...; manifest re-signed
- secrets-scan.sh: fixture excludes -> new package/domain paths.

Full gate from the new root: PASS=64 FAIL=0 SKIP=3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
thanhnv
2026-07-08 13:26:36 +09:00
co-authored by Claude Opus 4.8
parent 7101af9fd4
commit 36a4812ef3
925 changed files with 410 additions and 18001 deletions
+467
View File
@@ -0,0 +1,467 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import path from 'node:path';
import request from 'supertest';
import { createTestApp, loginToken } from './helpers.js';
// ─── Auth guard ───────────────────────────────────────────────────────────────
test('unauthenticated requests are rejected with 401', async () => {
const { app } = await createTestApp();
try {
await request(app.getHttpServer()).get('/api/v1/objectives').expect(401);
await request(app.getHttpServer()).get('/api/v1/objectives/1').expect(401);
await request(app.getHttpServer()).get('/api/v1/key-results/1').expect(401);
} finally {
await app.close();
}
});
// ─── GET /objectives (list) ───────────────────────────────────────────────────
test('GET /objectives employee sees only own objectives', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
const res = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.expect(200);
assert.equal(res.body.success, true);
assert.ok(Array.isArray(res.body.data));
// Every returned objective must belong to employee (id=3)
assert.ok(res.body.data.every((o: { ownerId: number }) => o.ownerId === 3));
assert.equal(res.body.meta.total, res.body.data.length);
} finally {
await app.close();
}
});
test('GET /objectives manager sees all objectives', async () => {
const { app } = await createTestApp();
try {
const managerToken = await loginToken(app, 'manager');
const employeeToken = await loginToken(app, 'employee');
const managerRes = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${managerToken}`)
.expect(200);
const employeeRes = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${employeeToken}`)
.expect(200);
assert.ok(managerRes.body.data.length > employeeRes.body.data.length);
assert.equal(managerRes.body.data.length, 3);
} finally {
await app.close();
}
});
test('GET /objectives?quarter= filters to matching quarter only', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
const matching = await request(app.getHttpServer())
.get('/api/v1/objectives?quarter=Q2/2026')
.set('Authorization', `Bearer ${token}`)
.expect(200);
const empty = await request(app.getHttpServer())
.get('/api/v1/objectives?quarter=Q1/1900')
.set('Authorization', `Bearer ${token}`)
.expect(200);
assert.ok(matching.body.data.every((o: { quarter: string }) => o.quarter === 'Q2/2026'));
assert.equal(empty.body.data.length, 0);
assert.equal(empty.body.meta.total, 0);
} finally {
await app.close();
}
});
// ─── GET /objectives/:id ──────────────────────────────────────────────────────
test('GET /objectives/:id returns objective with computedProgress and relations', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
const res = await request(app.getHttpServer())
.get('/api/v1/objectives/1')
.set('Authorization', `Bearer ${token}`)
.expect(200);
const data = res.body.data;
assert.equal(data.id, 1);
assert.equal(typeof data.computedProgress, 'number');
// KR1=33, KR2=60 → avg = Math.round(46.5) = 47
assert.equal(data.computedProgress, 47);
assert.ok(data.owner !== undefined);
assert.ok(Array.isArray(data.keyResults));
} finally {
await app.close();
}
});
test('GET /objectives/:id returns 404 for non-existent objective', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
await request(app.getHttpServer())
.get('/api/v1/objectives/999999')
.set('Authorization', `Bearer ${token}`)
.expect(404);
} finally {
await app.close();
}
});
test('GET /objectives/:id returns 403 when employee accesses another owner objective', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// Objective 3 belongs to manager (id=2), not employee (id=3)
await request(app.getHttpServer())
.get('/api/v1/objectives/3')
.set('Authorization', `Bearer ${token}`)
.expect(403);
} finally {
await app.close();
}
});
// ─── POST /objectives ─────────────────────────────────────────────────────────
test('POST /objectives returns 400 for empty title', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
await request(app.getHttpServer())
.post('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.send({ title: '', ownerId: 3, quarter: 'Q1/2026' })
.expect(400);
} finally {
await app.close();
}
});
test('POST /objectives returns 400 for invalid quarter format', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// DTO requires Q[1-4]/YYYY format
await request(app.getHttpServer())
.post('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.send({ title: 'Valid title', ownerId: 3, quarter: '2026-Q2' })
.expect(400);
} finally {
await app.close();
}
});
test('POST /objectives manager creates objective and receives it with NOT_STARTED status', async () => {
const { app, prisma } = await createTestApp();
let createdId: number | undefined;
try {
const token = await loginToken(app, 'manager');
const res = await request(app.getHttpServer())
.post('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.send({ title: 'E2E created objective', ownerId: 3, quarter: 'Q1/2099' })
.expect(201);
createdId = res.body.data.id;
assert.equal(res.body.data.status, 'NOT_STARTED');
assert.equal(res.body.data.ownerId, 3);
assert.ok(typeof createdId === 'number');
} finally {
if (createdId) await prisma.objective.delete({ where: { id: createdId } });
await app.close();
}
});
test('POST /objectives returns 403 when employee creates for another user', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// employee (id=3) trying to create objective for employee2 (id=4)
await request(app.getHttpServer())
.post('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.send({ title: 'Sneaky objective', ownerId: 4, quarter: 'Q1/2099' })
.expect(403);
} finally {
await app.close();
}
});
test('POST /objectives returns 404 for non-existent owner', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
await request(app.getHttpServer())
.post('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.send({ title: 'Ghost owner objective', ownerId: 999999, quarter: 'Q1/2099' })
.expect(404);
} finally {
await app.close();
}
});
// ─── GET /key-results/:id ─────────────────────────────────────────────────────
test('GET /key-results/:id returns KR with nested objective', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
const res = await request(app.getHttpServer())
.get('/api/v1/key-results/1')
.set('Authorization', `Bearer ${token}`)
.expect(200);
assert.equal(res.body.success, true);
assert.equal(res.body.data.id, 1);
assert.ok(res.body.data.objective !== undefined);
} finally {
await app.close();
}
});
test('GET /key-results/:id returns 404 for non-existent KR', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
await request(app.getHttpServer())
.get('/api/v1/key-results/999999')
.set('Authorization', `Bearer ${token}`)
.expect(404);
} finally {
await app.close();
}
});
test('GET /key-results/:id returns 403 when employee reads another owner KR', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// KR 3 belongs to objective 2 (ownerId=employee2/id=4)
await request(app.getHttpServer())
.get('/api/v1/key-results/3')
.set('Authorization', `Bearer ${token}`)
.expect(403);
} finally {
await app.close();
}
});
// ─── POST /key-results ────────────────────────────────────────────────────────
test('POST /key-results employee creates KR on own objective', async () => {
const { app, prisma } = await createTestApp();
let createdId: number | undefined;
try {
const token = await loginToken(app, 'employee');
const res = await request(app.getHttpServer())
.post('/api/v1/key-results')
.set('Authorization', `Bearer ${token}`)
.send({
objectiveId: 1,
title: 'E2E created key result',
progress: 0,
startValue: 0,
targetValue: 5,
deadline: '2099-12-31',
})
.expect(201);
createdId = res.body.data.id;
assert.equal(res.body.data.progress, 0);
assert.equal(res.body.data.objectiveId, 1);
} finally {
if (createdId) await prisma.keyResult.delete({ where: { id: createdId } });
await app.close();
}
});
test('POST /key-results returns 403 when employee creates KR on another owner objective', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// Objective 3 belongs to manager (id=2)
await request(app.getHttpServer())
.post('/api/v1/key-results')
.set('Authorization', `Bearer ${token}`)
.send({
objectiveId: 3,
title: 'Sneaky KR',
progress: 0,
startValue: 0,
targetValue: 1,
deadline: '2099-12-31',
})
.expect(403);
} finally {
await app.close();
}
});
// ─── PATCH /key-results/:id/progress ─────────────────────────────────────────
test('PATCH /key-results/:id/progress updates progress and returns updated KR', async () => {
const { app, prisma } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
const res = await request(app.getHttpServer())
.patch('/api/v1/key-results/2/progress')
.set('Authorization', `Bearer ${token}`)
.send({ progress: 70, comment: 'E2E progress update' })
.expect(200);
assert.equal(res.body.data.progress, 70);
} finally {
await prisma.keyResult.update({ where: { id: 2 }, data: { progress: 60 } });
await prisma.progressUpdate.deleteMany({ where: { keyResultId: 2, progress: 70 } });
await app.close();
}
});
test('PATCH /key-results/:id/progress returns 400 for out-of-range values', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
await request(app.getHttpServer())
.patch('/api/v1/key-results/1/progress')
.set('Authorization', `Bearer ${token}`)
.send({ progress: -1 })
.expect(400);
await request(app.getHttpServer())
.patch('/api/v1/key-results/1/progress')
.set('Authorization', `Bearer ${token}`)
.send({ progress: 101 })
.expect(400);
} finally {
await app.close();
}
});
test('PATCH /key-results/:id/progress returns 403 when employee updates another owner KR', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
// KR 3 belongs to employee2's objective
await request(app.getHttpServer())
.patch('/api/v1/key-results/3/progress')
.set('Authorization', `Bearer ${token}`)
.send({ progress: 50 })
.expect(403);
} finally {
await app.close();
}
});
test('PATCH /key-results/:id/progress returns 404 for non-existent KR', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
await request(app.getHttpServer())
.patch('/api/v1/key-results/999999/progress')
.set('Authorization', `Bearer ${token}`)
.send({ progress: 50 })
.expect(404);
} finally {
await app.close();
}
});
// ─── Golden regression ────────────────────────────────────────────────────────
test('golden: manager objective list does not drift', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
const response = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.expect(200);
const canonical = JSON.stringify(
{
success: response.body.success,
data: response.body.data.map((o: { id: number; title: string; quarter: string; status: string; keyResults: { id: number; title: string; progress: number }[] }) => ({
id: o.id,
title: o.title,
quarter: o.quarter,
status: o.status,
keyResults: o.keyResults.map((kr) => ({ id: kr.id, title: kr.title, progress: kr.progress })),
})),
meta: response.body.meta,
},
null,
2,
);
const fixture = await fs.readFile(path.resolve('test/golden/objectives.manager.json'), 'utf8');
assert.equal(`${canonical}\n`, fixture, 'Manager objective list response drifted from golden fixture');
} finally {
await app.close();
}
});
test('golden: employee objective list does not drift', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'employee');
const response = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.expect(200);
const canonical = JSON.stringify(
{
success: response.body.success,
data: response.body.data.map((o: { id: number; title: string; quarter: string; status: string; keyResults: { id: number; title: string; progress: number }[] }) => ({
id: o.id,
title: o.title,
quarter: o.quarter,
status: o.status,
keyResults: o.keyResults.map((kr) => ({ id: kr.id, title: kr.title, progress: kr.progress })),
})),
meta: response.body.meta,
},
null,
2,
);
const fixture = await fs.readFile(path.resolve('test/golden/objectives.employee.json'), 'utf8');
assert.equal(`${canonical}\n`, fixture, 'Employee objective list response drifted from golden fixture');
} finally {
await app.close();
}
});
test('golden: objective detail (GET /objectives/1) does not drift', async () => {
const { app } = await createTestApp();
try {
const token = await loginToken(app, 'manager');
const response = await request(app.getHttpServer())
.get('/api/v1/objectives/1')
.set('Authorization', `Bearer ${token}`)
.expect(200);
const data = response.body.data;
const canonical = JSON.stringify(
{
success: response.body.success,
data: {
id: data.id,
title: data.title,
quarter: data.quarter,
status: data.status,
computedProgress: data.computedProgress,
keyResults: data.keyResults.map((kr: { id: number; title: string; progress: number }) => ({
id: kr.id,
title: kr.title,
progress: kr.progress,
})),
},
},
null,
2,
);
const fixture = await fs.readFile(path.resolve('test/golden/objective-detail.json'), 'utf8');
assert.equal(`${canonical}\n`, fixture, 'Objective detail response drifted from golden fixture');
} finally {
await app.close();
}
});
+22
View File
@@ -0,0 +1,22 @@
{
"success": true,
"data": {
"id": 1,
"title": "POC AI for SQL Injection prevention",
"quarter": "Q2/2026",
"status": "IN_PROGRESS",
"computedProgress": 47,
"keyResults": [
{
"id": 1,
"title": "Complete 3 POC sessions with security team",
"progress": 33
},
{
"id": 2,
"title": "Reduce manual SQL injection review effort by 30%",
"progress": 60
}
]
}
}
@@ -0,0 +1,26 @@
{
"success": true,
"data": [
{
"id": 1,
"title": "POC AI for SQL Injection prevention",
"quarter": "Q2/2026",
"status": "IN_PROGRESS",
"keyResults": [
{
"id": 1,
"title": "Complete 3 POC sessions with security team",
"progress": 33
},
{
"id": 2,
"title": "Reduce manual SQL injection review effort by 30%",
"progress": 60
}
]
}
],
"meta": {
"total": 1
}
}
@@ -0,0 +1,57 @@
{
"success": true,
"data": [
{
"id": 1,
"title": "POC AI for SQL Injection prevention",
"quarter": "Q2/2026",
"status": "IN_PROGRESS",
"keyResults": [
{
"id": 1,
"title": "Complete 3 POC sessions with security team",
"progress": 33
},
{
"id": 2,
"title": "Reduce manual SQL injection review effort by 30%",
"progress": 60
}
]
},
{
"id": 2,
"title": "AI for All enablement across department",
"quarter": "Q2/2026",
"status": "NOT_STARTED",
"keyResults": [
{
"id": 3,
"title": "Certify 100 department members on AI for All",
"progress": 0
}
]
},
{
"id": 3,
"title": "Improve OKR operating cadence",
"quarter": "Q2/2026",
"status": "IN_PROGRESS",
"keyResults": [
{
"id": 4,
"title": "Reach 90% weekly OKR update compliance",
"progress": 75
},
{
"id": 5,
"title": "Resolve stale OKR reports within two business days",
"progress": 45
}
]
}
],
"meta": {
"total": 3
}
}
+30
View File
@@ -0,0 +1,30 @@
import { ValidationPipe } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import type { INestApplication } from '@nestjs/common';
import { PrismaService } from '../src/prisma/prisma.service.js';
import { AppModule } from '../src/app.module.js';
export async function createTestApp(): Promise<{ app: INestApplication; prisma: PrismaService }> {
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
const app = moduleRef.createNestApplication();
app.setGlobalPrefix('api/v1');
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
await app.init();
return { app, prisma: app.get(PrismaService) };
}
export async function loginToken(app: INestApplication, username = 'employee'): Promise<string> {
const request = await import('supertest');
const response = await request
.default(app.getHttpServer())
.post('/api/v1/auth/login')
.send({ username, password: 'Password@123' })
.expect(201);
return response.body.data.token as string;
}
+165
View File
@@ -0,0 +1,165 @@
/**
* LLM-judge gate: sends live API responses to Claude and fails if the model
* returns REJECTED. This catches semantic breakage that structural assertions miss
* (e.g. scrambled titles, impossible status/progress combinations, data leakage).
*
* Requires ANTHROPIC_API_KEY to be set. The test is skipped — not failed — when
* the key is absent, so local dev without a key still passes CI.
*
* REJECTED→fix cycle:
* 1. CI logs the judge verdict and reason.
* 2. Developer reads the reason, fixes the business logic or seed data.
* 3. CI re-runs; the judge issues ACCEPTED once invariants are restored.
*/
import test from 'node:test';
import assert from 'node:assert/strict';
import request from 'supertest';
import { createTestApp, loginToken } from './helpers.js';
const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
const JUDGE_MODEL = 'claude-haiku-4-5-20251001';
interface JudgeVerdict {
verdict: 'ACCEPTED' | 'REJECTED';
reason: string;
}
async function callLlmJudge(prompt: string): Promise<JudgeVerdict> {
const response = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'x-api-key': ANTHROPIC_API_KEY!,
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
},
body: JSON.stringify({
model: JUDGE_MODEL,
max_tokens: 512,
temperature: 0,
messages: [{ role: 'user', content: prompt }],
}),
});
if (!response.ok) {
const body = await response.text();
throw new Error(`Anthropic API error ${response.status}: ${body}`);
}
const result = await response.json() as { content: Array<{ type: string; text: string }> };
const text = result.content.find((c) => c.type === 'text')?.text ?? '';
// Extract JSON object from the model response (model may wrap it in prose)
const match = text.match(/\{[\s\S]*"verdict"[\s\S]*\}/);
if (!match) {
throw new Error(`LLM judge returned unparseable response:\n${text}`);
}
return JSON.parse(match[0]) as JudgeVerdict;
}
function buildJudgePrompt(label: string, responseJson: unknown): string {
const serialized = JSON.stringify(responseJson, null, 2);
return `You are a strict quality gate for an OKR (Objectives and Key Results) management API.
Evaluate the API response below for the endpoint: ${label}
Check ALL of the following invariants:
1. \`success\` field is exactly boolean \`true\`.
2. \`data\` exists and is a non-empty array (for list endpoints) or a non-null object (for detail endpoints).
3. For list responses: \`meta.total\` equals \`data.length\`.
4. Each objective has: id (positive integer), title (non-empty string), quarter (format Q[1-4]/YYYY), status (one of NOT_STARTED, IN_PROGRESS, COMPLETED), keyResults (array).
5. Each keyResult has: id (positive integer), title (non-empty string), progress (integer 0-100 inclusive).
6. Status-progress invariant — for each objective:
- If ALL keyResults have progress === 0 → status MUST be NOT_STARTED.
- If ALL keyResults have progress === 100 → status MUST be COMPLETED.
- Otherwise → status MUST be IN_PROGRESS.
7. Titles must NOT contain placeholder or test noise (e.g. "undefined", "null", "TODO", "string", "untitled", "test objective").
API response:
\`\`\`json
${serialized}
\`\`\`
Respond with a JSON object ONLY — no prose before or after:
{"verdict": "ACCEPTED" or "REJECTED", "reason": "<one sentence explaining pass or the first failing invariant>"}`;
}
// Skip gracefully when the API key is absent
const skip = !ANTHROPIC_API_KEY && 'Set ANTHROPIC_API_KEY to enable the LLM-judge gate';
test(
'LLM judge: manager objective list passes all semantic invariants',
{ skip },
async () => {
const { app } = await createTestApp();
let verdict: JudgeVerdict;
try {
const token = await loginToken(app, 'manager');
const res = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.expect(200);
verdict = await callLlmJudge('GET /api/v1/objectives (manager)', res.body);
} finally {
await app.close();
}
if (verdict!.verdict === 'REJECTED') {
assert.fail(`LLM judge REJECTED manager objective list: ${verdict!.reason}`);
}
},
);
test(
'LLM judge: employee objective list passes all semantic invariants',
{ skip },
async () => {
const { app } = await createTestApp();
let verdict: JudgeVerdict;
try {
const token = await loginToken(app, 'employee');
const res = await request(app.getHttpServer())
.get('/api/v1/objectives')
.set('Authorization', `Bearer ${token}`)
.expect(200);
verdict = await callLlmJudge('GET /api/v1/objectives (employee)', res.body);
} finally {
await app.close();
}
if (verdict!.verdict === 'REJECTED') {
assert.fail(`LLM judge REJECTED employee objective list: ${verdict!.reason}`);
}
},
);
test(
'LLM judge: objective detail passes all semantic invariants',
{ skip },
async () => {
const { app } = await createTestApp();
let verdict: JudgeVerdict;
try {
const token = await loginToken(app, 'manager');
const res = await request(app.getHttpServer())
.get('/api/v1/objectives/1')
.set('Authorization', `Bearer ${token}`)
.expect(200);
// Normalize: present as a single-item list so the judge uses the same invariant set
const normalized = {
success: res.body.success,
data: [res.body.data],
meta: { total: 1 },
};
verdict = await callLlmJudge('GET /api/v1/objectives/1 (detail, normalized to list form)', normalized);
} finally {
await app.close();
}
if (verdict!.verdict === 'REJECTED') {
assert.fail(`LLM judge REJECTED objective detail: ${verdict!.reason}`);
}
},
);
+403
View File
@@ -0,0 +1,403 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { JwtService } from '@nestjs/jwt';
import { ForbiddenException, NotFoundException, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../src/auth/auth.service.js';
import type { JwtUser } from '../src/common/auth.types.js';
import { KeyResultsService } from '../src/key-results/key-results.service.js';
import { ObjectivesService } from '../src/objectives/objectives.service.js';
import { PrismaService } from '../src/prisma/prisma.service.js';
// Seeded user identities (from prisma/seed.ts insertion order)
const managerUser: JwtUser = { sub: 2, email: 'manager@okr.local', role: 'MANAGER', name: 'Nguyen Van Manager' };
const employeeUser: JwtUser = { sub: 3, email: 'employee@okr.local', role: 'EMPLOYEE', name: 'Nguyen Van A' };
const employee2User: JwtUser = { sub: 4, email: 'employee2@okr.local', role: 'EMPLOYEE', name: 'Tran Thi B' };
// ─── Auth ───────────────────────────────────────────────────────────────────
test('AuthService rejects invalid password with UnauthorizedException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new AuthService(prisma, new JwtService({ secret: 'test-secret' }));
try {
await assert.rejects(() => service.login('employee', 'wrong-password'), UnauthorizedException);
} finally {
await prisma.$disconnect();
}
});
test('AuthService rejects non-existent user with UnauthorizedException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new AuthService(prisma, new JwtService({ secret: 'test-secret' }));
try {
await assert.rejects(() => service.login('nobody@ghost.local', 'Password@123'), UnauthorizedException);
} finally {
await prisma.$disconnect();
}
});
test('AuthService accepts correct credentials and returns signed JWT', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new AuthService(prisma, new JwtService({ secret: 'test-secret' }));
try {
const result = await service.login('employee', 'Password@123');
assert.equal(result.user.email, 'employee@okr.local');
assert.equal(result.user.role, 'EMPLOYEE');
assert.ok(result.token.length > 20, 'JWT must be non-trivially long');
// Token has 3 dot-separated segments
assert.equal(result.token.split('.').length, 3);
} finally {
await prisma.$disconnect();
}
});
test('AuthService accepts login by email as well as username', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new AuthService(prisma, new JwtService({ secret: 'test-secret' }));
try {
const byUsername = await service.login('manager', 'Password@123');
const byEmail = await service.login('manager@okr.local', 'Password@123');
assert.equal(byUsername.user.id, byEmail.user.id);
} finally {
await prisma.$disconnect();
}
});
// ─── ObjectivesService.list ──────────────────────────────────────────────────
test('ObjectivesService.list employee sees only own objectives', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
const results = await service.list(employeeUser);
assert.ok(results.length > 0, 'Employee must have at least one objective');
assert.ok(
results.every((o) => o.ownerId === employeeUser.sub),
'Every returned objective must belong to the employee',
);
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.list manager sees all objectives', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
const managerResults = await service.list(managerUser);
const employeeResults = await service.list(employeeUser);
assert.ok(managerResults.length > employeeResults.length, 'Manager must see more objectives than employee');
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.list quarter filter narrows results to matching quarter only', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
const all = await service.list(managerUser);
const filtered = await service.list(managerUser, 'Q2/2026');
const empty = await service.list(managerUser, 'Q1/1900');
assert.ok(filtered.length <= all.length);
assert.ok(filtered.every((o) => o.quarter === 'Q2/2026'), 'All filtered results must match the requested quarter');
assert.equal(empty.length, 0, 'No objectives should match a future non-existent quarter');
} finally {
await prisma.$disconnect();
}
});
// ─── ObjectivesService.getById ───────────────────────────────────────────────
test('ObjectivesService.getById returns objective with computedProgress', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
// Objective 1: KR1=33, KR2=60 → avg = Math.round((33+60)/2) = 47
const result = await service.getById(1, managerUser);
assert.equal(result.id, 1);
assert.equal(result.computedProgress, 47);
assert.ok(result.keyResults.length === 2);
assert.ok(result.owner !== undefined, 'Owner relation must be loaded');
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.getById employee accessing own objective succeeds', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
// Objective 1 is owned by employee (id=3)
const result = await service.getById(1, employeeUser);
assert.equal(result.ownerId, employeeUser.sub);
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.getById employee accessing another user objective throws ForbiddenException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
// Objective 3 belongs to manager (id=2), not employee (id=3)
await assert.rejects(() => service.getById(3, employeeUser), ForbiddenException);
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.getById non-existent id throws NotFoundException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
await assert.rejects(() => service.getById(999999, managerUser), NotFoundException);
} finally {
await prisma.$disconnect();
}
});
// ─── ObjectivesService.create ────────────────────────────────────────────────
test('ObjectivesService.create sets status to NOT_STARTED and loads relations', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
let createdId: number | undefined;
try {
const result = await service.create(
{ title: 'Test objective for create', ownerId: employeeUser.sub, quarter: 'Q1/2099' },
managerUser,
);
createdId = result.id;
assert.equal(result.status, 'NOT_STARTED');
assert.equal(result.ownerId, employeeUser.sub);
assert.equal(result.quarter, 'Q1/2099');
assert.ok(result.owner !== undefined, 'Owner relation must be eagerly loaded');
assert.deepEqual(result.keyResults, [], 'New objective must have no key results');
} finally {
if (createdId) await prisma.objective.delete({ where: { id: createdId } });
await prisma.$disconnect();
}
});
test('ObjectivesService.create employee creating for another user throws ForbiddenException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
await assert.rejects(
() =>
service.create(
{ title: 'Sneaky objective', ownerId: employee2User.sub, quarter: 'Q1/2099' },
employeeUser,
),
ForbiddenException,
);
} finally {
await prisma.$disconnect();
}
});
test('ObjectivesService.create with non-existent owner throws NotFoundException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new ObjectivesService(prisma);
try {
await assert.rejects(
() => service.create({ title: 'Ghost objective', ownerId: 999999, quarter: 'Q1/2099' }, managerUser),
NotFoundException,
);
} finally {
await prisma.$disconnect();
}
});
// ─── KeyResultsService.getById ───────────────────────────────────────────────
test('KeyResultsService.getById returns KR with nested objective and owner', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
// KR 1 belongs to objective 1 which is owned by employee
const result = await service.getById(1, employeeUser);
assert.equal(result.id, 1);
assert.ok(typeof result.progress === 'number');
assert.ok(result.objective !== undefined, 'Objective relation must be loaded');
assert.ok(result.objective.owner !== undefined, 'Owner relation must be loaded inside objective');
} finally {
await prisma.$disconnect();
}
});
test('KeyResultsService.getById employee reading another owner KR throws ForbiddenException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
// KR 3 belongs to objective 2 (ownerId=employee2/id=4), not employee (id=3)
await assert.rejects(() => service.getById(3, employeeUser), ForbiddenException);
} finally {
await prisma.$disconnect();
}
});
test('KeyResultsService.getById non-existent id throws NotFoundException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
await assert.rejects(() => service.getById(999999, managerUser), NotFoundException);
} finally {
await prisma.$disconnect();
}
});
// ─── KeyResultsService.updateProgress ────────────────────────────────────────
test('KeyResultsService.updateProgress blocks employee updating KR from another owner', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
// KR 3 belongs to employee2's objective — employee cannot update it
await assert.rejects(
() => service.updateProgress(3, { progress: 50, comment: 'Not mine' }, employeeUser),
ForbiddenException,
);
} finally {
await prisma.$disconnect();
}
});
test('KeyResultsService.updateProgress records new progress value and creates ProgressUpdate', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
const updated = await service.updateProgress(1, { progress: 100, comment: 'Completed KR' }, employeeUser);
assert.equal(updated.progress, 100);
const progressUpdates = await prisma.progressUpdate.findMany({ where: { keyResultId: 1, progress: 100 } });
assert.ok(progressUpdates.length > 0, 'A ProgressUpdate record must be created');
} finally {
await prisma.keyResult.update({ where: { id: 1 }, data: { progress: 33 } });
await prisma.progressUpdate.deleteMany({ where: { keyResultId: 1 } });
await prisma.$disconnect();
}
});
test('KeyResultsService.updateProgress non-existent KR throws NotFoundException', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
try {
await assert.rejects(
() => service.updateProgress(999999, { progress: 50, comment: 'ghost' }, managerUser),
NotFoundException,
);
} finally {
await prisma.$disconnect();
}
});
// ─── Status recalculation (full NOT_STARTED → IN_PROGRESS → COMPLETED cycle) ─
test('KeyResultsService status recalculation: NOT_STARTED → IN_PROGRESS → COMPLETED → IN_PROGRESS', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
let testObjectiveId: number | undefined;
let testKrId: number | undefined;
try {
// Create isolated objective + KR owned by employee
const obj = await prisma.objective.create({
data: { title: 'status-cycle-test', ownerId: employeeUser.sub, quarter: 'Q4/2099', status: 'NOT_STARTED' },
});
testObjectiveId = obj.id;
const kr = await prisma.keyResult.create({
data: {
objectiveId: obj.id,
title: 'single KR for status test',
progress: 0,
startValue: 0,
targetValue: 10,
deadline: new Date('2099-12-31'),
},
});
testKrId = kr.id;
// 0% → IN_PROGRESS
await service.updateProgress(kr.id, { progress: 50, comment: 'halfway' }, employeeUser);
const afterHalf = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterHalf.status, 'IN_PROGRESS');
// 100% → COMPLETED
await service.updateProgress(kr.id, { progress: 100, comment: 'done' }, employeeUser);
const afterFull = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterFull.status, 'COMPLETED');
// Regression back → IN_PROGRESS
await service.updateProgress(kr.id, { progress: 80, comment: 'regressed' }, employeeUser);
const afterRegress = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterRegress.status, 'IN_PROGRESS');
// Back to 0 → NOT_STARTED
await service.updateProgress(kr.id, { progress: 0, comment: 'reset' }, employeeUser);
const afterZero = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterZero.status, 'NOT_STARTED');
} finally {
if (testKrId) await prisma.keyResult.delete({ where: { id: testKrId } });
if (testObjectiveId) await prisma.objective.delete({ where: { id: testObjectiveId } });
await prisma.$disconnect();
}
});
test('KeyResultsService averageProgress: multi-KR completion drives COMPLETED status', async () => {
const prisma = new PrismaService();
await prisma.$connect();
const service = new KeyResultsService(prisma);
let testObjectiveId: number | undefined;
let kr1Id: number | undefined;
let kr2Id: number | undefined;
try {
const obj = await prisma.objective.create({
data: { title: 'multi-kr-test', ownerId: employeeUser.sub, quarter: 'Q4/2099', status: 'NOT_STARTED' },
});
testObjectiveId = obj.id;
const kr1 = await prisma.keyResult.create({
data: { objectiveId: obj.id, title: 'kr1', progress: 0, startValue: 0, targetValue: 1, deadline: new Date('2099-12-31') },
});
kr1Id = kr1.id;
const kr2 = await prisma.keyResult.create({
data: { objectiveId: obj.id, title: 'kr2', progress: 0, startValue: 0, targetValue: 1, deadline: new Date('2099-12-31') },
});
kr2Id = kr2.id;
// Set KR1 to 100%, KR2 stays 0 → avg=50 → IN_PROGRESS
await service.updateProgress(kr1.id, { progress: 100, comment: '' }, employeeUser);
const afterFirst = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterFirst.status, 'IN_PROGRESS');
// Set KR2 to 100% → avg=100 → COMPLETED
await service.updateProgress(kr2.id, { progress: 100, comment: '' }, employeeUser);
const afterBoth = await prisma.objective.findUniqueOrThrow({ where: { id: obj.id } });
assert.equal(afterBoth.status, 'COMPLETED');
} finally {
if (kr1Id) await prisma.keyResult.delete({ where: { id: kr1Id } });
if (kr2Id) await prisma.keyResult.delete({ where: { id: kr2Id } });
if (testObjectiveId) await prisma.objective.delete({ where: { id: testObjectiveId } });
await prisma.$disconnect();
}
});