Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

33.7 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

import assert from "node:assert/strict";
import test from "node:test";
import { loadWithStubs } from "./helpers/module-stubs.ts";
import * as formatFastApiError from "../src/lib/format-fastapi-error.ts";
import * as zustand from "zustand";
import type * as ScheduledTasksApi from "../src/features/scheduled-tasks/api.ts";
import type * as ScheduledTasksStore from "../src/features/scheduled-tasks/store.ts";
import type * as UnseenStore from "../src/features/scheduled-tasks/unseen.ts";
import {
  ACTIVE_POLL_MAX_MS,
  ACTIVE_POLL_MS,
  CATCH_UP_MS,
  HIDDEN_POLL_MS,
  IDLE_POLL_MS,
  advanceSeenRuns,
  claimAnnouncements,
  collectFinishedRuns,
  nextPollDelay,
  pollDelayFor,
  readSeenRuns,
  runErrorText,
  runsToAnnounce,
  runsAskingToLoad,
  runsToNotify,
  readInRecents,
  unseenChatChanges,
  shouldSaveSeen,
} from "../src/features/scheduled-tasks/results.ts";
import {
  type ScheduleSummary,
  type TaskSchedule,
  ISO_WEEKDAYS,
  defaultScheduleForm,
  formFromSchedule,
  formatRunDay,
  relativeTimeParts,
  sameSchedule,
  scheduleFormComplete,
  scheduleFromForm,
  summarizeRepeats,
  summarizeSchedule,
  timeChoices,
  titleFromPrompt,
  zoneLabel,
} from "../src/features/scheduled-tasks/schedule.ts";

const ISO_ALL = [...ISO_WEEKDAYS];

// ICU puts a narrow no-break space before AM/PM; compare the words, not the spacing.
function plain(summary: ScheduleSummary): ScheduleSummary {
  const values = Object.fromEntries(
    Object.entries(summary.values).map(([key, value]) => [
      key,
      typeof value === "string" ? value.replace(/\s/g, " ") : value,
    ]),
  );
  return { ...summary, values } as ScheduleSummary;
}

test("a new task runs every day at 09:00, and a one-time one starts tomorrow", () => {
  assert.deepEqual(defaultScheduleForm(new Date(2026, 9, 14, 16, 30)), {
    repeat: "daily",
    hours: 1,
    time: "09:00",
    date: "2026-10-15",
    days: [3],
    dayOfMonth: 14,
  });
  assert.equal(defaultScheduleForm(new Date(2026, 11, 31, 12)).date, "2027-01-01");
});

test("the form round-trips every kind of schedule", () => {
  const now = new Date(2026, 9, 14);
  const schedules: TaskSchedule[] = [
    { kind: "once", at: "2026-10-15T09:30" },
    { kind: "hourly", hours: 1 },
    { kind: "hourly", hours: 2 },
    { kind: "hourly", hours: 5 },
    { kind: "hourly", hours: 24 },
    { kind: "daily", time: "07:00" },
    { kind: "weekly", time: "18:00", days: [1, 2, 3, 4, 5] },
    { kind: "weekly", time: "18:00", days: [2, 6] },
    { kind: "monthly", time: "08:15", day: 31 },
  ];
  for (const schedule of schedules) {
    assert.deepEqual(scheduleFromForm(formFromSchedule(schedule, now)), schedule);
  }
});

test("Monday to Friday reads back as Weekdays, anything else as Weekly", () => {
  const now = new Date(2026, 9, 14);
  const weekdays = formFromSchedule({ kind: "weekly", time: "09:00", days: [1, 2, 3, 4, 5] }, now);
  const weekly = formFromSchedule({ kind: "weekly", time: "09:00", days: [1, 2, 3, 4] }, now);
  assert.equal(weekdays.repeat, "weekdays");
  assert.deepEqual([weekly.repeat, weekly.days], ["weekly", [1, 2, 3, 4]]);
});

test("weekly days are sent once each, in order", () => {
  const form = { ...defaultScheduleForm(new Date(2026, 9, 14)), repeat: "weekly" as const, days: [5, 1, 5] };
  assert.deepEqual(scheduleFromForm(form), { kind: "weekly", time: "09:00", days: [1, 5] });
});

test("an incomplete form cannot be saved", () => {
  const form = defaultScheduleForm(new Date(2026, 9, 14));
  assert.equal(scheduleFormComplete(form), true);
  assert.equal(scheduleFormComplete({ ...form, time: "" }), false);
  assert.equal(scheduleFormComplete({ ...form, time: "24:00" }), false);
  assert.equal(scheduleFormComplete({ ...form, repeat: "once", date: "" }), false);
  assert.equal(scheduleFormComplete({ ...form, repeat: "weekly", days: [] }), false);
  assert.equal(scheduleFormComplete({ ...form, repeat: "monthly", dayOfMonth: 32 }), false);
  assert.equal(scheduleFormComplete({ ...form, days: [], date: "" }), true);
});

