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

chatThreadActions.test.ts

107 lines3.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 startNewThreadFromContext,
8 type ChatThreadActionContext,
9} from "./chatThreadActions";
10​
11const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
12const PROJECT_ID = ProjectId.make("project-1");
13const FALLBACK_PROJECT_ID = ProjectId.make("project-2");
14​
15function createContext(overrides: Partial<ChatThreadActionContext> = {}): ChatThreadActionContext {
16 return {
17 activeDraftThread: null,
18 activeThread: undefined,
19 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, FALLBACK_PROJECT_ID),
20 handleNewThread: async () => {},
21 ...overrides,
22 };
24​
25describe("chatThreadActions", () => {
26 it("only applies the start-from-origin default to new worktree drafts", () => {
27 expect(
28 resolveNewDraftStartFromOrigin({
29 envMode: "worktree",
30 newWorktreesStartFromOrigin: true,
31 }),
32 ).toBe(true);
33 expect(
34 resolveNewDraftStartFromOrigin({
35 envMode: "local",
36 newWorktreesStartFromOrigin: true,
37 }),
38 ).toBe(false);
39 });
40​
41 it("prefers the active thread project when resolving thread actions", () => {
42 const projectRef = resolveThreadActionProjectRef(
43 createContext({
44 activeThread: {
45 environmentId: ENVIRONMENT_ID,
46 projectId: PROJECT_ID,
47 },
48 }),
49 );
50​
51 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
52 });
53​
54 it("falls back to the active draft thread project when there is no active thread", () => {
55 const projectRef = resolveThreadActionProjectRef(
56 createContext({
57 activeDraftThread: {
58 environmentId: ENVIRONMENT_ID,
59 projectId: PROJECT_ID,
60 },
61 }),
62 );
63​
64 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
65 });
66​
67 it("falls back to the default project ref when there is no active thread context", () => {
68 const projectRef = resolveThreadActionProjectRef(
69 createContext({
70 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID),
71 }),
72 );
73​
74 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
75 });
76​
77 it("inherits only the project from context, never branch or worktree state", async () => {
78 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
79​
80 const didStart = await startNewThreadFromContext(
81 createContext({
82 activeThread: {
83 environmentId: ENVIRONMENT_ID,
84 projectId: PROJECT_ID,
85 },
86 handleNewThread,
87 }),
88 );
89​
90 expect(didStart).toBe(true);
91 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
92 });
93​
94 it("does not start a thread when there is no project context", async () => {
95 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
96​
97 const didStart = await startNewThreadFromContext(
98 createContext({
99 defaultProjectRef: null,
100 handleNewThread,
101 }),
102 );
103​
104 expect(didStart).toBe(false);
105 expect(handleNewThread).not.toHaveBeenCalled();
106 });
107});