Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime";
2import { EnvironmentId, ProjectId } from "@t3tools/contracts";
3import { describe, expect, it, vi } from "vitest";
4import {
5 resolveThreadActionProjectRef,
6 startNewLocalThreadFromContext,
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 defaultThreadEnvMode: "local",
21 handleNewThread: async () => {},
22 ...overrides,
23 };
24}
25
26describe("chatThreadActions", () => {
27 it("prefers the active draft thread project when resolving thread actions", () => {
28 const projectRef = resolveThreadActionProjectRef(
29 createContext({
30 activeDraftThread: {
31 environmentId: ENVIRONMENT_ID,
32 projectId: PROJECT_ID,
33 branch: "feature/refactor",
34 worktreePath: "/tmp/worktree",
35 envMode: "worktree",
36 },
37 }),
38 );
39
40 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
41 });
42
43 it("falls back to the default project ref when there is no active thread context", () => {
44 const projectRef = resolveThreadActionProjectRef(
45 createContext({
46 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID),
47 }),
48 );
49
50 expect(projectRef).toEqual(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID));
51 });
52
53 it("starts a contextual new thread from the active draft thread", async () => {
54 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
55
56 const didStart = await startNewThreadFromContext(
57 createContext({
58 activeDraftThread: {
59 environmentId: ENVIRONMENT_ID,
60 projectId: PROJECT_ID,
61 branch: "feature/refactor",
62 worktreePath: "/tmp/worktree",
63 envMode: "worktree",
64 },
65 handleNewThread,
66 }),
67 );
68
69 expect(didStart).toBe(true);
70 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), {
71 branch: "feature/refactor",
72 worktreePath: "/tmp/worktree",
73 envMode: "worktree",
74 });
75 });
76
77 it("starts a local thread with the configured default env mode", async () => {
78 const handleNewThread = vi.fn<ChatThreadActionContext["handleNewThread"]>(async () => {});
79
80 const didStart = await startNewLocalThreadFromContext(
81 createContext({
82 defaultProjectRef: scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID),
83 defaultThreadEnvMode: "worktree",
84 handleNewThread,
85 }),
86 );
87
88 expect(didStart).toBe(true);
89 expect(handleNewThread).toHaveBeenCalledWith(scopeProjectRef(ENVIRONMENT_ID, PROJECT_ID), {
90 envMode: "worktree",
91 });
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});