test("hourly intervals accept whole hours from 1 to 24 independently of calendar fields", () => {
  const form = { ...defaultScheduleForm(new Date(2026, 9, 14)), repeat: "hourly" as const, time: "", date: "" };
  for (const hours of [1, 2, 5, 24]) {
    assert.equal(scheduleFormComplete({ ...form, hours }), true);
    assert.deepEqual(scheduleFromForm({ ...form, hours }), { kind: "hourly", hours });
  }
  for (const hours of [0, -1, 1.5, 25, Number.NaN, Number.POSITIVE_INFINITY]) {
    assert.equal(scheduleFormComplete({ ...form, hours }), false);
  }
});

test("editing an hourly interval preserves its cadence unless the hour count changes", () => {
  const hourly: TaskSchedule = { kind: "hourly", hours: 2 };
  const form = formFromSchedule(hourly, new Date(2026, 9, 14));
  assert.ok(sameSchedule(hourly, scheduleFromForm(form)));
  assert.ok(!sameSchedule(hourly, scheduleFromForm({ ...form, hours: 3 })));
  assert.ok(!sameSchedule(hourly, { kind: "daily", time: "09:00" }));
  assert.ok(!sameSchedule({ kind: "daily", time: "09:00" }, hourly));
  assert.ok(!sameSchedule({ kind: "hourly", hours: 24 }, { kind: "daily", time: "09:00" }));
});

test("hourly summaries distinguish one hour from custom intervals without a wall-clock time", () => {
  assert.deepEqual(summarizeSchedule({ kind: "hourly", hours: 1 }, "en-US"), {
    key: "hourly", values: {},
  });
  assert.deepEqual(summarizeRepeats({ kind: "hourly", hours: 1 }, "en-US"), { key: "hourly" });
  for (const hours of [2, 5, 24]) {
    assert.deepEqual(summarizeSchedule({ kind: "hourly", hours }, "en-US"), {
      key: "everyHours", values: { hours },
    });
    assert.deepEqual(summarizeRepeats({ kind: "hourly", hours }, "en-US"), {
      key: "everyHours", hours,
    });
  }
});

test("schedules compare by what they mean", () => {
  assert.ok(
    sameSchedule(
      { kind: "weekly", time: "09:00", days: [1, 3] },
      { kind: "weekly", time: "09:00", days: [3, 1] },
    ),
  );
  assert.ok(!sameSchedule({ kind: "daily", time: "09:00" }, { kind: "daily", time: "09:01" }));
  assert.ok(
    !sameSchedule(
      { kind: "daily", time: "09:00" },
      { kind: "weekly", time: "09:00", days: [1, 2, 3, 4, 5, 6, 7] },
    ),
  );
  assert.ok(
    !sameSchedule(
      { kind: "monthly", time: "09:00", day: 1 },
      { kind: "monthly", time: "09:00", day: 2 },
    ),
  );
});

test("schedules are described in the user's locale", () => {
  assert.deepEqual(plain(summarizeSchedule({ kind: "daily", time: "21:05" }, "en-US")), {
    key: "daily",
    values: { time: "9:05 PM" },
  });
  assert.deepEqual(summarizeSchedule({ kind: "daily", time: "21:05" }, "de"), {
    key: "daily",
    values: { time: "21:05" },
  });
  assert.deepEqual(
    plain(summarizeSchedule({ kind: "weekly", time: "09:00", days: [1, 3, 5] }, "en-US")),
    { key: "weekly", values: { days: "Mon, Wed, and Fri", time: "9:00 AM" } },
  );
  assert.equal(
    summarizeSchedule({ kind: "weekly", time: "09:00", days: [1, 2, 3, 4, 5] }, "en-US").key,
    "weekdays",
  );
  assert.equal(
    summarizeSchedule({ kind: "weekly", time: "09:00", days: [1, 2, 3, 4, 5, 6, 7] }, "en-US").key,
    "daily",
  );
  assert.deepEqual(plain(summarizeSchedule({ kind: "once", at: "2026-10-15T09:00" }, "en-US")), {
    key: "once",
    values: { date: "Oct 15, 2026", time: "9:00 AM" },
  });
  assert.deepEqual(plain(summarizeSchedule({ kind: "monthly", time: "09:00", day: 31 }, "en-US")), {
    key: "monthly",
    values: { day: 31, time: "9:00 AM" },
  });
});

