11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22import { AppLifecycleService } from "./service.js" ;
33
4- const { mockApp, mockAgentService , mockTrackAppEvent, mockShutdownPostHog } =
4+ const { mockApp, mockContainer , mockTrackAppEvent, mockShutdownPostHog } =
55 vi . hoisted ( ( ) => ( {
66 mockApp : {
77 exit : vi . fn ( ) ,
88 } ,
9- mockAgentService : {
10- cleanupAll : vi . fn ( ( ) => Promise . resolve ( ) ) ,
9+ mockContainer : {
10+ unbindAll : vi . fn ( ( ) => Promise . resolve ( ) ) ,
1111 } ,
1212 mockTrackAppEvent : vi . fn ( ) ,
1313 mockShutdownPostHog : vi . fn ( ( ) => Promise . resolve ( ) ) ,
@@ -33,10 +33,8 @@ vi.mock("../posthog-analytics.js", () => ({
3333 shutdownPostHog : mockShutdownPostHog ,
3434} ) ) ;
3535
36- vi . mock ( "../../di/tokens.js" , ( ) => ( {
37- MAIN_TOKENS : {
38- AgentService : Symbol . for ( "AgentService" ) ,
39- } ,
36+ vi . mock ( "../../di/container.js" , ( ) => ( {
37+ container : mockContainer ,
4038} ) ) ;
4139
4240vi . mock ( "../../../types/analytics.js" , ( ) => ( {
@@ -50,11 +48,7 @@ describe("AppLifecycleService", () => {
5048
5149 beforeEach ( ( ) => {
5250 vi . clearAllMocks ( ) ;
53-
5451 service = new AppLifecycleService ( ) ;
55- (
56- service as unknown as { agentService : typeof mockAgentService }
57- ) . agentService = mockAgentService ;
5852 } ) ;
5953
6054 describe ( "isQuittingForUpdate" , ( ) => {
@@ -69,9 +63,9 @@ describe("AppLifecycleService", () => {
6963 } ) ;
7064
7165 describe ( "shutdown" , ( ) => {
72- it ( "cleans up agents " , async ( ) => {
66+ it ( "unbinds all container services " , async ( ) => {
7367 await service . shutdown ( ) ;
74- expect ( mockAgentService . cleanupAll ) . toHaveBeenCalled ( ) ;
68+ expect ( mockContainer . unbindAll ) . toHaveBeenCalled ( ) ;
7569 } ) ;
7670
7771 it ( "tracks app quit event" , async ( ) => {
@@ -87,8 +81,8 @@ describe("AppLifecycleService", () => {
8781 it ( "calls cleanup steps in order" , async ( ) => {
8882 const callOrder : string [ ] = [ ] ;
8983
90- mockAgentService . cleanupAll . mockImplementation ( async ( ) => {
91- callOrder . push ( "cleanupAll " ) ;
84+ mockContainer . unbindAll . mockImplementation ( async ( ) => {
85+ callOrder . push ( "unbindAll " ) ;
9286 } ) ;
9387 mockTrackAppEvent . mockImplementation ( ( ) => {
9488 callOrder . push ( "trackAppEvent" ) ;
@@ -100,16 +94,14 @@ describe("AppLifecycleService", () => {
10094 await service . shutdown ( ) ;
10195
10296 expect ( callOrder ) . toEqual ( [
103- "cleanupAll " ,
97+ "unbindAll " ,
10498 "trackAppEvent" ,
10599 "shutdownPostHog" ,
106100 ] ) ;
107101 } ) ;
108102
109- it ( "continues shutdown if agent cleanup fails" , async ( ) => {
110- mockAgentService . cleanupAll . mockRejectedValue (
111- new Error ( "cleanup failed" ) ,
112- ) ;
103+ it ( "continues shutdown if container unbind fails" , async ( ) => {
104+ mockContainer . unbindAll . mockRejectedValue ( new Error ( "unbind failed" ) ) ;
113105
114106 await service . shutdown ( ) ;
115107
@@ -120,7 +112,6 @@ describe("AppLifecycleService", () => {
120112 it ( "continues shutdown if PostHog shutdown fails" , async ( ) => {
121113 mockShutdownPostHog . mockRejectedValue ( new Error ( "posthog failed" ) ) ;
122114
123- // Should not throw
124115 await expect ( service . shutdown ( ) ) . resolves . toBeUndefined ( ) ;
125116 } ) ;
126117 } ) ;
@@ -129,16 +120,16 @@ describe("AppLifecycleService", () => {
129120 it ( "calls shutdown before exit" , async ( ) => {
130121 const callOrder : string [ ] = [ ] ;
131122
132- mockAgentService . cleanupAll . mockImplementation ( async ( ) => {
133- callOrder . push ( "cleanupAll " ) ;
123+ mockContainer . unbindAll . mockImplementation ( async ( ) => {
124+ callOrder . push ( "unbindAll " ) ;
134125 } ) ;
135126 mockApp . exit . mockImplementation ( ( ) => {
136127 callOrder . push ( "exit" ) ;
137128 } ) ;
138129
139130 await service . shutdownAndExit ( ) ;
140131
141- expect ( callOrder [ 0 ] ) . toBe ( "cleanupAll " ) ;
132+ expect ( callOrder [ 0 ] ) . toBe ( "unbindAll " ) ;
142133 expect ( callOrder [ callOrder . length - 1 ] ) . toBe ( "exit" ) ;
143134 } ) ;
144135
0 commit comments