feat(corporate): exact task claim and manual plan inventory execution slice
Deploy Canina / deploy (push) Successful in 2m31s
Deploy Canina / deploy (push) Successful in 2m31s
This commit is contained in:
1 parent
216e05ba25
commit
8c4faf8820
42 files changed
+5717
-4
No files matched your search
@@ -1,9 +1,14 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { resolve } from 'path';
|
||||
import { execFile } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import passport from 'passport';
|
||||
import { Strategy, ExtractJwt } from 'passport-jwt';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import { canonical } from './corporate-policy';
|
||||
import request from 'supertest';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CorporateController } from './corporate.controller';
|
||||
@@ -80,6 +85,109 @@ integration('corporate API / disposable PostgreSQL', () => {
|
||||
'TRUNCATE corporate_operations, corporate_reports, corporate_effects, corporate_reviews, corporate_evidence, corporate_events, corporate_tasks, corporate_goals, corporate_roadmaps, corporate_sources, corporate_research CASCADE',
|
||||
);
|
||||
});
|
||||
describe('exact task claim selector', () => {
|
||||
it('claims only the exact task, leaving the older eligible task untouched', async () => {
|
||||
const older = (await post('tasks', input).expect(201)).body;
|
||||
const target = (
|
||||
await post('tasks', { ...input, title: 'target' }).expect(201)
|
||||
).body;
|
||||
await db.corporateTask.update({
|
||||
where: { id: older.id },
|
||||
data: { updatedAt: new Date('2020-01-01T00:00:00Z') },
|
||||
});
|
||||
const result = (
|
||||
await post('claims', { taskId: target.id.toUpperCase() }).expect(201)
|
||||
).body;
|
||||
expect(result.task.id).toBe(target.id);
|
||||
expect(result.task.executionCount).toBe(1);
|
||||
expect(
|
||||
await db.corporateTask.findUniqueOrThrow({ where: { id: older.id } }),
|
||||
).toMatchObject({
|
||||
status: 'TODO',
|
||||
executionCount: 0,
|
||||
claimToken: null,
|
||||
});
|
||||
expect(
|
||||
await db.corporateEvent.count({
|
||||
where: { taskId: older.id, step: 'CLAIMED' },
|
||||
}),
|
||||
).toBe(0);
|
||||
});
|
||||
it('does not bypass the future PLANNED gate or fall back to another task', async () => {
|
||||
const legacy = (await post('tasks', input).expect(201)).body;
|
||||
const { body } = await planFixture();
|
||||
const plan = (await post('monthly-plans', body).expect(201)).body;
|
||||
const target = plan.tasks[0];
|
||||
expect(
|
||||
(await post('claims', { taskId: target.id }).expect(201)).body.task,
|
||||
).toBeNull();
|
||||
expect(
|
||||
await db.corporateTask.findUniqueOrThrow({ where: { id: target.id } }),
|
||||
).toMatchObject({
|
||||
status: 'TODO',
|
||||
executionCount: 0,
|
||||
claimToken: null,
|
||||
});
|
||||
expect((await post('claims', {}).expect(201)).body.task.id).toBe(
|
||||
legacy.id,
|
||||
);
|
||||
});
|
||||
it('rejects invalid UUID selectors before recording any operation', async () => {
|
||||
for (const taskId of ['not-a-uuid', "' OR true --", '', null, 123])
|
||||
await post('claims', { taskId }).expect(400);
|
||||
expect(await db.corporateOperation.count()).toBe(0);
|
||||
});
|
||||
it('binds idempotency to the normalized selector and never claims a replacement', async () => {
|
||||
const a = (await post('tasks', input).expect(201)).body;
|
||||
const b = (await post('tasks', { ...input, title: 'other' }).expect(201))
|
||||
.body;
|
||||
const op = key();
|
||||
const claimed = (
|
||||
await post('claims', { taskId: a.id }, 'executor', op).expect(201)
|
||||
).body;
|
||||
expect(
|
||||
(
|
||||
await post(
|
||||
'claims',
|
||||
{ taskId: a.id.toUpperCase() },
|
||||
'executor',
|
||||
op,
|
||||
).expect(201)
|
||||
).body,
|
||||
).toEqual(claimed);
|
||||
await post('claims', { taskId: b.id }, 'executor', op).expect(409);
|
||||
await post('claims', {}, 'executor', op).expect(409);
|
||||
const legacyOp = key();
|
||||
const legacy = (
|
||||
await post('claims', {}, 'executor', legacyOp).expect(201)
|
||||
).body;
|
||||
expect(legacy.task.id).toBe(b.id);
|
||||
await post('claims', { taskId: b.id }, 'executor', legacyOp).expect(409);
|
||||
expect(
|
||||
await db.corporateEvent.count({ where: { step: 'CLAIMED' } }),
|
||||
).toBe(2);
|
||||
});
|
||||
it('preserves the historical no-selector hash and replay response', async () => {
|
||||
const op = key();
|
||||
const hash = (value: unknown) =>
|
||||
createHash('sha256').update(canonical(value)).digest('hex');
|
||||
const response = { task: null };
|
||||
await db.corporateOperation.create({
|
||||
data: {
|
||||
key: hash(['apikey:executor', 'claim', op]),
|
||||
payloadHash: hash({ department: null, seconds: 300 }),
|
||||
response,
|
||||
},
|
||||
});
|
||||
await post('tasks', input).expect(201);
|
||||
expect(
|
||||
(await post('claims', {}, 'executor', op).expect(201)).body,
|
||||
).toEqual(response);
|
||||
expect(
|
||||
await db.corporateEvent.count({ where: { step: 'CLAIMED' } }),
|
||||
).toBe(0);
|
||||
});
|
||||
});
|
||||
async function planFixture(month = '2996-02') {
|
||||
const research = (
|
||||
await post('market-insights', {
|
||||
@@ -170,6 +278,26 @@ integration('corporate API / disposable PostgreSQL', () => {
|
||||
},
|
||||
};
|
||||
}
|
||||
it('runs manual planned inventory through HTTP, independent QA and persisted PMO without completing article', async () => {
|
||||
const { body, research, roadmap } = await planFixture('2024-02');
|
||||
// Real repository bytes, not invented clinical evidence or provider output.
|
||||
const script = resolve(__dirname, '../../../../scripts/corporate_plan_inventory.py');
|
||||
await db.corporateSource.update({
|
||||
where: { id: research.sources[0].id },
|
||||
data: { sha256: createHash('sha256').update(readFileSync(script)).digest('hex') },
|
||||
});
|
||||
const plan = (await post('monthly-plans', body).expect(201)).body;
|
||||
const parent = plan.tasks.find((t: any) => t.actionType === 'BLOG_NEW');
|
||||
const address = app.getHttpServer().address();
|
||||
const result = await promisify(execFile)('python3', [
|
||||
resolve(__dirname, '../../../../scripts/corporate_plan_inventory_http_test.py'),
|
||||
`http://127.0.0.1:${address.port}`, roadmap.id, body.month, parent.id,
|
||||
], { timeout: 20000 });
|
||||
const receipt = JSON.parse(result.stdout);
|
||||
expect(receipt).toMatchObject({ originalStatus: 'BLOCKED', independentQA: true,
|
||||
selfReviewDenied: true, persistedPMOReadback: true });
|
||||
console.log('MANUAL_PLAN_INVENTORY_RECEIPT', receipt);
|
||||
});
|
||||
it('persists a complete leap-month daily-new plan, separate needs and evidence lineage with readback', async () => {
|
||||
const { body, research, roadmap } = await planFixture();
|
||||
body.roadmapId = body.roadmapId.toUpperCase();
|
||||
@@ -195,7 +323,9 @@ integration('corporate API / disposable PostgreSQL', () => {
|
||||
(await post('monthly-plans', body, 'planner', op).expect(201)).body,
|
||||
).toEqual(created);
|
||||
const read = await request(app.getHttpServer())
|
||||
.get(`/admin/hermes/monthly-plans/${roadmap.id.toUpperCase()}?month=${body.month}`)
|
||||
.get(
|
||||
`/admin/hermes/monthly-plans/${roadmap.id.toUpperCase()}?month=${body.month}`,
|
||||
)
|
||||
.set('x-api-key', 'executor')
|
||||
.expect(200);
|
||||
expect(read.body).toEqual(created);
|
||||
|
||||
@@ -309,7 +309,8 @@ export class CorporateService {
|
||||
});
|
||||
}
|
||||
claim(actor: string, key: string, input: unknown) {
|
||||
const b = object(input, ['department', 'leaseSeconds']);
|
||||
const b = object(input, ['department', 'leaseSeconds', 'taskId']);
|
||||
const taskId = b.taskId === undefined ? undefined : uuid(b.taskId);
|
||||
const department =
|
||||
b.department === undefined ? null : choice(b.department, departments);
|
||||
const seconds = Number(b.leaseSeconds ?? 300);
|
||||
@@ -319,7 +320,7 @@ export class CorporateService {
|
||||
actor,
|
||||
'claim',
|
||||
key,
|
||||
{ department, seconds },
|
||||
{ department, seconds, ...(taskId === undefined ? {} : { taskId }) },
|
||||
async (tx) => {
|
||||
const candidates = await tx.$queryRaw<
|
||||
{ id: string }[]
|
||||
@@ -329,6 +330,7 @@ export class CorporateService {
|
||||
AND (t."dueCycle" IS NULL OR t."dueCycle" !~ '^[0-9]{4}-[0-9]{2}-[0-9]{2}$'
|
||||
OR e.payload->>'dueCycle' IS DISTINCT FROM t."dueCycle"
|
||||
OR t."dueCycle">to_char(clock_timestamp() AT TIME ZONE 'UTC','YYYY-MM-DD')))
|
||||
AND (${taskId ?? null}::text IS NULL OR t.id=${taskId ?? null}::uuid)
|
||||
AND (${department}::text IS NULL OR t.department=${department})
|
||||
AND NOT EXISTS (SELECT 1 FROM corporate_tasks c WHERE c."parentId"=t.id AND c.status<>'DONE')
|
||||
ORDER BY t.priority ASC,t."updatedAt" ASC,t.id ASC FOR UPDATE OF t SKIP LOCKED LIMIT 1`;
|
||||
|
||||
Reference in new issue
Block a user