test("the Repeats column names the cadence without its time", () => {
  assert.deepEqual(summarizeRepeats({ kind: "daily", time: "09:00" }, "en-US"), { key: "daily" });
  assert.deepEqual(summarizeRepeats({ kind: "once", at: "2026-10-21T09:00" }, "en-US"), {
    key: "once",
  });
  assert.deepEqual(summarizeRepeats({ kind: "weekly", time: "09:00", days: ISO_ALL }, "en-US"), {
    key: "daily",
  });
  assert.deepEqual(
    summarizeRepeats({ kind: "weekly", time: "09:00", days: [1, 2, 3, 4, 5] }, "en-US"),
    { key: "weekdays" },
  );
  assert.deepEqual(summarizeRepeats({ kind: "weekly", time: "09:00", days: [1, 3, 5] }, "en-US"), {
    key: "weekly",
    days: "Mon, Wed, Fri",
  });
  assert.deepEqual(summarizeRepeats({ kind: "monthly", time: "09:00", day: 31 }, "en-US"), {
    key: "monthly",
    day: 31,
  });
});

test("a next run reads Today or Tomorrow, else its date", () => {
  const now = new Date(2026, 9, 20, 23, 30).getTime();
  assert.equal(formatRunDay(new Date(2026, 9, 20, 23, 45).getTime(), now, "en-US"), "Today");
  assert.equal(formatRunDay(new Date(2026, 9, 21, 0, 15).getTime(), now, "en-US"), "Tomorrow");
  assert.equal(formatRunDay(new Date(2026, 9, 21, 0, 15).getTime(), now, "de"), "Morgen");
  assert.equal(formatRunDay(new Date(2026, 9, 29, 9, 0).getTime(), now, "en-US"), "Thu, Oct 29");
});

test("the results poll looks quickly as a run starts, backs off while it goes on, and again just after one is due", () => {
  const going = { now: 0, activeRuns: 1, nextRunAt: null };
  assert.equal(nextPollDelay(going), ACTIVE_POLL_MS);
  assert.equal(nextPollDelay(going, ACTIVE_POLL_MS), 2 * ACTIVE_POLL_MS);
  assert.equal(nextPollDelay(going, ACTIVE_POLL_MAX_MS), ACTIVE_POLL_MAX_MS);
  assert.equal(nextPollDelay({ ...going, more: true }, ACTIVE_POLL_MAX_MS), ACTIVE_POLL_MS);
  assert.equal(nextPollDelay({ now: 0, activeRuns: 0, nextRunAt: null }), IDLE_POLL_MS);
  assert.equal(nextPollDelay({ now: 0, activeRuns: 0, nextRunAt: 60_000 }), 63_000);
  assert.equal(nextPollDelay({ now: 0, activeRuns: 0, nextRunAt: 1_000 }), 4_000);
  assert.equal(nextPollDelay({ now: 10_000, activeRuns: 0, nextRunAt: 0 }), ACTIVE_POLL_MS);
  assert.equal(nextPollDelay({ now: 0, activeRuns: 0, nextRunAt: 3_600_000 }), IDLE_POLL_MS);
  assert.equal(nextPollDelay({ now: 0, activeRuns: 0, nextRunAt: null, more: true }), ACTIVE_POLL_MS);
});

test("a hidden tab polls no faster than every 30 seconds, a shown one as paced", () => {
  assert.equal(pollDelayFor(ACTIVE_POLL_MS, true), HIDDEN_POLL_MS);
  assert.equal(pollDelayFor(ACTIVE_POLL_MS, false), ACTIVE_POLL_MS);
  assert.equal(pollDelayFor(IDLE_POLL_MS, true), IDLE_POLL_MS);
});

test("a poll that announced nothing writes the shared record only once its cursor moved a minute", () => {
  const stored = { since: 100_000, ids: ["a"] };
  assert.equal(shouldSaveSeen(stored, { since: 130_000, ids: ["a"] }, 0), false);
  assert.equal(shouldSaveSeen(stored, { since: 160_000, ids: ["a"] }, 0), true);
  assert.equal(shouldSaveSeen(stored, { since: 100_000, ids: ["a", "b"] }, 1), true);
  assert.equal(shouldSaveSeen(null, { since: 100_000, ids: [] }, 0), true);
});

test("a next run's day is counted in the task's zone, like its time", () => {
  // 00:15 on Oct 24 in New York is 21:15 on Oct 23 in Los Angeles.
  const at = Date.UTC(2026, 9, 24, 4, 15);
  const now = Date.UTC(2026, 9, 23, 20, 0);
  assert.equal(formatRunDay(at, now, "en-US", "America/New_York"), "Tomorrow");
  assert.equal(formatRunDay(at, now, "en-US", "America/Los_Angeles"), "Today");
});

test("formatters are kept per locale", () => {
  const at = new Date(2026, 9, 29, 9, 0).getTime();
  const now = new Date(2026, 9, 20, 9, 0).getTime();
  assert.equal(formatRunDay(at, now, "en-US"), "Thu, Oct 29");
  assert.notEqual(formatRunDay(at, now, "de-DE"), "Thu, Oct 29");
  assert.equal(formatRunDay(at, now, "en-US"), "Thu, Oct 29");
});

