Initial upload to Gitea

This commit is contained in:
antonn committed 2026-11-30 16:32:37 +00:00
1 parent 78193254ad
commit 8478998cae
1485 files changed
+461025 -42348

No files matched your search

@@ -0,0 +1,133 @@
"use strict";
// A Planner schedule binds the selected model, not a concurrently changed account preference.
const test = require("node:test");
const assert = require("node:assert");
const fs = require("node:fs");
const path = require("node:path");
const vm = require("node:vm");
const ROOT = path.join(__dirname, "..");
const BOT_JS = path.join(ROOT, "public", "js", "bot.js");
const botSrc = fs.readFileSync(BOT_JS, "utf8");
const indexSrc = fs.readFileSync(path.join(ROOT, "index.js"), "utf8");
function sourceWindow(src, startAnchor, endAnchor, label) {
const start = src.indexOf(startAnchor);
assert.notStrictEqual(start, -1, `${label || "window"}: start anchor not found: ${startAnchor}`);
const end = src.indexOf(endAnchor, start + startAnchor.length);
assert.notStrictEqual(end, -1, `${label || "window"}: end anchor not found: ${endAnchor}`);
return src.slice(start, end);
}
const createWindow = sourceWindow(
botSrc,
"async function createSchedulerTask() {",
"function resetSchedulerForm() {",
"createSchedulerTask"
);
const plannerBranch = sourceWindow(
createWindow,
"} else if (actionType === 'planner_next_to_agent') {",
"} else if (actionType === 'agent_prompt') {",
"planner create branch"
);
const agentPromptBranch = sourceWindow(
createWindow,
"} else if (actionType === 'agent_prompt') {",
"if (schedulerCreateBtn) {",
"agent_prompt create branch"
);
const postAnchor = "fetchWithServerActionToken(`/api/scheduler/";
test("Planner scheduling sends its captured choice without rewriting the account default", () => {
assert.match(plannerBranch, /const schedulerModelValue = String\(aiModelSelect\?\.value \|\| ''\)\.trim\(\);\s+if \(schedulerModelValue\) payload\.selection = schedulerModelValue;/);
assert.doesNotMatch(plannerBranch, /saveSelectedAiModel|\bfetch\(/);
assert.match(indexSrc, /resolveAiModelSelectionForUserId\(user\?\.id, payload\.selection\)[\s\S]*?delete payload\.selection;\s+payload\.provider = selection\.provider;\s+payload\.model = selection\.model;/);
});
test("an explicit model wins over the saved default and an invalid model cannot fall back", async () => {
const source = sourceWindow(indexSrc, "async function resolveAiModelSelectionForUserId(", "async function saveAiModelSelectionForUserId(", "model resolution");
let preferenceReads = 0;
const resolve = vm.runInNewContext(`(${source.trim()})`, {
normalizeAiModelSelectionPayload: ({ selection }) => JSON.parse(selection),
assertAiProviderReady(provider, model) {
if (provider !== "fixture" || model !== "chosen") throw new Error("invalid model");
return { provider, model };
},
async ensureAiChatTablesReady() {},
db: { async query() { preferenceReads += 1; return [{ provider: "fixture", model: "default" }]; } },
normalizeAiProviderId: String,
sanitizeAiModelId: String,
});
assert.strictEqual((await resolve(7, '{"provider":"fixture","model":"chosen"}')).model, "chosen");
await assert.rejects(resolve(7, '{"provider":"fixture","model":"invalid"}'), /invalid model/);
assert.strictEqual(preferenceReads, 0);
assert.strictEqual((await resolve(7)).model, "default");
assert.strictEqual(preferenceReads, 1);
});
test("Scheduler returns provider validation failures as client errors", async () => {
const start = indexSrc.indexOf("if (actionType === \"planner_next_to_agent\") {\n let selection;");
assert.ok(start >= 0);
const branch = sourceWindow(indexSrc.slice(start), 'if (actionType === "planner_next_to_agent") {', 'if (actionType === "agent_prompt") {', "Planner scheduling validation");
const { AgentRpcError } = require("../AgentWorker/private-rpc");
const { assertAiProviderReady } = require("../AgentWorker/provider-settings");
const responses = [];
const run = vm.runInNewContext(`(async () => { const actionType = 'planner_next_to_agent'; ${branch} })`, {
AgentRpcError, user: { id: 7 }, payload: { selection: "unknown:fixture" },
resolveAiModelSelectionForUserId: async () => assertAiProviderReady("unknown", "fixture", () => ({})),
res: { status: code => ({ json: body => responses.push({ code, ...body }) }) },
});
await run();
assert.deepStrictEqual(JSON.parse(JSON.stringify(responses)), [{ code: 400, ok: false, error: "invalid AI provider" }]);
});
test("Scheduler create still takes the in-flight guard and releases it on rejection", () => {
// Guard, set, finally clear — the mechanics this fix must not disturb
// (exact code-line occurrences; comments stripped so prose cannot count).
const code = createWindow.replace(/\/\/[^\n]*/g, "");
assert.strictEqual(
(code.match(/if \(schedulerCreateInFlight\) return;/g) || []).length,
1,
"the re-entrancy guard must stay exactly once"
);
assert.strictEqual(
(code.match(/schedulerCreateInFlight = true;/g) || []).length,
1,
"the guard must be set exactly once"
);
assert.strictEqual(
(code.match(/schedulerCreateInFlight = false;/g) || []).length,
1,
"the guard must be cleared exactly once — in the finally"
);
const postAt = createWindow.indexOf(postAnchor);
assert.notStrictEqual(postAt, -1, "the create POST is missing");
const finallyAt = createWindow.indexOf("} finally {");
assert.notStrictEqual(finallyAt, -1, "the finally is missing");
assert.ok(finallyAt > postAt, "the finally must sit after the create POST");
assert.ok(
createWindow.slice(finallyAt).includes("schedulerCreateBtn.disabled = false"),
"the finally must re-enable the create button on the abort path too"
);
});
test("the agent_prompt create branch has no model-save step to fail (explicit payload send)", () => {
assert.ok(
!agentPromptBranch.includes("saveSelectedAiModelToBackendNow"),
"the agent_prompt branch must not pre-save any model — its in-form dropdown rides the create payload explicitly (adpanel:scheduler-model-locality), so there is no save whose failure could bind a stale model"
);
assert.ok(
!agentPromptBranch.includes("saveSelectedAiModelToStorage"),
"the agent_prompt branch must not persist any model — the scheduler dropdown is per-task form state, never the account-global default"
);
assert.ok(
agentPromptBranch.includes("payload.provider = providerId;") && agentPromptBranch.includes("payload.model = modelName;"),
"the explicit payload send must stay — it is why this branch needs no fail-closed save"
);
});