Review findings, all in code this range introduced. Attaching a branch to a node that already had a next step replaced that hop, and "come back where it attached" pointed the branch back at the caller, which loops: the caller then leads into the branch it just returned from. Everything downstream was orphaned. The branch now continues into the step that hop used to point at, so attaching inserts in front of it instead of deleting it. Rejoining a condition is refused outright, because re-entering re-evaluates the same flags and retakes the same branch. Return candidates are limited to node types that read `next`: a choice or condition open end would have received a hop no walker follows. The dialog says which of the three applies, and only offers the toggle when a return exists, so an untouched default is not sent and reported back as a refusal. Condition true/false dots were drawn on the input handle: the hit-area class sets left and right on the handle itself, which over-constrains React Flow's `right: 0`, so both dots drifted to the card's left edge. They now take the hit area as a child, like the other handles. Refusals are honest: `connectHop` reports `changed: false` for a write it did not make (it used to report true and return a new array), the editor explains instead of rewriting an identical graph, and a refused tail-drag keeps the original hop rather than deleting it first. A passing connection test now really is invalidated when the configuration changes. That ran in `afterChange`, whose return value is only the operation's response, so the stored row kept showing a stale pass while the test asserted the response. It moved to `beforeChange`, counts only fields actually submitted (so the Test button's own result write does not invalidate itself), and the test reads the row back. Also: the fragment prompt no longer teaches `isStart: true` through the worked example or asks for "the whole conversation" on retry; requests carry a ten minute ceiling so an unreachable-but-accepting host cannot hang Generate, Compose or the Test button; the API key description no longer claims at-rest encryption, which does not exist; the per-dialogue voice override gets the same dash and length cleaning as the global it outranks; and the unused `describeAttachmentPoint` is gone, with the attachment output now included in the hint the model receives.
241 lines
7.9 KiB
TypeScript
241 lines
7.9 KiB
TypeScript
import type { Payload } from "payload";
|
|
import { beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import { getTestPayload } from "../helpers/get-test-payload";
|
|
import { resolveConnection } from "@/lib/ai/connections";
|
|
|
|
/**
|
|
* The connection record is where a swapped provider can break silently, so the two
|
|
* things that must not drift are pinned here: the write-only key contract, and the
|
|
* resolution the AI calls actually perform.
|
|
*
|
|
* The key rules exist because the field's read access denies everyone, so the admin
|
|
* form can never show the stored value. That makes a blank submission ambiguous
|
|
* ("unchanged" or "delete?"), which is why blank means unchanged and `clearApiKey`
|
|
* is the only delete.
|
|
*/
|
|
let payload: Payload;
|
|
|
|
beforeAll(async () => {
|
|
payload = await getTestPayload();
|
|
});
|
|
|
|
const createConnection = (overrides: Record<string, unknown> = {}) =>
|
|
payload.create({
|
|
collection: "ai-connections",
|
|
data: {
|
|
label: "Unit box",
|
|
baseUrl: "http://192.168.1.7:8888/v1",
|
|
modelId: "unsloth/Ornith-1.0-9B-GGUF",
|
|
structuredMode: "json_schema",
|
|
sendReasoningOff: true,
|
|
...overrides,
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
describe("ai-connections key handling", () => {
|
|
it("denies the key field to every reader that does not bypass access", async () => {
|
|
const created = await createConnection({ apiKey: "sk-test-secret" });
|
|
expect(created.hasApiKey).toBe(true);
|
|
|
|
// The local API defaults to overrideAccess: true, so a plain findByID proves
|
|
// nothing about the gate. Assert the field's own access rule instead: this is
|
|
// what strips the value from REST, GraphQL and the admin form.
|
|
const collection = payload.config.collections.find((c) => c.slug === "ai-connections");
|
|
const field = collection?.fields.find(
|
|
(candidate) => "name" in candidate && candidate.name === "apiKey",
|
|
) as { access?: { read?: (args: unknown) => unknown } } | undefined;
|
|
|
|
expect(typeof field?.access?.read).toBe("function");
|
|
expect(await field?.access?.read?.({ req: {} })).toBe(false);
|
|
});
|
|
|
|
it("hides the collection from a caller without the superuser tier", async () => {
|
|
await createConnection({ apiKey: "sk-test-secret" });
|
|
|
|
await expect(
|
|
payload.find({ collection: "ai-connections", overrideAccess: false, depth: 0 }),
|
|
).rejects.toThrow(/not allowed/i);
|
|
});
|
|
|
|
it("keeps the stored key when the form submits it blank", async () => {
|
|
const created = await createConnection({ apiKey: "sk-first" });
|
|
|
|
const updated = await payload.update({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
data: { label: "Renamed", apiKey: "" },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
expect(updated.hasApiKey).toBe(true);
|
|
|
|
const stored = await payload.findByID({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect((stored as { apiKey?: string | null }).apiKey).toBe("sk-first");
|
|
});
|
|
|
|
it("replaces the key when a new value is submitted", async () => {
|
|
const created = await createConnection({ apiKey: "sk-first" });
|
|
|
|
await payload.update({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
data: { apiKey: "sk-second" },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
const stored = await payload.findByID({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect((stored as { apiKey?: string | null }).apiKey).toBe("sk-second");
|
|
});
|
|
|
|
it("clears the key only when clearApiKey is set, and resets the flag", async () => {
|
|
const created = await createConnection({ apiKey: "sk-first" });
|
|
|
|
const cleared = await payload.update({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
data: { clearApiKey: true },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
expect(cleared.hasApiKey).toBe(false);
|
|
// The checkbox is an action: leaving it ticked would clear a key set later.
|
|
expect(cleared.clearApiKey).toBe(false);
|
|
|
|
const stored = await payload.findByID({
|
|
collection: "ai-connections",
|
|
id: created.id,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
});
|
|
expect((stored as { apiKey?: string | null }).apiKey ?? null).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("resolveConnection", () => {
|
|
it("returns null when a surface has no connection selected", async () => {
|
|
await payload.updateGlobal({
|
|
slug: "ai-settings",
|
|
data: { composeConnection: null, dialogueDraftConnection: null, dialogueLineConnection: null },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
expect(await resolveConnection(payload, "compose")).toBeNull();
|
|
expect(await resolveConnection(payload, "dialogue-draft")).toBeNull();
|
|
expect(await resolveConnection(payload, "dialogue-line")).toBeNull();
|
|
});
|
|
|
|
it("resolves the connection selected for that surface only", async () => {
|
|
const draft = await createConnection({
|
|
label: "Draft box",
|
|
modelId: "draft-model",
|
|
apiKey: "sk-draft",
|
|
structuredMode: "json_object",
|
|
});
|
|
const line = await createConnection({
|
|
label: "Line box",
|
|
modelId: "line-model",
|
|
sendReasoningOff: false,
|
|
});
|
|
|
|
await payload.updateGlobal({
|
|
slug: "ai-settings",
|
|
data: {
|
|
composeConnection: null,
|
|
dialogueDraftConnection: draft.id,
|
|
dialogueLineConnection: line.id,
|
|
},
|
|
overrideAccess: true,
|
|
});
|
|
|
|
const draftResolved = await resolveConnection(payload, "dialogue-draft");
|
|
expect(draftResolved).toMatchObject({
|
|
label: "Draft box",
|
|
modelId: "draft-model",
|
|
apiKey: "sk-draft",
|
|
structuredMode: "json_object",
|
|
});
|
|
|
|
const lineResolved = await resolveConnection(payload, "dialogue-line");
|
|
expect(lineResolved).toMatchObject({
|
|
label: "Line box",
|
|
modelId: "line-model",
|
|
sendReasoningOff: false,
|
|
structuredMode: "json_schema",
|
|
});
|
|
|
|
// Compose was left blank, so it must keep using the env defaults.
|
|
expect(await resolveConnection(payload, "compose")).toBeNull();
|
|
});
|
|
|
|
it("falls back to the env defaults for a half-filled connection", async () => {
|
|
const connection = await createConnection({ modelId: " " });
|
|
|
|
await payload.updateGlobal({
|
|
slug: "ai-settings",
|
|
data: { dialogueDraftConnection: connection.id },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
expect(await resolveConnection(payload, "dialogue-draft")).toBeNull();
|
|
});
|
|
|
|
it("resets a passing test result when the configuration changes", async () => {
|
|
const connection = await createConnection();
|
|
await payload.update({
|
|
collection: "ai-connections",
|
|
id: connection.id,
|
|
data: { lastTestOk: true, lastTestMessage: "passed" },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
await payload.update({
|
|
collection: "ai-connections",
|
|
id: connection.id,
|
|
data: { modelId: "some-other-model" },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
// Read back from the database: an afterChange return value is only ever the
|
|
// response of the operation, so asserting the update's own result would pass
|
|
// while the stored row kept saying "passed".
|
|
const stored = await payload.findByID({
|
|
collection: "ai-connections",
|
|
id: connection.id,
|
|
overrideAccess: true,
|
|
});
|
|
expect(stored.lastTestOk).toBe(false);
|
|
expect(stored.lastTestMessage).toContain("run the test again");
|
|
});
|
|
|
|
it("keeps a fresh test result when only the result is written", async () => {
|
|
const connection = await createConnection();
|
|
// The Test button patches the result fields alone. If an absent field counted
|
|
// as "changed", that write would immediately invalidate itself.
|
|
await payload.update({
|
|
collection: "ai-connections",
|
|
id: connection.id,
|
|
data: { lastTestOk: true, lastTestMessage: "passed" },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
const stored = await payload.findByID({
|
|
collection: "ai-connections",
|
|
id: connection.id,
|
|
overrideAccess: true,
|
|
});
|
|
expect(stored.lastTestOk).toBe(true);
|
|
expect(stored.lastTestMessage).toBe("passed");
|
|
});
|
|
});
|