test("a saved record reads back as it was saved, with no clock of this browser's involved", () => {
  const stored = (since: unknown, ids: unknown = ["run-1"]) => JSON.stringify({ since, ids });
  assert.deepEqual(readSeenRuns(stored(5_000)), { since: 5_000, ids: ["run-1"] });
  assert.equal(readSeenRuns(null), null);
  assert.equal(readSeenRuns("not json"), null);
  assert.equal(readSeenRuns(JSON.stringify({ ids: [] })), null);
  assert.equal(readSeenRuns(stored("5000")), null);
  assert.deepEqual(readSeenRuns(stored(5_000, ["a", 7, null, "b"])), { since: 5_000, ids: ["a", "b"] });
  assert.deepEqual(readSeenRuns(JSON.stringify({ since: 5_000, ids: [], savedAt: 1 })), { since: 5_000, ids: [] });
  const seen = { since: 5_000, ids: ["a"] };
  assert.deepEqual(readSeenRuns(JSON.stringify(seen)), seen);
});

test("a record further behind the server than the catch-up window resumes from that window, by the server's clock", async () => {
  const now = 10 * CATCH_UP_MS;
  const oldest = now - CATCH_UP_MS;
  const runs = [finished("older", oldest - 60_000), finished("recent", oldest + 60_000)];
  const asked: (number | null)[] = [];
  const fetchAnswer = async (since: number | null) => {
    asked.push(since);
    const after = runs.filter((run) => since !== null && (run.finishedAt ?? 0) >= since);
    return { now, runs: after, more: false, activeRuns: 0, nextRunAt: null };
  };
  const resumed = await collectFinishedRuns({ since: now - 3 * CATCH_UP_MS, ids: [] }, fetchAnswer);
  assert.deepEqual(asked, [now - 3 * CATCH_UP_MS, oldest]);
  assert.deepEqual(resumed.fresh.map((run) => run.id), ["recent"]);
  assert.equal(resumed.seen.since, now - 60_000);
  asked.length = 0;
  const kept = await collectFinishedRuns({ since: oldest - 1 + 60_000, ids: [] }, fetchAnswer);
  assert.deepEqual(asked, [oldest - 1 + 60_000]);
  assert.deepEqual(kept.fresh.map((run) => run.id), ["recent"]);
});

test("the record moves on every poll, a minute behind the server, and remembers what it announced", () => {
  const answer = (now: number) => ({ now, runs: [], more: false });
  const first = advanceSeenRuns(null, answer(600_000), []);
  assert.deepEqual(first, { since: 540_000, ids: [] });
  const second = advanceSeenRuns(first, answer(900_000), ["run-1"]);
  assert.deepEqual(second, { since: 840_000, ids: ["run-1"] });
  assert.equal(advanceSeenRuns(second, answer(700_000), []).since, 840_000);
  const many = Array.from({ length: 260 }, (_, index) => `run-${index}`);
  const capped = advanceSeenRuns(second, answer(900_000), many).ids;
  assert.equal(capped.length, 200);
  assert.equal(capped.at(-1), "run-259");
});

function finished(id: string, finishedAt: number): ScheduledTasksApi.FinishedScheduledTaskRun {
  return {
    id,
    taskId: "task-1",
    source: "manual",
    status: "succeeded",
    scheduledFor: null,
    createdAt: finishedAt,
    startedAt: finishedAt,
    finishedAt,
    threadId: `chat-${id}`,
    model: null,
    errorCode: null,
    error: null,
    taskTitle: "Morning brief",
    taskNotify: "all",
    taskModel: null,
  };
}

test("results past one answer are read a page at a time and announced once", async () => {
  const runs = [finished("a", 1_000), finished("b", 2_000), finished("c", 3_000)];
  const asked: (number | null)[] = [];
  const fetchAnswer = async (since: number | null) => {
    asked.push(since);
    const after = runs.filter((run) => since !== null && (run.finishedAt ?? 0) >= since);
    return { now: 100_000, runs: after.slice(0, 2), more: after.length > 2, activeRuns: 0, nextRunAt: null };
  };
  const { fresh, seen, last } = await collectFinishedRuns({ since: 0, ids: [] }, fetchAnswer);
  assert.deepEqual(asked, [0, 2_000]);
  assert.deepEqual(fresh.map((run) => run.id), ["a", "b", "c"]);
  assert.deepEqual(seen, { since: 40_000, ids: ["a", "b", "c"] });
  assert.equal(last.more, false);
});

