Files
canina/backend/src/admin/department-tasks.controller.spec.ts
T
devops 02fe634944 feat(admin): implement approval separation and verifiable completion policy
- Separate B2B_SUPPLIER_STRATEGY strategic manager actions from digital tasks
- Ensure strategic proposals do not create/requeue digital tasks or set executedAt
- Ensure SEO and digital approvals transition actionable tasks to TODO
- Strengthen task completion policy: require independently verifiable HTTP(S) deliverables, reject canina://, git://stage, and post-tsk missing article placeholders
- Reject completion when QA verification evidence reports 404 or missing article errors
- Add unit test coverage for ApprovalsController, DepartmentTasksController and completion policy
2026-10-20 19:41:39 +00:00

164 lines
4.9 KiB
TypeScript

import { Test, TestingModule } from "@nestjs/testing";
import { BadRequestException, NotFoundException } from "@nestjs/common";
import { DepartmentTasksController } from "./department-tasks.controller";
import { PrismaService } from "../prisma/prisma.service";
describe("DepartmentTasksController", () => {
let controller: DepartmentTasksController;
let prisma: {
departmentTask: {
findUnique: jest.Mock;
findMany: jest.Mock;
count: jest.Mock;
create: jest.Mock;
update: jest.Mock;
delete: jest.Mock;
};
auditLog: {
create: jest.Mock;
};
};
beforeEach(async () => {
prisma = {
departmentTask: {
findUnique: jest.fn(),
findMany: jest.fn(),
count: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
auditLog: {
create: jest.fn().mockResolvedValue({ id: "audit-1" }),
},
};
const module: TestingModule = await Test.createTestingModule({
controllers: [DepartmentTasksController],
providers: [
{
provide: PrismaService,
useValue: prisma,
},
],
}).compile();
controller = module.get<DepartmentTasksController>(DepartmentTasksController);
});
describe("updateStatus - verifiable completion guards", () => {
it("rejects completion with pseudo-protocol canina://", async () => {
const taskId = "task-101";
prisma.departmentTask.findUnique.mockResolvedValue({
id: taskId,
code: "TSK-100101",
title: "Test Task",
status: "TEST",
acceptanceCriteria: { criteria: ["200 OK"] },
executionLogs: [{ status: "QA_PASSED" }],
deliverableUrl: null,
});
await expect(
controller.updateStatus(taskId, {
status: "DONE",
deliverableUrl: "canina://blog/fake-article",
}),
).rejects.toThrow(BadRequestException);
});
it("rejects completion with git://stage repository reference", async () => {
const taskId = "task-102";
prisma.departmentTask.findUnique.mockResolvedValue({
id: taskId,
code: "TSK-100102",
title: "Test Task",
status: "TEST",
acceptanceCriteria: { criteria: ["200 OK"] },
executionLogs: [{ status: "QA_PASSED" }],
deliverableUrl: null,
});
await expect(
controller.updateStatus(taskId, {
status: "DONE",
deliverableUrl: "git://stage/canina-repo#commit-xyz",
}),
).rejects.toThrow(BadRequestException);
});
it("rejects completion with post-tsk placeholder article", async () => {
const taskId = "task-103";
prisma.departmentTask.findUnique.mockResolvedValue({
id: taskId,
code: "TSK-100103",
title: "Test Task",
status: "TEST",
acceptanceCriteria: { criteria: ["200 OK"] },
executionLogs: [{ status: "QA_PASSED" }],
deliverableUrl: null,
});
await expect(
controller.updateStatus(taskId, {
status: "DONE",
deliverableUrl: "https://canina.ir/blog/post-tsk-998877",
}),
).rejects.toThrow(BadRequestException);
});
it("rejects completion when QA log reports missing article / 404", async () => {
const taskId = "task-104";
prisma.departmentTask.findUnique.mockResolvedValue({
id: taskId,
code: "TSK-100104",
title: "Test Task",
status: "TEST",
acceptanceCriteria: { criteria: ["200 OK"] },
executionLogs: [],
deliverableUrl: "https://canina.ir/blog/real-article",
});
await expect(
controller.updateStatus(taskId, {
status: "DONE",
logNote: "QA_PASSED but received 404 - مقاله یافت نشد",
}),
).rejects.toThrow(BadRequestException);
});
it("allows completion with valid HTTPS deliverable URL and verified QA evidence", async () => {
const taskId = "task-105";
const existingTask = {
id: taskId,
code: "TSK-100105",
title: "Optimized Cat Food Landing",
status: "TEST",
acceptanceCriteria: { criteria: ["HTTP 200", "Lighthouse > 90"] },
executionLogs: [{ status: "PRODUCTION_VERIFIED", note: "Live 200 verified on production" }],
deliverableUrl: "https://canina.ir/blog/cat-food-nutrition",
};
prisma.departmentTask.findUnique.mockResolvedValue(existingTask);
prisma.departmentTask.update.mockResolvedValue({
...existingTask,
status: "DONE",
completedAt: new Date(),
});
const res = await controller.updateStatus(taskId, {
status: "DONE",
});
expect(res.success).toBe(true);
expect(prisma.departmentTask.update).toHaveBeenCalledWith({
where: { id: taskId },
data: expect.objectContaining({
status: "DONE",
completedAt: expect.any(Date),
}),
});
});
});
});