Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions apps/mobile/src/features/threads/threadListV2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
import { createInboxReturnTracker } from "@t3tools/client-runtime/state/thread-inbox";
import {
createInboxReturnTracker,
WORKING_EXIT_GRACE_MS,
} from "@t3tools/client-runtime/state/thread-inbox";
import { presentThreadShell } from "@t3tools/client-runtime/state/models";
import * as DateTime from "effect/DateTime";
import * as Schema from "effect/Schema";
Expand Down Expand Up @@ -1776,10 +1779,10 @@ describe("Working section beta", () => {
tracker.observe([inactive]);
expect(render().workingCount).toBe(1);
expect(render().items[0]?.thread.runtime).toBeNull();
vi.advanceTimersByTime(2_000);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(render().workingCount).toBe(0);
expect(ids(render())).toEqual(["grace"]);
expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + 2_000);
expect(tracker.returnedAt(inactive)).toBe(Date.parse(NOW) + WORKING_EXIT_GRACE_MS);
} finally {
tracker.observe(null);
vi.useRealTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EnvironmentId } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Option from "effect/Option";
import { Atom, AtomRegistry } from "effect/reactivity";
import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
import { expect, it, vi } from "vite-plus/test";

import { fakeTimers, stubGlobal } from "../../test/render";
Expand Down Expand Up @@ -279,8 +280,9 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
},
);

it("presents completion notifications and sound as soon as the shell completes", async () => {
it("presents completion notifications and sound once the Working shelf's exit grace passes", async () => {
await using app = setup();
using _timers = fakeTimers();
state.mode = "notifications-and-sound";
app.start();
state.shells.set(
Expand All @@ -289,6 +291,7 @@ it("presents completion notifications and sound as soon as the shell completes",
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:00:01.000Z" },
}),
);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(TestNotification.sent.map((sent) => sent.title)).toEqual(["Thread completed"]);
expect(state.sound).toHaveBeenCalledWith("completion", expect.any(Function));
});
Expand Down Expand Up @@ -327,6 +330,7 @@ it.each([false, true])(
"one",
watching("turn", "completed", "2026-09-13T07:00:01.000Z", false, monitor),
);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(titles()).toEqual(["Thread completed"]);

state.shells.set("one", watching("wake", "running", null, true, monitor));
Expand All @@ -351,6 +355,7 @@ it.each([false, true])(
state.shells.set("one", watching("final", "running", null, false, []));
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
state.shells.set("one", watching("final", "completed", "2026-09-13T07:00:11.000Z", false, []));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(titles()).toEqual(["Thread completed", "Thread completed"]);
expect(state.sound).toHaveBeenCalledTimes(2);
},
Expand All @@ -360,6 +365,7 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
"notifies for offline completion and %s once across reconnects",
async (flag) => {
await using app = setup();
using _timers = fakeTimers();
const asking = { id: "asking", hasPendingApprovals: true };
const offline = { id: "offline", [flag]: true };
const done = {
Expand Down Expand Up @@ -387,18 +393,20 @@ it.each(["hasPendingApprovals", "hasPendingUserInput"] as const)(
status: "synchronizing",
});
state.shells.set("one", shell(completed, asking, offline, done));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);

expect(TestNotification.sent.map(({ title }) => title)).toEqual([
"Approval needed",
"Thread completed",
flag === "hasPendingApprovals" ? "Approval needed" : "Input needed",
"Thread completed",
]);
state.shells.set("one", { ...shell(completed, asking, offline, done), status: "cached" });
state.shells.set("one", shell(completed, asking, offline, done));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(TestNotification.sent.map(({ title }) => title)).toEqual([
"Approval needed",
"Thread completed",
flag === "hasPendingApprovals" ? "Approval needed" : "Input needed",
"Thread completed",
]);
},
);
Expand All @@ -407,20 +415,24 @@ it.each([false, true])(
"does not replay a notified completion after reconnect (continued: %s)",
async (continued) => {
await using app = setup();
using _timers = fakeTimers();
app.start();
const completed = shell({
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:05:00.000Z" },
});
state.shells.set("one", completed);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
state.shells.set("one", { ...completed, status: "cached" });
expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]);
state.shells.set("one", continued ? shell() : completed);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(TestNotification.sent.map(({ title }) => title)).toEqual(["Thread completed"]);
},
);

