Repository navigation
Expand file tree
/
Copy pathprotocol.ts
More file actions
128 lines (116 loc) · 3.1 KB
/
Copy pathprotocol.ts
File metadata and controls
128 lines (116 loc) · 3.1 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
export const NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION = 1 as const;
export const DEFAULT_NATIVE_SCRIPT_VITEST_PORT = 17_878;
export type NativeScriptTestState =
| 'queued'
| 'running'
| 'passed'
| 'failed'
| 'skipped'
| 'todo';
export interface NativeScriptTestDescriptor {
id: string;
name: string;
fullName: string;
file: string;
state: NativeScriptTestState;
duration?: number;
error?: string;
}
export type NativeScriptTestEvent =
| {
type: 'worker-run-started';
worker: number;
files: string[];
timestamp: number;
}
| {
type: 'tests-collected';
worker: number;
tests: NativeScriptTestDescriptor[];
}
| {
type: 'test-updated';
worker: number;
test: NativeScriptTestDescriptor;
}
| {
type: 'worker-run-finished';
worker: number;
timestamp: number;
}
| {
type: 'worker-error';
worker: number;
message: string;
};
export type NativeScriptTestEventListener = (
event: NativeScriptTestEvent,
) => void;
export interface NativeScriptTestEventSource {
subscribe(listener: NativeScriptTestEventListener): () => void;
}
/**
* Slot layout: when `main` is true, slot 0 executes on the application's main
* thread (required for UI tests) and worker slots occupy 1..workers. When
* `main` is false, worker slots occupy 0..workers-1.
*/
export interface NativeScriptSlotLayout {
main: boolean;
workers: number;
}
export function totalSlots(layout: NativeScriptSlotLayout): number {
return (layout.main ? 1 : 0) + layout.workers;
}
export type NativeScriptVitestWireMessage =
| {
kind: 'hello';
protocol: typeof NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION;
}
| ({
kind: 'configure';
protocol: typeof NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION;
} & NativeScriptSlotLayout)
| {
kind: 'worker-ready';
slot: number;
}
| {
kind: 'worker-message';
slot: number;
frame: string;
}
| {
kind: 'error';
message: string;
};
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}
function hasSlot(value: Record<string, unknown>): boolean {
return Number.isInteger(value.slot) && (value.slot as number) >= 0;
}
export function isNativeScriptVitestWireMessage(
value: unknown,
): value is NativeScriptVitestWireMessage {
if (!isRecord(value) || typeof value.kind !== 'string') return false;
switch (value.kind) {
case 'hello':
return value.protocol === NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION;
case 'configure':
return (
value.protocol === NATIVE_SCRIPT_VITEST_PROTOCOL_VERSION &&
typeof value.main === 'boolean' &&
Number.isInteger(value.workers) &&
(value.workers as number) >= 0 &&
(value.main === true || (value.workers as number) > 0)
);
case 'worker-ready':
return hasSlot(value);
case 'worker-message':
return hasSlot(value) && typeof value.frame === 'string';
case 'error':
return typeof value.message === 'string';
default:
return false;
}
}