1
1
import type {
2
- CreateActorInput ,
3
- CreateActorOutput ,
4
- GetActorOutput ,
5
2
GetForIdInput ,
6
3
GetWithKeyInput ,
4
+ GetOrCreateWithKeyInput ,
7
5
ManagerDriver ,
6
+ CreateInput ,
8
7
} from "@/driver-helpers/mod" ;
9
8
import { ActorAlreadyExists } from "@/actor/errors" ;
10
9
import type { TestGlobalState } from "./global-state" ;
11
10
import * as crypto from "node:crypto" ;
12
11
import { ManagerInspector } from "@/inspector/manager" ;
13
12
import type { ActorCoreApp } from "@/app/mod" ;
13
+ import { ActorOutput } from "@/manager/driver" ;
14
14
15
15
export class TestManagerDriver implements ManagerDriver {
16
16
#state: TestGlobalState ;
@@ -30,9 +30,7 @@ export class TestManagerDriver implements ManagerDriver {
30
30
this . #state = state ;
31
31
}
32
32
33
- async getForId ( {
34
- actorId,
35
- } : GetForIdInput ) : Promise < GetActorOutput | undefined > {
33
+ async getForId ( { actorId } : GetForIdInput ) : Promise < ActorOutput | undefined > {
36
34
// Validate the actor exists
37
35
const actor = this . #state. getActor ( actorId ) ;
38
36
if ( ! actor ) {
@@ -49,7 +47,7 @@ export class TestManagerDriver implements ManagerDriver {
49
47
async getWithKey ( {
50
48
name,
51
49
key,
52
- } : GetWithKeyInput ) : Promise < GetActorOutput | undefined > {
50
+ } : GetWithKeyInput ) : Promise < ActorOutput | undefined > {
53
51
// NOTE: This is a slow implementation that checks each actor individually.
54
52
// This can be optimized with an index in the future.
55
53
@@ -115,10 +113,18 @@ export class TestManagerDriver implements ManagerDriver {
115
113
return undefined ;
116
114
}
117
115
118
- async createActor ( {
119
- name,
120
- key,
121
- } : CreateActorInput ) : Promise < CreateActorOutput > {
116
+ async getOrCreateWithKey (
117
+ input : GetOrCreateWithKeyInput ,
118
+ ) : Promise < ActorOutput > {
119
+ const getOutput = await this . getWithKey ( input ) ;
120
+ if ( getOutput ) {
121
+ return getOutput ;
122
+ } else {
123
+ return await this . createActor ( input ) ;
124
+ }
125
+ }
126
+
127
+ async createActor ( { name, key } : CreateInput ) : Promise < ActorOutput > {
122
128
// Check if actor with the same name and key already exists
123
129
const existingActor = await this . getWithKey ( { name, key } ) ;
124
130
if ( existingActor ) {
@@ -132,6 +138,8 @@ export class TestManagerDriver implements ManagerDriver {
132
138
133
139
return {
134
140
actorId,
141
+ name,
142
+ key,
135
143
} ;
136
144
}
137
145
}
0 commit comments