@@ -722,26 +722,41 @@ describe("declarationRouter", () => {
722722 ) ;
723723 } ) ;
724724
725- it ( "refuses getOrCreate when no declaration exists (would create a draft)" , async ( ) => {
726- // Simulate the "no existing row" branch — where getOrCreate would
727- // normally insert a new draft. The impersonation guard fires right
728- // before the insert, so the admin never creates a draft in the
729- // user's name.
730- const txSelect = vi . fn ( ) . mockImplementation ( ( ) => ( {
731- from : vi . fn ( ) . mockReturnValue ( {
732- where : vi . fn ( ) . mockReturnValue ( {
733- limit : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
725+ it ( "returns a placeholder declaration in mimoquage when none exists (no insert)" , async ( ) => {
726+ // When an admin mimoques a company that has not started a
727+ // declaration, getOrCreate must return a transient empty row so the
728+ // read-only UI can render. No INSERT is performed (issue #3230).
729+ const emptySelect = ( ) =>
730+ vi . fn ( ) . mockImplementation ( ( ) => ( {
731+ from : vi . fn ( ) . mockReturnValue ( {
732+ where : vi . fn ( ) . mockReturnValue ( {
733+ limit : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
734+ } ) ,
734735 } ) ,
735- } ) ,
736- } ) ) ;
737- const tx = { select : txSelect , insert : vi . fn ( ) , delete : vi . fn ( ) } ;
736+ } ) ) ;
737+ const insertSpy = vi . fn ( ) ;
738+ const tx = {
739+ select : emptySelect ( ) ,
740+ insert : insertSpy ,
741+ delete : vi . fn ( ) ,
742+ } ;
738743 mockTransaction . mockImplementation ( async ( fn : ( tx : unknown ) => unknown ) =>
739744 fn ( tx ) ,
740745 ) ;
741- const mockDb = { transaction : mockTransaction } as unknown ;
746+ const mockDb = {
747+ transaction : mockTransaction ,
748+ select : emptySelect ( ) ,
749+ } as unknown ;
742750 const caller = await createCaller ( mockDb , null , impersonation ) ;
743751
744- await expect ( caller . getOrCreate ( ) ) . rejects . toThrow ( "Mode mimoquage" ) ;
752+ const result = await caller . getOrCreate ( ) ;
753+ expect ( result . declaration . id ) . toBe ( "" ) ;
754+ expect ( result . declaration . siren ) . toBe ( impersonation . siren ) ;
755+ expect ( result . declaration . status ) . toBe ( "draft" ) ;
756+ expect ( result . declaration . currentStep ) . toBe ( 0 ) ;
757+ expect ( result . jobCategories ) . toEqual ( [ ] ) ;
758+ expect ( result . employeeCategories ) . toEqual ( [ ] ) ;
759+ expect ( insertSpy ) . not . toHaveBeenCalled ( ) ;
745760 } ) ;
746761
747762 it ( "allows getOrCreate when an existing declaration is returned" , async ( ) => {
0 commit comments