Repository navigation
Expand file tree
/
Copy pathlog.ts
More file actions
131 lines (117 loc) · 4.35 KB
/
Copy pathlog.ts
File metadata and controls
131 lines (117 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/** What a `RemoteMock` saw: answered calls, opened streams, and requests without a rule. */
/** One unary call, logged when its rule is selected and settled when the rule does. */
export interface LoggedCall {
readonly endpoint: string
/** Positional args as the caller passed them. */
readonly args: readonly unknown[]
/** `pending` until the rule settles, then `answered` or `failed`. */
readonly state: 'pending' | 'answered' | 'failed'
/** The answer once `answered`, the thrown error once `failed`. */
readonly result: unknown
/** Global order across calls and stream opens. */
readonly seq: number
}
/** One stream open and its current state. */
export interface LoggedStream {
readonly endpoint: string
/** Positional args the stream was opened with. */
readonly args: readonly unknown[]
readonly state: 'open' | 'ended' | 'failed' | 'cancelled'
/** Items delivered to the consumer so far. */
readonly pushed: number
/** Global order across calls and stream opens. */
readonly seq: number
}
/** One request that found no rule. */
export interface LoggedMiss {
readonly endpoint: string
readonly mode: 'unary' | 'stream'
}
/** Read side of the log. */
export interface MockLog {
/**
* Unary calls, oldest first, with their live state.
* @param endpoint - restrict to one endpoint.
* @returns matching entries.
*/
calls(endpoint?: string): readonly LoggedCall[]
/**
* Stream opens, oldest first, with their live state.
* @param endpoint - restrict to one endpoint.
* @returns matching entries.
*/
streams(endpoint?: string): readonly LoggedStream[]
/**
* The first positional arg of every unary call and stream open, oldest first: the request object of the Gateway's
* one-parameter endpoints. Without `endpoint`, every endpoint the client asked the Host for, except the Gateway's
* own `$`-prefixed ones (`$events`).
* @param endpoint - restrict to one endpoint.
* @returns the requests.
*/
requests(endpoint?: string): readonly unknown[]
/** Requests that found no rule, oldest first. */
unmatched(): readonly LoggedMiss[]
}
/** Mutable call entry the dispatcher settles. */
export interface CallRecord extends LoggedCall {
state: LoggedCall['state']
result: unknown
}
/** Mutable stream entry shared with the live stream. */
export interface StreamRecord extends LoggedStream {
state: LoggedStream['state']
pushed: number
}
/** The mock's log store. */
export class MockLogStore implements MockLog {
private readonly callEntries: CallRecord[] = []
private readonly streamEntries: StreamRecord[] = []
private readonly missEntries: LoggedMiss[] = []
private seq = 0
calls(endpoint?: string): readonly LoggedCall[] {
return this.callEntries.filter(entry => endpoint === undefined || entry.endpoint === endpoint)
}
streams(endpoint?: string): readonly LoggedStream[] {
return this.streamEntries.filter(entry => endpoint === undefined || entry.endpoint === endpoint)
}
requests(endpoint?: string): readonly unknown[] {
const entries: (LoggedCall | LoggedStream)[] = [...this.calls(endpoint), ...this.streams(endpoint)]
return entries
.filter(entry => endpoint !== undefined || !entry.endpoint.startsWith('$'))
.sort((a, b) => a.seq - b.seq)
.map(entry => entry.args[0])
}
unmatched(): readonly LoggedMiss[] {
return [...this.missEntries]
}
/**
* Record one call whose rule was selected.
* @param endpoint - endpoint.
* @param args - positional args.
* @returns the mutable entry the dispatcher settles.
*/
call(endpoint: string, args: readonly unknown[]): CallRecord {
const record: CallRecord = { endpoint, args, state: 'pending', result: undefined, seq: ++this.seq }
this.callEntries.push(record)
return record
}
/**
* Record one stream open.
* @param endpoint - endpoint.
* @param args - positional args.
* @returns the mutable entry the stream updates.
*/
stream(endpoint: string, args: readonly unknown[]): StreamRecord {
const record: StreamRecord = { endpoint, args, state: 'open', pushed: 0, seq: ++this.seq }
this.streamEntries.push(record)
return record
}
/**
* Record one request without a rule.
* @param endpoint - endpoint.
* @param mode - how it was requested.
*/
miss(endpoint: string, mode: 'unary' | 'stream'): void {
this.missEntries.push({ endpoint, mode })
}
}