@@ -2,8 +2,11 @@ import { describe, it, expect, vi, beforeEach } from 'vitest';
22import { GET } from './route' ;
33import { createMockSupabaseClient , mockUser } from '@/test/mocks/supabase' ;
44
5+ const mockGetAuthenticatedUser = vi . fn ( ) ;
6+
57vi . mock ( '@/lib/supabase/server' , ( ) => ( {
68 createClient : vi . fn ( ) ,
9+ getAuthenticatedUser : ( ...args : unknown [ ] ) => mockGetAuthenticatedUser ( ...args ) ,
710} ) ) ;
811
912import { createClient } from '@/lib/supabase/server' ;
@@ -57,15 +60,10 @@ describe('GET /api/chat/history', () => {
5760 } ) ;
5861
5962 const mockSupabase = createMockSupabaseClient ( {
60- auth : {
61- getUser : vi . fn ( ) . mockResolvedValue ( {
62- data : { user : mockUser } ,
63- error : null ,
64- } ) ,
65- } ,
6663 from : mockFrom ,
6764 } ) ;
6865 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
66+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
6967
7068 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
7169 const request = new Request ( `http://localhost/api/chat/history?sessionId=${ sessionId } ` ) ;
@@ -98,15 +96,10 @@ describe('GET /api/chat/history', () => {
9896 } ) ;
9997
10098 const mockSupabase = createMockSupabaseClient ( {
101- auth : {
102- getUser : vi . fn ( ) . mockResolvedValue ( {
103- data : { user : mockUser } ,
104- error : null ,
105- } ) ,
106- } ,
10799 from : mockFrom ,
108100 } ) ;
109101 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
102+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
110103
111104 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
112105 const request = new Request ( `http://localhost/api/chat/history?sessionId=${ sessionId } ` ) ;
@@ -132,15 +125,10 @@ describe('GET /api/chat/history', () => {
132125 } ) ;
133126
134127 const mockSupabase = createMockSupabaseClient ( {
135- auth : {
136- getUser : vi . fn ( ) . mockResolvedValue ( {
137- data : { user : mockUser } ,
138- error : null ,
139- } ) ,
140- } ,
141128 from : mockFrom ,
142129 } ) ;
143130 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
131+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
144132
145133 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
146134 const before = '2024-01-01T00:00:30Z' ;
@@ -165,15 +153,10 @@ describe('GET /api/chat/history', () => {
165153 } ) ;
166154
167155 const mockSupabase = createMockSupabaseClient ( {
168- auth : {
169- getUser : vi . fn ( ) . mockResolvedValue ( {
170- data : { user : mockUser } ,
171- error : null ,
172- } ) ,
173- } ,
174156 from : mockFrom ,
175157 } ) ;
176158 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
159+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
177160
178161 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
179162 const request = new Request ( `http://localhost/api/chat/history?sessionId=${ sessionId } &limit=2` ) ;
@@ -186,15 +169,9 @@ describe('GET /api/chat/history', () => {
186169 } ) ;
187170
188171 it ( 'returns 400 for invalid session ID' , async ( ) => {
189- const mockSupabase = createMockSupabaseClient ( {
190- auth : {
191- getUser : vi . fn ( ) . mockResolvedValue ( {
192- data : { user : mockUser } ,
193- error : null ,
194- } ) ,
195- } ,
196- } ) ;
172+ const mockSupabase = createMockSupabaseClient ( { } ) ;
197173 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
174+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
198175
199176 const request = new Request ( 'http://localhost/api/chat/history?sessionId=invalid' ) ;
200177
@@ -203,15 +180,9 @@ describe('GET /api/chat/history', () => {
203180 } ) ;
204181
205182 it ( 'returns 400 for missing session ID' , async ( ) => {
206- const mockSupabase = createMockSupabaseClient ( {
207- auth : {
208- getUser : vi . fn ( ) . mockResolvedValue ( {
209- data : { user : mockUser } ,
210- error : null ,
211- } ) ,
212- } ,
213- } ) ;
183+ const mockSupabase = createMockSupabaseClient ( { } ) ;
214184 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
185+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
215186
216187 const request = new Request ( 'http://localhost/api/chat/history' ) ;
217188
@@ -220,15 +191,9 @@ describe('GET /api/chat/history', () => {
220191 } ) ;
221192
222193 it ( 'returns 400 for limit out of range' , async ( ) => {
223- const mockSupabase = createMockSupabaseClient ( {
224- auth : {
225- getUser : vi . fn ( ) . mockResolvedValue ( {
226- data : { user : mockUser } ,
227- error : null ,
228- } ) ,
229- } ,
230- } ) ;
194+ const mockSupabase = createMockSupabaseClient ( { } ) ;
231195 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
196+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
232197
233198 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
234199 const request = new Request (
@@ -249,15 +214,10 @@ describe('GET /api/chat/history', () => {
249214 } ) ;
250215
251216 const mockSupabase = createMockSupabaseClient ( {
252- auth : {
253- getUser : vi . fn ( ) . mockResolvedValue ( {
254- data : { user : mockUser } ,
255- error : null ,
256- } ) ,
257- } ,
258217 from : mockFrom ,
259218 } ) ;
260219 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
220+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
261221
262222 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
263223 const request = new Request ( `http://localhost/api/chat/history?sessionId=${ sessionId } ` ) ;
@@ -283,15 +243,10 @@ describe('GET /api/chat/history', () => {
283243 } ) ;
284244
285245 const mockSupabase = createMockSupabaseClient ( {
286- auth : {
287- getUser : vi . fn ( ) . mockResolvedValue ( {
288- data : { user : mockUser } ,
289- error : null ,
290- } ) ,
291- } ,
292246 from : mockFrom ,
293247 } ) ;
294248 vi . mocked ( createClient ) . mockResolvedValue ( mockSupabase as never ) ;
249+ mockGetAuthenticatedUser . mockResolvedValue ( { user : mockUser , error : null } ) ;
295250
296251 const sessionId = 'a1b2c3d4-e5f6-7890-abcd-ef1234567890' ;
297252 const request = new Request ( `http://localhost/api/chat/history?sessionId=${ sessionId } ` ) ;
0 commit comments