blob: bd499f849a3f36dcc192d2b0187dacc49a7466f7 [file] [log] [blame]
philip.zeyliger26bc6592025-06-30 20:15:30 -07001/* eslint-disable @typescript-eslint/no-explicit-any */
bankseancad67b02025-06-27 21:57:05 +00002import { test, expect } from "@sand4rt/experimental-ct-web";
philip.zeyliger26bc6592025-06-30 20:15:30 -07003import { State } from "./types";
bankseancad67b02025-06-27 21:57:05 +00004
5// Test the messages-viewer logic without importing the actual module
6// to avoid decorator issues in the test environment
7
8// Helper function to create mock session data
9function createMockSessionData(overrides: any = {}) {
10 return {
11 session_id: "test-session-123",
12 user_name: "john.doe",
13 user_email: "john.doe@example.com",
14 session_state: {
15 git_username: "john.doe",
16 git_origin: "https://github.com/example/repo.git",
17 session_id: "test-session-123",
18 ...overrides.session_state,
19 },
20 ...overrides,
21 };
22}
23
24// Helper function to create mock view data
25function createMockViewData(overrides: any = {}) {
26 return {
27 SessionWithData: overrides.SessionWithData || createMockSessionData(),
28 Messages: [
29 {
30 type: "user",
31 content: "Hello, this is a test user message",
32 timestamp: new Date().toISOString(),
33 conversation_id: "test-conv",
34 idx: 1,
35 hide_output: false,
36 },
37 {
38 type: "agent",
39 content: "This is an agent response",
40 timestamp: new Date().toISOString(),
41 conversation_id: "test-conv",
42 idx: 2,
43 hide_output: false,
44 },
45 ...(overrides.Messages || []),
46 ],
47 ToolResults: overrides.ToolResults || {},
48 };
49}
50
51// Test the state creation logic directly
52function createStateFromViewData(viewData: any): Partial<State> {
53 const sessionWithData = viewData.SessionWithData;
54 return {
55 session_id: sessionWithData?.session_id || "",
56 git_username:
57 sessionWithData?.session_state?.git_username ||
58 sessionWithData?.user_name,
59 git_origin: sessionWithData?.session_state?.git_origin,
60 };
61}
62
63test("creates proper state object with git_username from session_state", () => {
64 const mockViewData = createMockViewData();
65 const state = createStateFromViewData(mockViewData);
66
67 expect(state.session_id).toBe("test-session-123");
68 expect(state.git_username).toBe("john.doe");
69 expect(state.git_origin).toBe("https://github.com/example/repo.git");
70});
71
72test("uses git_username from session_state when available", () => {
73 const mockViewData = createMockViewData({
74 SessionWithData: createMockSessionData({
75 user_name: "fallback.user",
76 session_state: {
77 git_username: "primary.user", // This should take precedence
78 },
79 }),
80 });
81
82 const state = createStateFromViewData(mockViewData);
83 expect(state.git_username).toBe("primary.user");
84});
85
86test("falls back to user_name when session_state.git_username not available", () => {
87 const mockViewData = createMockViewData({
88 SessionWithData: createMockSessionData({
89 user_name: "fallback.user",
90 session_state: {
91 // git_username not provided
92 },
93 }),
94 });
95
96 const state = createStateFromViewData(mockViewData);
97 expect(state.git_username).toBe("fallback.user");
98});
99
100test("handles missing git username gracefully", () => {
101 const mockViewData = createMockViewData({
102 SessionWithData: {
103 session_id: "test-session-123",
104 // user_name not provided
105 session_state: {
106 // git_username not provided
107 },
108 },
109 });
110
111 const state = createStateFromViewData(mockViewData);
112 expect(state.git_username).toBeUndefined();
113});
114
115test("message filtering logic works correctly", () => {
116 const messages = [
117 {
118 type: "user",
119 content: "Visible message",
120 hide_output: false,
121 timestamp: new Date().toISOString(),
122 conversation_id: "test-conv",
123 idx: 1,
124 },
125 {
126 type: "agent",
127 content: "Hidden message",
128 hide_output: true, // This should be filtered out
129 timestamp: new Date().toISOString(),
130 conversation_id: "test-conv",
131 idx: 2,
132 },
133 {
134 type: "agent",
135 content: "Another visible message",
136 hide_output: false,
137 timestamp: new Date().toISOString(),
138 conversation_id: "test-conv",
139 idx: 3,
140 },
141 ];
142
143 // Test the filtering logic
144 const visibleMessages = messages.filter((msg: any) => !msg.hide_output);
145
146 expect(visibleMessages).toHaveLength(2);
147 expect(visibleMessages[0].content).toBe("Visible message");
148 expect(visibleMessages[1].content).toBe("Another visible message");
149});
150
151test("handles empty or malformed session data", () => {
152 const mockViewData = {
153 SessionWithData: null, // Malformed data
154 Messages: [],
155 ToolResults: {},
156 };
157
158 // Should not throw an error
159 expect(() => {
160 const state = createStateFromViewData(mockViewData);
161 expect(state.session_id).toBe("");
162 expect(state.git_username).toBeUndefined();
163 }).not.toThrow();
164});
165
166test("preserves git_origin from session state", () => {
167 const mockViewData = createMockViewData({
168 SessionWithData: createMockSessionData({
169 session_state: {
170 git_origin: "https://github.com/test/repository.git",
171 git_username: "test.user",
172 },
173 }),
174 });
175
176 const state = createStateFromViewData(mockViewData);
177 expect(state.git_origin).toBe("https://github.com/test/repository.git");
178});
179
180test("fallback hierarchy works correctly", () => {
181 // Test all combinations of the fallback hierarchy
182 const testCases = [
183 {
184 name: "session_state.git_username takes precedence",
185 sessionData: {
186 user_name: "fallback",
187 session_state: { git_username: "primary" },
188 },
189 expected: "primary",
190 },
191 {
192 name: "user_name when session_state.git_username missing",
193 sessionData: {
194 user_name: "fallback",
195 session_state: {},
196 },
197 expected: "fallback",
198 },
199 {
200 name: "undefined when both missing",
201 sessionData: {
202 session_id: "test-session-123",
203 // user_name not provided
204 session_state: {
205 // git_username not provided
206 },
207 },
208 expected: undefined,
209 },
210 ];
211
212 testCases.forEach(({ name, sessionData, expected }) => {
213 const mockViewData = createMockViewData({
214 SessionWithData:
215 name === "undefined when both missing"
216 ? sessionData
217 : createMockSessionData(sessionData),
218 });
219
220 const state = createStateFromViewData(mockViewData);
221 expect(state.git_username).toBe(expected);
222 });
223});