Implements 26 of 28 tasks from specs/004-product-knowledge/tasks.md across all three user stories -- Phase 3 of the roadmap. First feature to populate src/modules/ai-support/ (doc 07 places `knowledge` there; only that submodule is built, matching this codebase's convention of not pre-building unneeded submodules). Schema (prisma/schema.prisma + migration): - KnowledgeEntry, ErrorCode, KnownIssue, Runbook per docs/06, refining its conceptual flat `version` field into an explicit version-history mechanism: each edit inserts a new row (isCurrentVersion flag, compound unique on (code, version) / (key, productId, version)) instead of overwriting in place -- the only way "prior versions remain retrievable" (FR-004/FR-009) is actually true rather than aspirational. User Story 1 -- knowledge entry authoring/publish/version (P1, MVP): - draft -> published -> unpublished lifecycle; publish only takes effect from its effectiveDate. - Editing uses the same conditional-update-then-insert optimistic concurrency pattern as 003-ticketing's Ticket.version (409 on a stale expectedVersion). - Full version history readable via GET .../versions. User Story 2 -- error codes, known issues, runbooks (P2): - ErrorCode + KnownIssue with direct lookup-by-error-code. - Runbook steps stored as an ordered JSON array, preserved exactly; same version-on-edit mechanism as knowledge entries; inactive runbooks are indistinguishable from nonexistent ones on lookup. User Story 3 -- filtered retrieval (P3): - GET /knowledge/retrieve: product-scoped, excludes draft/ unpublished/not-yet-effective entries, validated entries ranked ahead of unvalidated. Deliberately NOT semantic/vector search -- doc 11 gap B1 explicitly defers embedding-model choice to the future AI-support feature; this is real, usable structured filtering a semantic layer can sit in front of later. Found and fixed one real bug before it reached tests: the retrieval endpoint initially queried by the raw external product id instead of resolving it to the internal Product.id first (every other endpoint in this feature does that resolution) -- would have silently returned zero results for every caller. Fixed with a lenient tryResolveProductId (empty array, not 404, for an unregistered product -- matches the "no matches, never an error" contract). Deliberately skipped (not forgotten, see checklist notes): the two planned mock-repository unit-test tasks (T004, T019) -- unlike 003-ticketing's state machine, this feature has no pure-logic surface to isolate from Prisma; coverage comes entirely from integration tests instead. All 13 integration test files in the repo (36 tests, spanning this feature and every prior one) verified passing together against a real Postgres/Redis/MinIO -- no regressions. Full quality gate (typecheck/lint/format/architecture/unit tests) passes. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
134 lines
4.9 KiB
TypeScript
134 lines
4.9 KiB
TypeScript
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|
import { buildApp } from '@/app';
|
|
import { prismaClient } from '@/infrastructure/database';
|
|
import { FastifyInstance } from 'fastify';
|
|
|
|
/** Covers specs/004-product-knowledge/quickstart.md Scenarios 1, 2, 3 against a real Postgres. */
|
|
describe('Knowledge entry authoring, publishing, and versioning', () => {
|
|
let app: FastifyInstance;
|
|
const externalProductId = `TEST_KNOWLEDGE_PROD_${Date.now()}`;
|
|
const code = `KB-TEST-${Date.now()}`;
|
|
|
|
beforeAll(async () => {
|
|
app = await buildApp();
|
|
await prismaClient.product.create({
|
|
data: { externalProductId, name: 'Knowledge Test Product', status: 'active' },
|
|
});
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await prismaClient.knowledgeEntry.deleteMany({ where: { code } });
|
|
await prismaClient.product.deleteMany({ where: { externalProductId } });
|
|
await app.close();
|
|
});
|
|
|
|
it('Scenario 1: a draft is invisible to retrieval, publishing makes it retrievable', async () => {
|
|
const createResponse = await app.inject({
|
|
method: 'POST',
|
|
url: `/admin/products/${externalProductId}/knowledge`,
|
|
payload: { code, type: 'known_issue', problem: 'PDF conversion fails' },
|
|
});
|
|
expect(createResponse.statusCode).toBe(201);
|
|
expect(createResponse.json().data.status).toBe('draft');
|
|
|
|
const beforePublish = await app.inject({
|
|
method: 'GET',
|
|
url: `/knowledge/retrieve?productId=${externalProductId}`,
|
|
});
|
|
expect(
|
|
beforePublish.json().data.find((e: { code: string }) => e.code === code),
|
|
).toBeUndefined();
|
|
|
|
const publishResponse = await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/knowledge/${code}/publish`,
|
|
});
|
|
expect(publishResponse.statusCode).toBe(200);
|
|
expect(publishResponse.json().data.status).toBe('published');
|
|
|
|
const afterPublish = await app.inject({
|
|
method: 'GET',
|
|
url: `/knowledge/retrieve?productId=${externalProductId}`,
|
|
});
|
|
expect(afterPublish.json().data.find((e: { code: string }) => e.code === code)).toBeDefined();
|
|
});
|
|
|
|
it('Scenario 2: editing creates a new version and preserves the prior one', async () => {
|
|
const editResponse = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/knowledge/${code}`,
|
|
payload: {
|
|
type: 'known_issue',
|
|
problem: 'PDF conversion fails — updated',
|
|
expectedVersion: 1,
|
|
},
|
|
});
|
|
expect(editResponse.statusCode).toBe(200);
|
|
expect(editResponse.json().data.version).toBe(2);
|
|
|
|
const versionsResponse = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/knowledge/${code}/versions`,
|
|
});
|
|
const versions = versionsResponse.json().data;
|
|
expect(versions).toHaveLength(2);
|
|
const v1 = versions.find((v: { version: number }) => v.version === 1);
|
|
const v2 = versions.find((v: { version: number }) => v.version === 2);
|
|
expect(v1.problem).toBe('PDF conversion fails');
|
|
expect(v2.problem).toBe('PDF conversion fails — updated');
|
|
expect(v2.isCurrentVersion).toBe(true);
|
|
expect(v1.isCurrentVersion).toBe(false);
|
|
|
|
const retrieveResponse = await app.inject({
|
|
method: 'GET',
|
|
url: `/knowledge/retrieve?productId=${externalProductId}`,
|
|
});
|
|
const retrieved = retrieveResponse.json().data.find((e: { code: string }) => e.code === code);
|
|
expect(retrieved.problem).toBe('PDF conversion fails — updated');
|
|
});
|
|
|
|
it('Scenario 3: a stale-version edit is rejected, not silently applied', async () => {
|
|
const first = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/knowledge/${code}`,
|
|
payload: { type: 'known_issue', problem: 'edit A', expectedVersion: 2 },
|
|
});
|
|
const second = await app.inject({
|
|
method: 'PUT',
|
|
url: `/admin/knowledge/${code}`,
|
|
payload: { type: 'known_issue', problem: 'edit B', expectedVersion: 2 },
|
|
});
|
|
|
|
const results = [first.statusCode, second.statusCode].sort();
|
|
expect(results).toEqual([200, 409]);
|
|
});
|
|
|
|
it('marking an entry validated is reflected on retrieval results', async () => {
|
|
const response = await app.inject({
|
|
method: 'PATCH',
|
|
url: `/admin/knowledge/${code}/validate`,
|
|
payload: { validationStatus: 'validated' },
|
|
});
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json().data.validationStatus).toBe('validated');
|
|
});
|
|
|
|
it('unpublishing removes the entry from retrieval without deleting it', async () => {
|
|
await app.inject({ method: 'PATCH', url: `/admin/knowledge/${code}/unpublish` });
|
|
|
|
const retrieveResponse = await app.inject({
|
|
method: 'GET',
|
|
url: `/knowledge/retrieve?productId=${externalProductId}`,
|
|
});
|
|
expect(
|
|
retrieveResponse.json().data.find((e: { code: string }) => e.code === code),
|
|
).toBeUndefined();
|
|
|
|
const versionsResponse = await app.inject({
|
|
method: 'GET',
|
|
url: `/admin/knowledge/${code}/versions`,
|
|
});
|
|
expect(versionsResponse.statusCode).toBe(200);
|
|
});
|
|
});
|