1
1
import { describe , test , expect , vi } from "vitest" ;
2
- import type { DriverTestConfig , DriverTestConfigWithTransport } from "@/mod" ;
3
- import { setupDriverTest } from "@/utils" ;
4
- import { resolve } from "node:path" ;
5
- import type { App as CounterApp } from "../../fixtures/apps/counter" ;
6
- import type { App as ScheduledApp } from "../../fixtures/apps/scheduled" ;
2
+ import type { DriverTestConfig , DriverTestConfigWithTransport } from "../mod" ;
3
+ import { setupDriverTest } from "../utils" ;
4
+ import {
5
+ COUNTER_APP_PATH ,
6
+ SCHEDULED_APP_PATH ,
7
+ type CounterApp ,
8
+ type ScheduledApp ,
9
+ } from "../test-apps" ;
7
10
8
11
/**
9
12
* Waits for the specified time, using either real setTimeout or vi.advanceTimersByTime
@@ -20,14 +23,16 @@ export async function waitFor(
20
23
return Promise . resolve ( ) ;
21
24
}
22
25
}
23
- export function runActorDriverTests ( driverTestConfig : DriverTestConfigWithTransport ) {
26
+ export function runActorDriverTests (
27
+ driverTestConfig : DriverTestConfigWithTransport ,
28
+ ) {
24
29
describe ( "Actor Driver Tests" , ( ) => {
25
30
describe ( "State Persistence" , ( ) => {
26
31
test ( "persists state between actor instances" , async ( c ) => {
27
32
const { client } = await setupDriverTest < CounterApp > (
28
33
c ,
29
34
driverTestConfig ,
30
- resolve ( __dirname , "../fixtures/apps/counter.ts" ) ,
35
+ COUNTER_APP_PATH ,
31
36
) ;
32
37
33
38
// Create instance and increment
@@ -45,13 +50,13 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
45
50
const { client } = await setupDriverTest < CounterApp > (
46
51
c ,
47
52
driverTestConfig ,
48
- resolve ( __dirname , "../fixtures/apps/counter.ts" ) ,
53
+ COUNTER_APP_PATH ,
49
54
) ;
50
55
51
56
// Create actor and set initial state
52
57
const counterInstance = client . counter . getOrCreate ( ) ;
53
58
await counterInstance . increment ( 5 ) ;
54
-
59
+
55
60
// Reconnect to the same actor
56
61
const reconnectedInstance = client . counter . getOrCreate ( ) ;
57
62
const persistedCount = await reconnectedInstance . increment ( 0 ) ;
@@ -62,17 +67,17 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
62
67
const { client } = await setupDriverTest < CounterApp > (
63
68
c ,
64
69
driverTestConfig ,
65
- resolve ( __dirname , "../fixtures/apps/counter.ts" ) ,
70
+ COUNTER_APP_PATH ,
66
71
) ;
67
72
68
73
// Create first counter with specific key
69
74
const counterA = client . counter . getOrCreate ( [ "counter-a" ] ) ;
70
75
await counterA . increment ( 5 ) ;
71
-
76
+
72
77
// Create second counter with different key
73
78
const counterB = client . counter . getOrCreate ( [ "counter-b" ] ) ;
74
79
await counterB . increment ( 10 ) ;
75
-
80
+
76
81
// Verify state is separate
77
82
const countA = await counterA . increment ( 0 ) ;
78
83
const countB = await counterB . increment ( 0 ) ;
@@ -86,74 +91,59 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
86
91
const { client } = await setupDriverTest < ScheduledApp > (
87
92
c ,
88
93
driverTestConfig ,
89
- resolve ( __dirname , "../fixtures/apps/scheduled.ts" ) ,
94
+ SCHEDULED_APP_PATH ,
90
95
) ;
91
96
92
97
// Create instance
93
98
const alarmInstance = client . scheduled . getOrCreate ( ) ;
94
-
99
+
95
100
// Schedule a task to run in 100ms
96
101
await alarmInstance . scheduleTask ( 100 ) ;
97
-
102
+
98
103
// Wait for longer than the scheduled time
99
104
await waitFor ( driverTestConfig , 150 ) ;
100
-
105
+
101
106
// Verify the scheduled task ran
102
107
const lastRun = await alarmInstance . getLastRun ( ) ;
103
108
const scheduledCount = await alarmInstance . getScheduledCount ( ) ;
104
-
109
+
105
110
expect ( lastRun ) . toBeGreaterThan ( 0 ) ;
106
111
expect ( scheduledCount ) . toBe ( 1 ) ;
107
112
} ) ;
108
113
} ) ;
109
-
114
+
110
115
describe ( "Actor Handle" , ( ) => {
111
116
test ( "stateless handle can perform RPC calls" , async ( c ) => {
112
117
const { client } = await setupDriverTest < CounterApp > (
113
118
c ,
114
119
driverTestConfig ,
115
- resolve ( __dirname , "../fixtures/apps/counter.ts" ) ,
120
+ COUNTER_APP_PATH ,
116
121
) ;
117
-
122
+
118
123
// Get a handle to an actor
119
124
const counterHandle = client . counter . getOrCreate ( "test-handle" ) ;
120
125
await counterHandle . increment ( 1 ) ;
121
126
await counterHandle . increment ( 2 ) ;
122
127
const count = await counterHandle . getCount ( ) ;
123
128
expect ( count ) . toBe ( 3 ) ;
124
129
} ) ;
125
-
130
+
126
131
test ( "stateless handles to same actor share state" , async ( c ) => {
127
132
const { client } = await setupDriverTest < CounterApp > (
128
133
c ,
129
134
driverTestConfig ,
130
- resolve ( __dirname , "../fixtures/apps/counter.ts" ) ,
135
+ COUNTER_APP_PATH ,
131
136
) ;
132
-
137
+
133
138
// Get a handle to an actor
134
139
const handle1 = client . counter . getOrCreate ( "test-handle-shared" ) ;
135
140
await handle1 . increment ( 5 ) ;
136
-
141
+
137
142
// Get another handle to same actor
138
143
const handle2 = client . counter . getOrCreate ( "test-handle-shared" ) ;
139
144
const count = await handle2 . getCount ( ) ;
140
145
expect ( count ) . toBe ( 5 ) ;
141
146
} ) ;
142
-
143
- // TODO: Fix this
144
- //test("create new actor with handle", async (c) => {
145
- // const { client } = await setupDriverTest<CounterApp>(
146
- // c,
147
- // driverTestConfig,
148
- // resolve(__dirname, "../fixtures/apps/counter.ts"),
149
- // );
150
- //
151
- // // Create a new actor with handle
152
- // const createdHandle = client.counter.create("test-handle-create");
153
- // await createdHandle.increment(5);
154
- // const count = await createdHandle.getCount();
155
- // expect(count).toBe(5);
156
- //});
157
147
} ) ;
158
148
} ) ;
159
149
}
0 commit comments