@@ -5,15 +5,12 @@ type MockPublicUser = {
55 username : string ;
66 name : string | null ;
77 avatarUrl : string | null ;
8+ lastLoginAt : Date | null ;
9+ publicUsageEnabled : boolean | null ;
810} ;
911
10- type MockPublicUserPreferences = {
11- publicUsageEnabled : boolean ;
12- } ;
13-
14- const findUserMock = mock ( async ( ) : Promise < MockPublicUser | null > => null ) ;
15- const findUserPreferencesMock = mock (
16- async ( ) : Promise < MockPublicUserPreferences | null > => null ,
12+ const findPublicUsersByUsernameMock = mock (
13+ async ( ) : Promise < MockPublicUser [ ] > => [ ] ,
1714) ;
1815const getUsageHistoryMock = mock ( async ( ) => [ ] ) ;
1916const getUsageInsightsMock = mock ( async ( ) => ( {
@@ -43,14 +40,15 @@ const getUsageInsightsMock = mock(async () => ({
4340
4441mock . module ( "./client" , ( ) => ( {
4542 db : {
46- query : {
47- users : {
48- findFirst : findUserMock ,
49- } ,
50- userPreferences : {
51- findFirst : findUserPreferencesMock ,
52- } ,
53- } ,
43+ select : ( ) => ( {
44+ from : ( ) => ( {
45+ leftJoin : ( ) => ( {
46+ where : ( ) => ( {
47+ limit : findPublicUsersByUsernameMock ,
48+ } ) ,
49+ } ) ,
50+ } ) ,
51+ } ) ,
5452 } ,
5553} ) ) ;
5654
@@ -65,11 +63,11 @@ mock.module("./usage-insights", () => ({
6563const publicUsageProfileModulePromise = import ( "./public-usage-profile" ) ;
6664
6765beforeEach ( ( ) => {
68- findUserMock . mockClear ( ) ;
69- findUserPreferencesMock . mockClear ( ) ;
66+ findPublicUsersByUsernameMock . mockClear ( ) ;
7067 getUsageHistoryMock . mockClear ( ) ;
7168 getUsageInsightsMock . mockClear ( ) ;
7269
70+ findPublicUsersByUsernameMock . mockImplementation ( async ( ) => [ ] ) ;
7371 getUsageHistoryMock . mockImplementation ( async ( ) => [ ] ) ;
7472 getUsageInsightsMock . mockImplementation ( async ( ) => ( {
7573 lookbackDays : 0 ,
@@ -262,7 +260,7 @@ describe("getPublicUsageProfile", () => {
262260 test ( "returns null when the user does not exist" , async ( ) => {
263261 const { getPublicUsageProfile } = await publicUsageProfileModulePromise ;
264262
265- findUserMock . mockImplementation ( async ( ) => null ) ;
263+ findPublicUsersByUsernameMock . mockImplementation ( async ( ) => [ ] ) ;
266264
267265 expect ( await getPublicUsageProfile ( "missing-user" , null ) ) . toBeNull ( ) ;
268266 expect ( getUsageHistoryMock ) . not . toHaveBeenCalled ( ) ;
@@ -272,15 +270,16 @@ describe("getPublicUsageProfile", () => {
272270 test ( "returns null when public usage is disabled" , async ( ) => {
273271 const { getPublicUsageProfile } = await publicUsageProfileModulePromise ;
274272
275- findUserMock . mockImplementation ( async ( ) => ( {
276- id : "user-1" ,
277- username : "private-user" ,
278- name : "Private User" ,
279- avatarUrl : null ,
280- } ) ) ;
281- findUserPreferencesMock . mockImplementation ( async ( ) => ( {
282- publicUsageEnabled : false ,
283- } ) ) ;
273+ findPublicUsersByUsernameMock . mockImplementation ( async ( ) => [
274+ {
275+ id : "user-1" ,
276+ username : "private-user" ,
277+ name : "Private User" ,
278+ avatarUrl : null ,
279+ lastLoginAt : new Date ( "2026-01-01T00:00:00.000Z" ) ,
280+ publicUsageEnabled : false ,
281+ } ,
282+ ] ) ;
284283
285284 expect ( await getPublicUsageProfile ( "private-user" , null ) ) . toBeNull ( ) ;
286285 expect ( getUsageHistoryMock ) . not . toHaveBeenCalled ( ) ;
@@ -290,15 +289,16 @@ describe("getPublicUsageProfile", () => {
290289 test ( "uses all-time queries when no valid date is provided" , async ( ) => {
291290 const { getPublicUsageProfile } = await publicUsageProfileModulePromise ;
292291
293- findUserMock . mockImplementation ( async ( ) => ( {
294- id : "user-2" ,
295- username : "all-time-user" ,
296- name : null ,
297- avatarUrl : null ,
298- } ) ) ;
299- findUserPreferencesMock . mockImplementation ( async ( ) => ( {
300- publicUsageEnabled : true ,
301- } ) ) ;
292+ findPublicUsersByUsernameMock . mockImplementation ( async ( ) => [
293+ {
294+ id : "user-2" ,
295+ username : "all-time-user" ,
296+ name : null ,
297+ avatarUrl : null ,
298+ lastLoginAt : new Date ( "2026-01-02T00:00:00.000Z" ) ,
299+ publicUsageEnabled : true ,
300+ } ,
301+ ] ) ;
302302
303303 const profile = await getPublicUsageProfile ( "all-time-user" , "bad-value" ) ;
304304
@@ -317,24 +317,39 @@ describe("getPublicUsageProfile", () => {
317317 } ) ;
318318 } ) ;
319319
320- test ( "forwards explicit date ranges to usage queries " , async ( ) => {
320+ test ( "prefers an enabled case-insensitive match " , async ( ) => {
321321 const { getPublicUsageProfile } = await publicUsageProfileModulePromise ;
322322
323- findUserMock . mockImplementation ( async ( ) => ( {
324- id : "user-3" ,
325- username : "range-user" ,
326- name : "Range User" ,
327- avatarUrl : null ,
328- } ) ) ;
329- findUserPreferencesMock . mockImplementation ( async ( ) => ( {
330- publicUsageEnabled : true ,
331- } ) ) ;
323+ findPublicUsersByUsernameMock . mockImplementation ( async ( ) => [
324+ {
325+ id : "user-disabled" ,
326+ username : "range-user" ,
327+ name : "Disabled User" ,
328+ avatarUrl : null ,
329+ lastLoginAt : new Date ( "2026-01-01T00:00:00.000Z" ) ,
330+ publicUsageEnabled : false ,
331+ } ,
332+ {
333+ id : "user-3" ,
334+ username : "Range-User" ,
335+ name : "Range User" ,
336+ avatarUrl : null ,
337+ lastLoginAt : new Date ( "2026-01-03T00:00:00.000Z" ) ,
338+ publicUsageEnabled : true ,
339+ } ,
340+ ] ) ;
332341
333342 const profile = await getPublicUsageProfile (
334- "range-user " ,
343+ "RANGE-USER " ,
335344 "2026-01-01..2026-01-31" ,
336345 ) ;
337346
347+ expect ( profile ?. user ) . toEqual ( {
348+ id : "user-3" ,
349+ username : "Range-User" ,
350+ name : "Range User" ,
351+ avatarUrl : null ,
352+ } ) ;
338353 expect ( profile ?. dateSelection ) . toEqual ( {
339354 kind : "range" ,
340355 value : "2026-01-01..2026-01-31" ,
0 commit comments