test("a run that succeeded after its chat was deleted is not announced, least of all as failed", () => {
  const ready = finished("ready", 1_000);
  const gone = { ...finished("gone", 2_000), threadId: null };
  const failed = { ...finished("failed", 3_000), status: "failed" as const, threadId: null };
  assert.deepEqual(runsToAnnounce([ready, gone, failed]).map((run) => run.id), ["ready", "failed"]);
});

test("a task's toasts cover every run, only its failures, or none", () => {
  const ok = finished("ok", 1_000);
  const bad = { ...finished("bad", 2_000), status: "failed" as const, threadId: null };
  const notified = (taskNotify: ScheduledTasksApi.TaskNotify) =>
    runsToNotify([ok, bad].map((run) => ({ ...run, taskNotify }))).map((run) => run.id);
  assert.deepEqual(notified("all"), ["ok", "bad"]);
  assert.deepEqual(notified("failures"), ["bad"]);
  assert.deepEqual(notified("none"), []);
});

test("of an account's tabs, the first to claim a run announces it, and the record keeps the furthest cursor", () => {
  const fresh = [finished("a", 3_000), finished("b", 3_500)];
  const { mine, record } = claimAnnouncements({ since: 3_200, ids: ["a"] }, { since: 4_000, ids: ["a", "b"] }, fresh);
  assert.deepEqual(mine.map((run) => run.id), ["b"]);
  assert.deepEqual(record, { since: 4_000, ids: ["a", "b"] });
  const late = claimAnnouncements({ since: 3_200, ids: [] }, { since: 4_000, ids: [] }, fresh);
  assert.deepEqual(late.mine.map((run) => run.id), ["b"]);
  assert.equal(claimAnnouncements({ since: 5_000, ids: [] }, { since: 4_000, ids: [] }, fresh).record.since, 5_000);
  const first = claimAnnouncements(null, { since: 4_000, ids: [] }, fresh);
  assert.deepEqual(first.mine.map((run) => run.id), ["a", "b"]);
  assert.deepEqual(first.record, { since: 4_000, ids: ["a", "b"] });
});

test("a backlog longer than one poll reads is picked up again shortly", async () => {
  let pages = 0;
  const endless = async () => {
    pages += 1;
    return { now: 100_000, runs: [finished(`run-${pages}`, pages)], more: true, activeRuns: 0, nextRunAt: null };
  };
  const { fresh, seen, last } = await collectFinishedRuns(null, endless);
  assert.equal(pages, 10);
  assert.equal(fresh.length, 10);
  assert.equal(seen.since, 10);
  assert.equal(nextPollDelay(last), ACTIVE_POLL_MS);
});

test("unread chats follow the stored unread result chats", () => {
  assert.deepEqual(unseenChatChanges(["c1"], []), { added: ["c1"], opened: [] });
  assert.deepEqual(unseenChatChanges(["c1", "c2"], ["c1"]), { added: ["c2"], opened: [] });
  assert.deepEqual(unseenChatChanges(["c2"], ["c1", "c2"]), { added: [], opened: ["c1"] });
});

test("a result chat marked read in Recents is read for good, others untouched", () => {
  const before = new Set(["c1", "c2", "other"]);
  assert.deepEqual(readInRecents(["c1", "c2"], before, new Set(["c2", "other"])), ["c1"]);
  assert.deepEqual(readInRecents(["c1", "c2"], before, new Set()), ["c1", "c2"]);
  assert.deepEqual(readInRecents(["c3"], before, new Set()), []);
});

test("a failed run is worded from its error code, else from the server's text", () => {
  const t = (key: string) => `<${key}>`;
  assert.equal(
    runErrorText({ errorCode: "no_model", error: null }, t),
    "<scheduledTasks.errors.noModel>",
  );
  assert.equal(runErrorText({ errorCode: null, error: "  Context full.  " }, t), "Context full.");
  assert.equal(
    runErrorText({ errorCode: "something_new", error: null }, t),
    "<scheduledTasks.errors.unknown>",
  );
});

test("relative times use the largest whole unit", () => {
  assert.deepEqual(relativeTimeParts(0, 30_000), [-30, "second"]);
  assert.deepEqual(relativeTimeParts(0, 5 * 60_000), [-5, "minute"]);
  assert.deepEqual(relativeTimeParts(0, 2 * 3_600_000), [-2, "hour"]);
  assert.deepEqual(relativeTimeParts(3 * 86_400_000, 0), [3, "day"]);
});

function client(respond: (path: string, init?: RequestInit) => Response) {
  const requests: { method: string; path: string; body: unknown }[] = [];
  const api = loadWithStubs<typeof ScheduledTasksApi>(
    new URL("../src/features/scheduled-tasks/api.ts", import.meta.url),
    {
      "@/features/auth": {
        authFetch: async (path: string, init?: RequestInit) => {
          requests.push({
            method: init?.method ?? "GET",
            path,
            body: typeof init?.body === "string" ? JSON.parse(init.body) : undefined,
          });
          return respond(path, init);
        },
        getAuthSessionEpoch: () => 1,
      },
      "@/lib/format-fastapi-error": formatFastApiError,
    },
  );
  return { api, requests };
}

