1import { ScheduledTaskId } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3
4import { resolveUserMessagePresentation } from "./userMessage.ts";
5
6describe("resolveUserMessagePresentation", () => {
7 const legacyText = "[Triggered by schedule task: Daily audit]\n\nCheck for crashes.\n";
8
9 it("removes attribution from legacy scheduled prompts", () => {
10 expect(
11 resolveUserMessagePresentation({ role: "user", createdBy: "agent", text: legacyText }),
12 ).toEqual({
13 text: "Check for crashes.\n",
14 attribution: "automation",
15 scheduledTaskId: undefined,
16 });
17 });
18
19 it("uses task metadata without changing the prompt", () => {
20 expect(
21 resolveUserMessagePresentation({
22 role: "user",
23 createdBy: "agent",
24 scheduledTaskId: ScheduledTaskId.make("task-1"),
25 text: legacyText,
26 }),
27 ).toEqual({ text: legacyText, attribution: "automation", scheduledTaskId: "task-1" });
28 });
29
30 it("preserves user-written and assistant-quoted schedule headers", () => {
31 for (const message of [
32 { role: "user", createdBy: "user" as const },
33 { role: "user" },
34 { role: "assistant", createdBy: "agent" as const },
35 ]) {
36 expect(resolveUserMessagePresentation({ ...message, text: legacyText })).toEqual({
37 text: legacyText,
38 attribution: null,
39 scheduledTaskId: undefined,
40 });
41 }
42 });
43
44 it("leaves other agent prompts and embedded headers intact", () => {
45 for (const text of [
46 "Review this area",
47 `Quoted prompt:\n${legacyText}`,
48 "[Triggered by schedule task: Daily audit]",
49 ]) {
50 expect(resolveUserMessagePresentation({ role: "user", createdBy: "agent", text })).toEqual({
51 text,
52 attribution: "agent",
53 scheduledTaskId: undefined,
54 });
55 }
56 });
57
58 it("attributes older server-sent restart continuations to T3 Code, not another agent", () => {
59 const text = "Note: the T3 server restarted.";
60 expect(
61 resolveUserMessagePresentation({
62 role: "user",
63 createdBy: "agent",
64 creationSource: "server",
65 text,
66 }),
67 ).toMatchObject({ attribution: "t3code" });
68 for (const creationSource of ["mcp", "provider"] as const) {
69 expect(
70 resolveUserMessagePresentation({ role: "user", createdBy: "agent", creationSource, text }),
71 ).toMatchObject({ attribution: "agent" });
72 }
73 });
74
75 it("recovers the triggering automation from older scheduler message ids", () => {
76 for (const trigger of ["scheduled", "manual"]) {
77 expect(
78 resolveUserMessagePresentation({
79 id: `scheduled-task-message:task:daily-audit:1788661140000:${trigger}`,
80 role: "user",
81 createdBy: "user",
82 text: legacyText,
83 }),
84 ).toEqual({
85 text: "Check for crashes.\n",
86 attribution: "automation",
87 scheduledTaskId: "task:daily-audit",
88 });
89 }
90 });
91});