@@ -11,8 +11,8 @@ use forge_app::{
1111 WorkspaceStatus , compute_hash,
1212} ;
1313use forge_domain:: {
14- AuthCredential , AuthDetails , FileHash , FileNode , ProviderId , ProviderRepository , SyncProgress ,
15- UserId , WorkspaceId , WorkspaceIndexRepository , WorkspaceRepository ,
14+ AuthCredential , AuthDetails , Error , FileHash , FileNode , ProviderId , ProviderRepository ,
15+ SyncProgress , UserId , WorkspaceId , WorkspaceIndexRepository , WorkspaceRepository ,
1616} ;
1717use forge_stream:: MpscStream ;
1818use futures:: future:: join_all;
@@ -725,6 +725,43 @@ impl<
725725
726726 Ok ( auth)
727727 }
728+
729+ async fn init_workspace ( & self , path : PathBuf ) -> Result < WorkspaceId > {
730+ let ( token, user_id) = self . get_workspace_credentials ( ) . await ?;
731+ let path = path
732+ . canonicalize ( )
733+ . with_context ( || format ! ( "Failed to resolve path: {}" , path. display( ) ) ) ?;
734+
735+ // Find workspace by exact match or ancestor
736+ let workspace = self . find_workspace_by_path ( path. clone ( ) , & user_id) . await ?;
737+
738+ // Check if workspace already exists
739+ if let Some ( workspace) = workspace {
740+ if workspace. user_id == user_id {
741+ // Workspace already exists for this user
742+ return Err ( Error :: WorkspaceAlreadyInitialized ( workspace. workspace_id ) . into ( ) ) ;
743+ } else {
744+ // Found workspace but different user - delete old one
745+ if let Err ( e) = self . infra . delete ( & workspace. workspace_id ) . await {
746+ warn ! ( error = %e, "Failed to delete old workspace entry from local database" ) ;
747+ }
748+ }
749+ }
750+
751+ // Create new workspace on server
752+ let workspace_id = self
753+ . with_retry ( || self . infra . create_workspace ( & path, & token) )
754+ . await
755+ . context ( "Failed to create workspace on server" ) ?;
756+
757+ // Save workspace in database
758+ self . infra
759+ . upsert ( & workspace_id, & user_id, & path)
760+ . await
761+ . context ( "Failed to save workspace" ) ?;
762+
763+ Ok ( workspace_id)
764+ }
728765}
729766
730767#[ cfg( test) ]
@@ -1552,6 +1589,69 @@ mod tests {
15521589 ) ;
15531590 }
15541591
1592+ #[ tokio:: test]
1593+ async fn test_init_workspace_creates_new_workspace ( ) {
1594+ let mock = MockInfra {
1595+ files : [ ( "src/main.rs" , "fn main() {}" ) ]
1596+ . iter ( )
1597+ . map ( |( p, c) | ( p. to_string ( ) , c. to_string ( ) ) )
1598+ . collect ( ) ,
1599+ workspace : None , // No workspace yet
1600+ ancestor_workspace : None ,
1601+ authenticated : true ,
1602+ ..Default :: default ( )
1603+ } ;
1604+
1605+ let service = ForgeWorkspaceService :: new ( Arc :: new ( mock. clone ( ) ) ) ;
1606+
1607+ // Initialize workspace without syncing
1608+ let workspace_id = service
1609+ . init_workspace ( std:: env:: current_dir ( ) . unwrap ( ) )
1610+ . await
1611+ . unwrap ( ) ;
1612+
1613+ // Verify we got a valid workspace ID
1614+ assert ! ( !workspace_id. to_string( ) . is_empty( ) ) ;
1615+ }
1616+
1617+ #[ tokio:: test]
1618+ async fn test_init_workspace_reuses_existing_workspace ( ) {
1619+ let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
1620+ let user_id = UserId :: generate ( ) ;
1621+ let existing_workspace_id = WorkspaceId :: generate ( ) ;
1622+
1623+ let existing_workspace = Workspace {
1624+ workspace_id : existing_workspace_id. clone ( ) ,
1625+ user_id : user_id. clone ( ) ,
1626+ path : current_dir. clone ( ) ,
1627+ created_at : chrono:: Utc :: now ( ) ,
1628+ updated_at : None ,
1629+ } ;
1630+
1631+ let mock = MockInfra {
1632+ files : [ ( "src/main.rs" , "fn main() {}" ) ]
1633+ . iter ( )
1634+ . map ( |( p, c) | ( p. to_string ( ) , c. to_string ( ) ) )
1635+ . collect ( ) ,
1636+ workspace : Some ( existing_workspace) ,
1637+ ancestor_workspace : None ,
1638+ authenticated : true ,
1639+ ..Default :: default ( )
1640+ } ;
1641+
1642+ let service = ForgeWorkspaceService :: new ( Arc :: new ( mock. clone ( ) ) ) ;
1643+
1644+ // Initialize workspace - should reuse existing
1645+ let result = service. init_workspace ( current_dir) . await ;
1646+ assert ! ( result. is_err( ) ) ;
1647+ let err = result. unwrap_err ( ) ;
1648+ let domain_error = err. downcast_ref :: < Error > ( ) ;
1649+ assert ! ( matches!(
1650+ domain_error,
1651+ Some ( Error :: WorkspaceAlreadyInitialized ( id) ) if id == & existing_workspace_id
1652+ ) ) ;
1653+ }
1654+
15551655 #[ tokio:: test]
15561656 async fn test_sync_prefers_exact_match_over_ancestor ( ) {
15571657 // Use current directory as exact match
0 commit comments