11import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22
3+ import { scopeProjectRef } from "@t3tools/client-runtime/environment" ;
34import { EnvironmentId , ProjectId , ThreadId } from "@t3tools/contracts" ;
45
5- const { readPreparedConnection, entities } = vi . hoisted ( ( ) => ( {
6+ const { readPreparedConnection, entities, settingsHydration } = vi . hoisted ( ( ) => ( {
67 readPreparedConnection : vi . fn < ( ) => { httpBaseUrl : string } | null > ( ( ) => null ) ,
78 entities : {
89 projects : [ ] as Array < Record < string , unknown > > ,
910 shellProjectId : null as string | null ,
1011 } ,
12+ settingsHydration : {
13+ hydrated : true ,
14+ finish : ( ) => { } ,
15+ promise : Promise . resolve ( ) ,
16+ } ,
1117} ) ) ;
1218
1319vi . mock ( "~/state/session" , ( ) => ( { readPreparedConnection } ) ) ;
@@ -26,13 +32,16 @@ vi.mock("./hooks/useSettings", async () => {
2632 return {
2733 getClientSettings : ( ) => DEFAULT_CLIENT_SETTINGS ,
2834 useClientSettings : ( ) => DEFAULT_CLIENT_SETTINGS ,
35+ isClientSettingsHydrated : ( ) => settingsHydration . hydrated ,
36+ ensureClientSettingsHydrated : ( ) => settingsHydration . promise ,
2937 } ;
3038} ) ;
3139vi . mock ( "~/state/environments" , ( ) => ( {
3240 readPrimaryEnvironmentId : ( ) => null ,
3341 usePrimaryEnvironmentId : ( ) => null ,
3442} ) ) ;
3543
44+ import { DraftId , useComposerDraftStore } from "./composerDraftStore" ;
3645import {
3746 BROWSER_HISTORY_MAX_PROJECTS ,
3847 type BrowserHistoryEntry ,
@@ -54,6 +63,9 @@ beforeEach(() => {
5463 readPreparedConnection . mockReturnValue ( null ) ;
5564 entities . projects = [ ] ;
5665 entities . shellProjectId = null ;
66+ settingsHydration . hydrated = true ;
67+ settingsHydration . promise = Promise . resolve ( ) ;
68+ useComposerDraftStore . setState ( { draftThreadsByThreadKey : { } } ) ;
5769} ) ;
5870afterEach ( ( ) => vi . restoreAllMocks ( ) ) ;
5971
@@ -195,6 +207,14 @@ describe("useBrowserHistoryStore", () => {
195207 } ) ;
196208} ) ;
197209
210+ const repoProject = {
211+ environmentId : EnvironmentId . make ( "env-1" ) ,
212+ id : ProjectId . make ( "project-1" ) ,
213+ workspaceRoot : "/repo" ,
214+ repositoryIdentity : null ,
215+ isScratch : false ,
216+ } ;
217+
198218describe ( "thread project resolution" , ( ) => {
199219 beforeEach ( ( ) => {
200220 resetBrowserHistoryForTests ( ) ;
@@ -222,6 +242,45 @@ describe("thread project resolution", () => {
222242 expect ( useBrowserHistoryStore . getState ( ) . pendingTitlesByThreadKey ) . toEqual ( { } ) ;
223243 } ) ;
224244
245+ it ( "files a draft thread's visits under the draft's project" , ( ) => {
246+ entities . projects = [ repoProject ] ;
247+ const projectRef = scopeProjectRef ( EnvironmentId . make ( "env-1" ) , ProjectId . make ( "project-1" ) ) ;
248+ useComposerDraftStore
249+ . getState ( )
250+ . setLogicalProjectDraftThreadId ( "env-1:/repo" , projectRef , DraftId . make ( "draft-1" ) , {
251+ threadId : threadRef . threadId ,
252+ } ) ;
253+
254+ recordVisitForThread ( threadRef , "http://localhost:5173/" , 1 ) ;
255+
256+ expect ( useBrowserHistoryStore . getState ( ) . byProjectKey ) . toEqual ( {
257+ "env-1:/repo" : [ { url : "http://localhost:5173/" , lastVisitedAt : 1 } ] ,
258+ } ) ;
259+ expect ( useBrowserHistoryStore . getState ( ) . pendingVisitsByThreadKey ) . toEqual ( { } ) ;
260+ } ) ;
261+
262+ it ( "waits for client settings before filing a thread under its project" , async ( ) => {
263+ entities . projects = [ repoProject ] ;
264+ entities . shellProjectId = "project-1" ;
265+ settingsHydration . hydrated = false ;
266+ settingsHydration . promise = new Promise < void > ( ( resolve ) => {
267+ settingsHydration . finish = ( ) => {
268+ settingsHydration . hydrated = true ;
269+ resolve ( ) ;
270+ } ;
271+ } ) ;
272+
273+ recordVisitForThread ( threadRef , "http://localhost:5173/" , 1 ) ;
274+ expect ( useBrowserHistoryStore . getState ( ) . projectKeyByThreadKey ) . toEqual ( { } ) ;
275+
276+ settingsHydration . finish ( ) ;
277+ await settingsHydration . promise ;
278+ expect ( useBrowserHistoryStore . getState ( ) . byProjectKey ) . toEqual ( {
279+ "env-1:/repo" : [ { url : "http://localhost:5173/" , lastVisitedAt : 1 } ] ,
280+ } ) ;
281+ expect ( useBrowserHistoryStore . getState ( ) . pendingVisitsByThreadKey ) . toEqual ( { } ) ;
282+ } ) ;
283+
225284 it ( "lands visits queued before the thread's project loaded with the next visit" , ( ) => {
226285 recordVisitForThread ( threadRef , "http://a.test/" , 1 ) ;
227286 entities . projects = [
0 commit comments