test("the client reaches every scheduled task route", async () => {
  const c = client((path, init) =>
    init?.method === "DELETE"
      ? new Response(null, { status: 204 })
      : Response.json(path === "/api/scheduled-tasks" && !init?.method ? { tasks: [], now: 7 } : {}),
  );
  const schedule: TaskSchedule = { kind: "daily", time: "09:00" };
  assert.deepEqual(await c.api.listScheduledTasks(), { tasks: [], now: 7 });
  await c.api.createScheduledTask({
    title: "Brief",
    prompt: "Hi",
    schedule,
    timezone: "UTC",
    notify: "failures",
    named: false,
  });
  await c.api.updateScheduledTask("a/b", { enabled: false });
  assert.equal(await c.api.deleteScheduledTask("t1"), undefined);
  await c.api.runScheduledTaskNow("t1");
  await c.api.getScheduledTaskActivity(null);
  await c.api.getScheduledTaskActivity(42);
  assert.deepEqual(
    c.requests.map(({ method, path }) => [method, path]),
    [
      ["GET", "/api/scheduled-tasks"],
      ["POST", "/api/scheduled-tasks"],
      ["PATCH", "/api/scheduled-tasks/a%2Fb"],
      ["DELETE", "/api/scheduled-tasks/t1"],
      ["POST", "/api/scheduled-tasks/t1/run"],
      ["GET", "/api/scheduled-tasks/runs"],
      ["GET", "/api/scheduled-tasks/runs?since=42"],
    ],
  );
  assert.deepEqual(c.requests[1].body, {
    title: "Brief",
    prompt: "Hi",
    schedule,
    timezone: "UTC",
    notify: "failures",
    named: false,
  });
  assert.deepEqual(c.requests[2].body, { enabled: false });
});

