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