C1 — Skills composer (name composition + dedupe_key preview)

Skills composer: category (required) + optional technology + optional
function -> composed name prefill + dedupe_key. Governing rule: a null
technology yields a generic (technology-agnostic) skill; a duplicate
(category, tech, function) is rejected.
Delivered:
- src/domain/skills.ts — pure composeSkillName (no DB, no src/db imports).
  Rule: function+technology -> '<fn> on <tech>'; function only -> '<fn>';
  technology only -> '<tech>'; neither -> category. Null technology ->
  generic skill (name never names a specific piece of gear).
- src/domain/skills.test.ts — 6 tests named after the composition
  behaviors.
- src/app/api/dev/skills/compose/route.ts — POST /api/dev/skills/compose
  returns { name, dedupeKey } for prefilling the form; writes no row.
  The admin edits the name, then submits to POST /api/dev/skills, which
  generates slug + dedupe_key and enforces the unique constraint (409 on
  duplicate).
No schema change, no migration: the skills table, data-layer CRUD, and
dedupeKey were delivered in B4; C1 adds only the composition rule +
composer endpoint (matching the isPublishable/validateRequirements
domain pattern).
Verification: typecheck lint test (50, +6) build all green; runtime
smoke /tmp/c1.sh (13 checks): compose fn+tech -> '<fn> on <tech>',
compose function-only -> generic, create composed skill (slug frozen,
dedupe_key persisted), duplicate -> 409, null-tech generic skill,
dangling FK -> 400.
This commit is contained in:
Tiel Coder
2026-11-18 03:08:25 +00:00
parent 5c789f63cc
commit e9168def6c
4 changed files with 183 additions and 1 deletions
+58
View File
@@ -0,0 +1,58 @@
import { NextRequest } from "next/server";
import { z } from "zod";
import { composeSkillName } from "@/domain/skills";
import { dedupeKey } from "@/db/positions";
const JSON_HEADERS = { "content-type": "application/json" };
/**
* The composer: pick a category (required) + optional technology + optional
* function, and get back the prefilled display name and the dedupe_key the
* create path will persist. The UI fills the form with `name` (editable) and
* submits to POST /api/dev/skills. This endpoint never writes a row.
*/
const composeSchema = z.object({
categoryId: z.string().min(1),
categoryName: z.string().min(1),
technologyId: z.string().nullable().optional(),
technologyName: z.string().nullable().optional(),
functionId: z.string().nullable().optional(),
functionName: z.string().nullable().optional(),
});
async function readBody(req: NextRequest) {
try {
return await req.json();
} catch {
return null;
}
}
const bad = (message: string) =>
new Response(JSON.stringify({ error: message }), {
status: 400,
headers: JSON_HEADERS,
});
/**
* POST /api/dev/skills/compose { categoryId, categoryName, technologyId?,
* technologyName?, functionId?, functionName? } -> { name, dedupeKey }.
*/
export const POST = async (req: NextRequest) => {
const body = await readBody(req);
if (!body || typeof body !== "object") return bad("invalid body");
const parsed = composeSchema.safeParse(body);
if (!parsed.success) return bad(parsed.error.message);
const d = parsed.data;
return new Response(
JSON.stringify({
name: composeSkillName({
categoryName: d.categoryName,
technologyName: d.technologyName,
functionName: d.functionName,
}),
dedupeKey: dedupeKey(d.categoryId, d.technologyId, d.functionId),
}),
{ status: 200, headers: JSON_HEADERS },
);
};
+64
View File
@@ -0,0 +1,64 @@
import { describe, expect, it } from "vitest";
import { composeSkillName } from "./skills";
describe("composeSkillName", () => {
it("composes function + technology as '<function> on <technology>'", () => {
expect(
composeSkillName({
categoryName: "Audio",
functionName: "Switch cameras",
technologyName: "ATEM Mini Pro",
}),
).toBe("Switch cameras on ATEM Mini Pro");
});
it("uses just the function when technology is null (generic skill)", () => {
expect(
composeSkillName({
categoryName: "Audio",
functionName: "Run FOH audio",
technologyName: null,
}),
).toBe("Run FOH audio");
});
it("uses just the technology when no function is selected", () => {
expect(
composeSkillName({
categoryName: "Livestream",
functionName: null,
technologyName: "ATEM Mini Pro",
}),
).toBe("ATEM Mini Pro");
});
it("falls back to the category when neither function nor technology (generic skill)", () => {
expect(
composeSkillName({
categoryName: "Audio",
functionName: null,
technologyName: null,
}),
).toBe("Audio");
});
it("never names a specific gear when technology is null", () => {
const name = composeSkillName({
categoryName: "Audio",
functionName: "Gain staging",
technologyName: null,
});
expect(name).not.toMatch(/console|mixer|gate/i);
expect(name).toBe("Gain staging");
});
it("trims surrounding whitespace from each selection", () => {
expect(
composeSkillName({
categoryName: " Audio ",
functionName: " Switch cameras ",
technologyName: " ATEM Mini Pro ",
}),
).toBe("Switch cameras on ATEM Mini Pro");
});
});
+30
View File
@@ -0,0 +1,30 @@
/**
* Skill name composition (ARCHITECTURE §4.3, unit C1 — Skills composer).
*
* A skill is composed from a category (required) + optional technology +
* optional function. The composer prefills the display name from these
* selections; the admin may then edit it before saving. The rule:
*
* function + technology -> "<function> on <technology>" e.g. "Switch cameras on ATEM Mini Pro"
* function only -> "<function>" e.g. "Run FOH audio" (technology-agnostic)
* technology only -> "<technology>" e.g. "ATEM Mini Pro"
* neither (category) -> "<category>" e.g. "Audio" (generic)
*
* A null technology yields a generic (technology-agnostic) skill: the composed
* name never names a specific piece of gear. Pure — no DB, no imports from
* `src/db`.
*/
export function composeSkillName(params: {
categoryName: string;
technologyName?: string | null;
functionName?: string | null;
}): string {
const categoryName = params.categoryName.trim();
const technologyName = params.technologyName?.trim() ?? "";
const functionName = params.functionName?.trim() ?? "";
if (functionName && technologyName) return `${functionName} on ${technologyName}`;
if (functionName) return functionName;
if (technologyName) return technologyName;
return categoryName;
}