test("a task change reaches the account's other tabs, and a deletion has Recents look again", async () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  const stored = new Map<string, string>();
  globals.window = new EventTarget();
  globals.localStorage = { setItem: (key: string, value: string) => void stored.set(key, value) };
  let historyRefreshes = 0;
  try {
    const store = loadWithStubs<typeof ScheduledTasksStore>(
      new URL("../src/features/scheduled-tasks/store.ts", import.meta.url),
      {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "signed-out", getAuthSessionEpoch: () => 1 },
        "@/features/chat": {
          notifyChatHistoryUpdated: () => {
            historyRefreshes += 1;
          },
        },
        "./api": { deleteScheduledTask: async () => undefined },
        "./results": {
          SCHEDULED_TASKS_CHANGED_EVENT: "changed",
          SCHEDULED_TASKS_WRITTEN_KEY: "written",
          SCHEDULED_TASK_RUNS_FINISHED_EVENT: "finished",
        },
      },
    );
    await store.useScheduledTasksStore.getState().remove("t1");
    const first = stored.get(store.SCHEDULED_TASKS_WRITTEN_KEY);
    assert.ok(first);
    assert.equal(historyRefreshes, 1);
    await store.useScheduledTasksStore.getState().remove("t2");
    assert.notEqual(stored.get(store.SCHEDULED_TASKS_WRITTEN_KEY), first);
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("a Run now answer arriving after its run finished keeps the finished run", async () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  globals.window = new EventTarget();
  globals.localStorage = { setItem: () => undefined };
  try {
    let answer: (run: unknown) => void = () => undefined;
    const store = loadWithStubs<typeof ScheduledTasksStore>(
      new URL("../src/features/scheduled-tasks/store.ts", import.meta.url),
      {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "signed-out", getAuthSessionEpoch: () => 1 },
        "@/features/chat": { notifyChatHistoryUpdated: () => undefined },
        "./api": { runScheduledTaskNow: () => new Promise((resolve) => (answer = resolve)) },
        "./results": {
          SCHEDULED_TASKS_CHANGED_EVENT: "changed",
          SCHEDULED_TASKS_WRITTEN_KEY: "written",
          SCHEDULED_TASK_RUNS_FINISHED_EVENT: "finished",
        },
      },
    );
    const tasks = store.useScheduledTasksStore;
    tasks.setState({ tasks: [{ id: "t1", lastRun: null } as never] });
    const pending = tasks.getState().runNow("t1");
    tasks.setState({
      tasks: [{ id: "t1", lastRun: { id: "r1", createdAt: 1, finishedAt: 2, status: "succeeded" } } as never],
    });
    answer({ id: "r1", createdAt: 1, finishedAt: null, status: "queued" });
    await pending;
    assert.equal(tasks.getState().tasks[0].lastRun?.status, "succeeded");
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("an edit answer that predates its run's finish keeps the finished run", async () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  globals.window = new EventTarget();
  globals.localStorage = { setItem: () => undefined };
  try {
    let answer: (task: unknown) => void = () => undefined;
    const store = loadWithStubs<typeof ScheduledTasksStore>(
      new URL("../src/features/scheduled-tasks/store.ts", import.meta.url),
      {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "signed-out", getAuthSessionEpoch: () => 1 },
        "@/features/chat": { notifyChatHistoryUpdated: () => undefined },
        "./api": { updateScheduledTask: () => new Promise((resolve) => (answer = resolve)) },
        "./results": {
          SCHEDULED_TASKS_CHANGED_EVENT: "changed",
          SCHEDULED_TASKS_WRITTEN_KEY: "written",
          SCHEDULED_TASK_RUNS_FINISHED_EVENT: "finished",
        },
      },
    );
    const tasks = store.useScheduledTasksStore;
    const run = { id: "r1", createdAt: 1, finishedAt: null, status: "running" };
    const pending = tasks.getState().update("t1", { title: "Renamed" });
    tasks.setState({
      tasks: [{ id: "t1", title: "Old", lastRun: { ...run, finishedAt: 2, status: "succeeded" } } as never],
    });
    answer({ id: "t1", title: "Renamed", lastRun: run });
    await pending;
    const [task] = tasks.getState().tasks;
    assert.equal(task.title, "Renamed");
    assert.equal(task.lastRun?.status, "succeeded");
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("an edit answered after a refresh that landed meanwhile reads the list again", async () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  globals.window = new EventTarget();
  globals.localStorage = { setItem: () => undefined };
  try {
    let answer: (task: unknown) => void = () => undefined;
    let lists = 0;
    const completed = { id: "t1", title: "Old", status: "completed", nextRunAt: null, lastRun: null };
    const store = loadWithStubs<typeof ScheduledTasksStore>(
      new URL("../src/features/scheduled-tasks/store.ts", import.meta.url),
      {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "signed-out", getAuthSessionEpoch: () => 1 },
        "@/features/chat": { notifyChatHistoryUpdated: () => undefined },
        "./api": {
          updateScheduledTask: () => new Promise((resolve) => (answer = resolve)),
          listScheduledTasks: async () => {
            lists += 1;
            return { tasks: [{ ...completed, title: lists > 1 ? "Renamed" : "Old" }], now: 0 };
          },
        },
        "./results": {
          SCHEDULED_TASKS_CHANGED_EVENT: "changed",
          SCHEDULED_TASKS_WRITTEN_KEY: "written",
          SCHEDULED_TASK_RUNS_FINISHED_EVENT: "finished",
        },
      },
    );
    const tasks = store.useScheduledTasksStore;
    const pending = tasks.getState().update("t1", { title: "Renamed" });
    // The once task is claimed and completes; a refresh sees it before the rename's answer arrives.
    await tasks.getState().refresh();
    answer({ ...completed, title: "Renamed", status: "active", nextRunAt: 5 });
    await pending;
    for (let i = 0; i < 5 && lists < 2; i += 1) await Promise.resolve();
    assert.equal(lists, 2);
    await new Promise((resolve) => setTimeout(resolve, 0));
    const [task] = tasks.getState().tasks;
    assert.equal(task.status, "completed");
    assert.equal(task.nextRunAt, null);
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("every unopened result chat stays unread across reloads, and opening one clears only it", () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  const stored = new Map<string, string>();
  globals.window = new EventTarget();
  globals.localStorage = {
    getItem: (key: string) => stored.get(key) ?? null,
    setItem: (key: string, value: string) => void stored.set(key, value),
  };
  try {
    const load = () =>
      loadWithStubs<typeof UnseenStore>(new URL("../src/features/scheduled-tasks/unseen.ts", import.meta.url), {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "out", AUTH_SESSION_STORED_EVENT: "in", getAuthToken: () => "t" },
        "@/lib/account-transition": { browserAccountKey: () => "owner" },
        "./results": { UNSEEN_KEY_PREFIX: "unseen:" },
      }).useUnseenScheduledRuns;
    const first = load();
    first.getState().markFinished([{ taskId: "t1", threadId: "c1" }], false);
    first.getState().markFinished([{ taskId: "t1", threadId: "c2" }], false);
    assert.deepEqual(load().getState().threads, ["c1", "c2"]);
    load().getState().clearThread("c1");
    assert.deepEqual(load().getState().threads, ["c2"]);
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("a write that drops a list refresh in flight asks for the list again", async () => {
  const globals = globalThis as Record<string, unknown>;
  const saved = { window: globals.window, localStorage: globals.localStorage };
  globals.window = new EventTarget();
  globals.localStorage = { setItem: () => undefined };
  try {
    let lists = 0;
    let firstList: (list: unknown) => void = () => undefined;
    const store = loadWithStubs<typeof ScheduledTasksStore>(
      new URL("../src/features/scheduled-tasks/store.ts", import.meta.url),
      {
        zustand,
        "@/features/auth": { AUTH_SESSION_CLEARED_EVENT: "signed-out", getAuthSessionEpoch: () => 1 },
        "@/features/chat": { notifyChatHistoryUpdated: () => undefined },
        "./api": {
          listScheduledTasks: () => {
            lists += 1;
            if (lists === 1) return new Promise((resolve) => (firstList = resolve));
            return Promise.resolve({ tasks: [{ id: "t1", lastRun: { id: "r1", status: "succeeded" } }], now: 0 });
          },
          runScheduledTaskNow: async () => ({ id: "r1", createdAt: 1, finishedAt: null, status: "queued" }),
        },
        "./results": {
          SCHEDULED_TASKS_CHANGED_EVENT: "changed",
          SCHEDULED_TASKS_WRITTEN_KEY: "written",
          SCHEDULED_TASK_RUNS_FINISHED_EVENT: "finished",
        },
      },
    );
    const tasks = store.useScheduledTasksStore;
    const dropped = tasks.getState().refresh();
    await tasks.getState().runNow("t1");
    firstList({ tasks: [], now: 0 });
    await dropped;
    await new Promise((resolve) => setTimeout(resolve, 0));
    assert.equal(lists, 2);
    assert.equal(tasks.getState().tasks[0]?.lastRun?.status, "succeeded");
  } finally {
    globals.window = saved.window;
    globals.localStorage = saved.localStorage;
  }
});

test("a refused request carries the server's reason", async () => {
  const c = client(() => Response.json({ detail: "Pick a time in the future." }, { status: 400 }));
  await assert.rejects(c.api.updateScheduledTask("t1", { enabled: true }), {
    message: "Pick a time in the future.",
  });
});

test("times are offered every quarter hour, keeping a saved one that falls between", () => {
  const choices = timeChoices("09:00");
  assert.equal(choices.length, 96);
  assert.deepEqual(choices.slice(0, 3), ["00:00", "00:15", "00:30"]);
  assert.equal(choices.at(-1), "23:45");
  const kept = timeChoices("09:07");
  const nine = kept.indexOf("09:00");
  assert.deepEqual(kept.slice(nine, nine + 3), ["09:00", "09:07", "09:15"]);
  assert.equal(timeChoices("9am").length, 96);
});

test("a time's zone is named as briefly as the locale allows", () => {
  const at = Date.UTC(2026, 9, 17, 12);
  assert.equal(zoneLabel("America/Los_Angeles", "en-US", at), "PT");
  assert.equal(zoneLabel("Europe/Berlin", "de", at), "MEZ");
  assert.equal(zoneLabel("Europe/Istanbul", "en-US", at), "GMT+3");
  assert.equal(zoneLabel("UTC", "en-US", at), "UTC");
  assert.equal(zoneLabel("Nowhere/Special", "en-US", at), "Nowhere/Special");
});

test("a task left unnamed is named after its instructions' first line", () => {
  assert.equal(titleFromPrompt("  What's the weather today?\nIn Paris."), "What's the weather today?");
  assert.equal(titleFromPrompt("Summarize   three\tpapers"), "Summarize three papers");
  const long = "Write a short upbeat note to start the day with three ideas for staying focused";
  const title = titleFromPrompt(long);
  assert.ok(title.endsWith("…") && title.length <= 61, title);
  assert.ok(long.startsWith(title.slice(0, -1)), "cut at a word, from the start");
  const emoji = titleFromPrompt(`Describe ${"\u{1F600}".repeat(70)}`);
  assert.ok(!/[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?<![\uD800-\uDBFF])[\uDC00-\uDFFF]/.test(emoji), "no lone surrogate");
  assert.equal(Array.from(emoji).length, 61);
});

test("a run whose model was not loaded asks once per task, whatever its notifications say", () => {
  const run = (id: string, taskId: string, errorCode: string | null, taskModel: string | null) => ({
    ...finished(id, 1_000),
    status: "failed" as const,
    taskId,
    errorCode,
    taskModel,
    taskNotify: "none" as const,
  });
  const asks = runsAskingToLoad([
    run("a", "task-1", "model_not_loaded", "unsloth/Qwen3-1.7B-GGUF"),
    run("b", "task-1", "model_not_loaded", "unsloth/Qwen3-1.7B-GGUF"),
    run("c", "task-2", "no_model", null),
    run("e", "task-4", "model_busy", "unsloth/gemma-3-GGUF"),
    run("d", "task-3", "model_not_loaded", null),
  ]);
  assert.deepEqual(
    asks.map((ask) => ask.id),
    ["b"],
  );
});