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

chatThreadActions.test.ts

147 lines4.6 KB

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