Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

userMessage.test.ts

74 lines2.4 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { GuestMessageId } from "@t3tools/contracts";
4​
5import { resolveUserMessagePresentation } from "./userMessage.ts";
6​
7describe("resolveUserMessagePresentation", () => {
8 const legacyText = "[Triggered by schedule task: Daily audit]\n\nCheck for crashes.\n";
9​
10 it("preserves user-written and assistant-quoted schedule headers", () => {
11 for (const message of [
12 { role: "user", createdBy: "user" as const },
13 { role: "user" },
14 { role: "assistant", createdBy: "agent" as const },
15 ]) {
16 expect(resolveUserMessagePresentation({ ...message, text: legacyText })).toEqual({
17 text: legacyText,
18 attribution: null,
19 scheduledTaskId: undefined,
20 });
21 }
22 });
23​
24 it("shows a guest's own words and name, not the attributed text the agent reads", () => {
25 expect(
26 resolveUserMessagePresentation({
27 role: "user",
28 createdBy: "user",
29 text: "Message from guest Sam, sent by the owner:\n\nWhy?",
30 context: {
31 guest: {
32 guestMessageId: GuestMessageId.make("guest-message:one"),
33 name: "Sam",
34 text: "Why?",
35 },
36 },
37 }),
38 ).toEqual({ text: "Why?", attribution: "guest", scheduledTaskId: undefined, guestName: "Sam" });
39 });
40​
41 it("attributes older server-sent restart continuations to T3 Code, not another agent", () => {
42 const text = "Note: the T3 server restarted.";
43 expect(
44 resolveUserMessagePresentation({
45 role: "user",
46 createdBy: "agent",
47 creationSource: "server",
48 text,
49 }),
50 ).toMatchObject({ attribution: "t3code" });
51 for (const creationSource of ["mcp", "provider"] as const) {
52 expect(
53 resolveUserMessagePresentation({ role: "user", createdBy: "agent", creationSource, text }),
54 ).toMatchObject({ attribution: "agent" });
55 }
56 });
57​
58 it("recovers the triggering automation from older scheduler message ids", () => {
59 for (const trigger of ["scheduled", "manual"]) {
60 expect(
61 resolveUserMessagePresentation({
62 id: `scheduled-task-message:task:daily-audit:1788661140000:${trigger}`,
63 role: "user",
64 createdBy: "user",
65 text: legacyText,
66 }),
67 ).toEqual({
68 text: "Check for crashes.\n",
69 attribution: "automation",
70 scheduledTaskId: "task:daily-audit",
71 });
72 }
73 });
74});