it("opening a thread or removing its environment closes only its in-app toasts, and disabling notifications closes the rest", async () => {
await using app = setup();
using _timers = fakeTimers();
state.inApp = true;
focused = true;
state.toast
Expand All @@ -434,12 +446,15 @@ it("opening a thread or removing its environment closes only its in-app toasts,
latestTurn: { turnId: "turn", state: "completed", completedAt: "2026-09-13T07:01:00Z" },
};
state.shells.set("one", shell(completed, { id: "other" }));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
state.shells.set("one", shell({ ...completed, hasPendingApprovals: true }, { id: "other" }));
state.shells.set(
"one",
shell({ ...completed, hasPendingApprovals: true }, { ...completed, id: "other" }),
);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
state.shells.set("two", shell(completed));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(state.toast).toHaveBeenCalledTimes(4);

app.openThread("one", "thread");
Expand Down
114 changes: 80 additions & 34 deletions apps/web/src/components/ThreadNotificationCoordinator.logic.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WORKING_EXIT_GRACE_MS } from "@t3tools/client-runtime/state/thread-inbox";
import { describe, expect, it, vi } from "vite-plus/test";

import {
Expand Down Expand Up @@ -28,6 +29,10 @@ const woke = (at: number): ThreadNotificationObservation => ({
});
const visible = { archived: false, quiet: false, snoozed: false };
const snoozed = { ...visible, snoozed: true };
const graces = [
["an ordinary", working, completed, WORKING_EXIT_GRACE_MS],
["a wake's", waking, woke, RELEASED_COMPLETION_GRACE_MS],
] as const;

function fakeTimers(): Disposable {
vi.useFakeTimers();
Expand All @@ -47,41 +52,59 @@ function harness() {

describe("createThreadNotificationGate", () => {
it("skips a completion seen while snoozed, but still notifies attention", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(working);
observe(completed(1), snoozed);
observe({ ...completed(1) });
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
observe({ ...working, attention: "run-2:approval" }, snoozed);
expect(notify.mock.calls).toEqual([["thread", "input"]]);
});

it("notifies a completion immediately and only once", () => {
it("notifies a completion once, after the Working shelf's exit grace", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(working);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1);
observe({ ...completed(1) });
expect(notify).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
observe({ ...completed(1) });
observe(working);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify).toHaveBeenCalledTimes(1);
});

it("notifies each newer completion immediately", () => {
it("drops a completion whose run continues inside the grace, and notifies each newer one", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(working);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS - 1);
observe(working);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
observe(completed(2));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
observe(working);
observe(completed(3));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify.mock.calls).toEqual([
["thread", "completion"],
["thread", "completion"],
]);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify).toHaveBeenCalledTimes(2);
});

it("notifies each attention once per stretch and still notifies the subsequent completion", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(working);
const attention = { ...working, attention: "run-1:approval:a", requestId: "a" };
Expand All @@ -102,6 +125,7 @@ describe("createThreadNotificationGate", () => {
expect(notify).toHaveBeenCalledTimes(5);
observe(working);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify.mock.calls).toEqual([
["thread", "input"],
["thread", "input"],
Expand All @@ -112,19 +136,30 @@ describe("createThreadNotificationGate", () => {
]);
});

it("does not replay a completion after attention resolves", () => {
it.each([
[
"after",
WORKING_EXIT_GRACE_MS,
[
["thread", "completion"],
["thread", "input"],
],
],
["inside", WORKING_EXIT_GRACE_MS - 1, [["thread", "input"]]],
])("does not replay a completion after attention %s the grace resolves", (_when, wait, calls) => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(working);
observe(completed(1));
vi.advanceTimersByTime(wait);
observe({ ...working, attention: "run-1:limited" });
observe(completed(1));
expect(notify.mock.calls).toEqual([
["thread", "completion"],
["thread", "input"],
]);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify.mock.calls).toEqual(calls);
});

it("never notifies existing, archived or stopped completions, including when unarchived", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(completed(1));
observe(working);
Expand All @@ -133,13 +168,16 @@ describe("createThreadNotificationGate", () => {
observe(working);
observe(completed(3), { ...visible, archived: true });
observe(completed(3));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
observe(working);
observe(completed(4));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
});

it("forgets handled completions when a thread is removed or the gate is cleared", () => {
using _timers = fakeTimers();
for (const reset of [
(gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.retain(new Set()),
(gate: ReturnType<typeof createThreadNotificationGate<string>>) => gate.clear(),
Expand All @@ -149,6 +187,7 @@ describe("createThreadNotificationGate", () => {
reset(gate);
gate.observe("thread", undefined, working, visible);
gate.observe("thread", working, completed(1), visible);
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
}
});
Expand All @@ -158,6 +197,7 @@ describe("createThreadNotificationGate", () => {
const { notify, observe } = harness();
observe(working);
observe(completed(1));
vi.advanceTimersByTime(WORKING_EXIT_GRACE_MS);
// Held until the monitor left, or completing after it already had: either way unheld here.
observe(waking);
observe(woke(2));
Expand All @@ -173,34 +213,40 @@ describe("createThreadNotificationGate", () => {
]);
});

it("notifies a wake's completion once after the grace when no run follows, even across a pause", () => {
using _timers = fakeTimers();
const { gate, notify, observe } = harness();
observe(waking);
observe(woke(1));
gate.pause();
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
observe({ ...woke(1) });
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS - 1);
observe({ ...woke(1) });
expect(notify).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
observe({ ...woke(1) });
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).toHaveBeenCalledTimes(1);
});
it.each(graces)(
"notifies %s completion once after its own grace when no run follows, even across a pause",
(_kind, running, done, graceMs) => {
using _timers = fakeTimers();
const { gate, notify, observe } = harness();
observe(running);
observe(done(1));
gate.pause();
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
observe({ ...done(1) });
vi.advanceTimersByTime(graceMs - 1);
observe({ ...done(1) });
expect(notify).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(notify.mock.calls).toEqual([["thread", "completion"]]);
observe({ ...done(1) });
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).toHaveBeenCalledTimes(1);
},
);

it("drops a pending wake completion when the thread is snoozed", () => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(waking);
observe(woke(1));
observe({ ...woke(1) }, snoozed);
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
});
it.each(graces)(
"drops %s pending completion when the thread is snoozed",
(_kind, running, done) => {
using _timers = fakeTimers();
const { notify, observe } = harness();
observe(running);
observe(done(1));
observe({ ...done(1) }, snoozed);
vi.advanceTimersByTime(RELEASED_COMPLETION_GRACE_MS);
expect(notify).not.toHaveBeenCalled();
},
);

it("drops a pending wake completion when the thread is removed or the gate is cleared", () => {
using _timers = fakeTimers();
Expand Down
Loading
Loading