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

chatThreadActions.test.ts

168 lines5.3 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vite-plus/test";
4import {
5 resolveThreadActionProjectRef,
6 resolveNewDraftStartFromOrigin,
7 startNewLocalThreadFromContext,
8 startNewThreadFromContext,
9 startNewThreadInProjectFromContext,
10 type ChatThreadActionContext,
11} from "./chatThreadActions";
12​
13const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
14const PROJECT_ID = ProjectId.make("project-1");
15const FALLBACK_PROJECT_ID = ProjectId.make("project-2");
16​
17function createContext(overrides: Partial<ChatThreadActionContext> = {}): ChatThreadActionContext {
18 return {
19 activeDraftThread: null,
20 activeThread: undefined,
21 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, FALLBACK_PROJECT_ID),
22 handleNewThread: async () => {},
23 ...overrides,
24 };
26​
27describe("chatThreadActions", () => {
28 it("only applies the start-from-origin default to new worktree drafts", () => {
29 expect(
30 resolveNewDraftStartFromOrigin({
31 envMode: "worktree",
32 newWorktreesStartFromOrigin: true,
33 }),
34 ).toBe(true);
35 expect(
36 resolveNewDraftStartFromOrigin({
37 envMode: "local",
38 newWorktreesStartFromOrigin: true,
39 }),
40 ).toBe(false);
41 });
42​
43 it("prefers the active draft thread project when resolving thread actions", () => {
44 const projectRef = resolveThreadActionProjectRef(
45 createContext({
46 activeDraftThread: {
47 environmentId: ENVIRONMENT_ID,
48 projectId: PROJECT_ID,
49 branch: "feature/refactor",
50 worktreePath: "/tmp/worktree",
51 envMode: "worktree",
52 startFromOrigin: true,
53 },
54 }),
55 );
56​
57 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
58 });
59​
60 it("falls back to the default project ref when there is no active thread context", () => {
61 const projectRef = resolveThreadActionProjectRef(
62 createContext({
63 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID),
64 }),
65 );
66​
67 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
68 });
69​
70 it("starts a contextual new thread from the active draft thread", async () => {
71 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
72​
73 const didStart = await startNewThreadFromContext(
74 createContext({
75 activeDraftThread: {
76 environmentId: ENVIRONMENT_ID,
77 projectId: PROJECT_ID,
78 branch: "feature/refactor",
79 worktreePath: "/tmp/worktree",
80 envMode: "worktree",
81 startFromOrigin: true,
82 },
83 handleNewThread,
84 }),
85 );
86​
87 expect(didStart).toBe(true);
88 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), {
89 branch: "feature/refactor",
90 worktreePath: "/tmp/worktree",
91 envMode: "worktree",
92 startFromOrigin: true,
93 });
94 });
95​
96 it("preserves an explicitly disabled origin base in contextual thread options", async () => {
97 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
98​
99 await startNewThreadFromContext(
100 createContext({
101 activeDraftThread: {
102 environmentId: ENVIRONMENT_ID,
103 projectId: PROJECT_ID,
104 branch: "feature/refactor",
105 worktreePath: "/tmp/worktree",
106 envMode: "worktree",
107 startFromOrigin: false,
108 },
109 handleNewThread,
110 }),
111 );
112​
113 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), {
114 branch: "feature/refactor",
115 worktreePath: "/tmp/worktree",
116 envMode: "worktree",
117 startFromOrigin: false,
118 });
119 });
120​
121 it("does not carry branch or worktree context into a different project", async () => {
122 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
123 const targetProjectRef = scopeProjectRef(ENVIRONMENT_ID, FALLBACK_PROJECT_ID);
124​
125 await startNewThreadInProjectFromContext(
126 createContext({
127 activeThread: {
128 environmentId: ENVIRONMENT_ID,
129 projectId: PROJECT_ID,
130 branch: "feature/refactor",
131 worktreePath: "/tmp/worktree",
132 },
133 handleNewThread,
134 }),
135 targetProjectRef,
136 );
137​
138 expect(handleNewThread).toHaveBeenCalledWith(targetProjectRef);
139 });
140​
141 it("delegates the target environment defaults to the new-thread handler", async () => {
142 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
143​
144 const didStart = await startNewLocalThreadFromContext(
145 createContext({
146 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID),
147 handleNewThread,
148 }),
149 );
150​
151 expect(didStart).toBe(true);
152 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
153 });
154​
155 it("does not start a thread when there is no project context", async () => {
156 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
157​
158 const didStart = await startNewThreadFromContext(
159 createContext({
160 defaultProjectRef: null,
161 handleNewThread,
162 }),
163 );
164​
165 expect(didStart).toBe(false);
166 expect(handleNewThread).not.toHaveBeenCalled();
167 });
168});