react-native-web-serial-api 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +188 -117
- package/TESTING.md +417 -176
- package/android/build.gradle +14 -0
- package/android/src/main/java/dev/webserialapi/NativeUsbSerialModule.java +74 -11
- package/android/src/main/java/dev/webserialapi/PortPickerActivity.java +61 -59
- package/bin/expose-serial.js +205 -0
- package/lib/commonjs/UsbSerial.js +1 -1
- package/lib/commonjs/WebSerial.js +110 -26
- package/lib/commonjs/WebSerial.js.map +1 -1
- package/lib/commonjs/index.js +2 -2
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/lib/event-target.js +3 -1
- package/lib/commonjs/lib/event-target.js.map +1 -1
- package/lib/commonjs/lib/web-streams.js +42 -0
- package/lib/commonjs/lib/web-streams.js.map +1 -0
- package/lib/commonjs/testing/device-fixture.js +70 -0
- package/lib/commonjs/testing/device-fixture.js.map +1 -0
- package/lib/commonjs/testing/expose.js +91 -0
- package/lib/commonjs/testing/expose.js.map +1 -0
- package/lib/commonjs/testing/harness.js +98 -0
- package/lib/commonjs/testing/harness.js.map +1 -0
- package/lib/commonjs/testing/{virtual-serial.js → in-memory-serial-transport.js} +66 -28
- package/lib/commonjs/testing/in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/index.js +100 -17
- package/lib/commonjs/testing/index.js.map +1 -1
- package/lib/commonjs/testing/install-in-memory-serial-transport.js +54 -0
- package/lib/commonjs/testing/install-in-memory-serial-transport.js.map +1 -0
- package/lib/commonjs/testing/serial-client.js +277 -0
- package/lib/commonjs/testing/serial-client.js.map +1 -0
- package/lib/commonjs/testing/{serial-device.js → simulated-device.js} +17 -17
- package/lib/commonjs/testing/simulated-device.js.map +1 -0
- package/lib/commonjs/testing/test-suite.js +142 -0
- package/lib/commonjs/testing/test-suite.js.map +1 -0
- package/lib/commonjs/transport.js +3 -3
- package/lib/commonjs/websocket/WebSocketSerialTransport.js +659 -0
- package/lib/commonjs/websocket/WebSocketSerialTransport.js.map +1 -0
- package/lib/commonjs/websocket/bridge.js +234 -0
- package/lib/commonjs/websocket/bridge.js.map +1 -0
- package/lib/commonjs/websocket/index.js +33 -0
- package/lib/commonjs/websocket/index.js.map +1 -0
- package/lib/commonjs/websocket/protocol.js +55 -0
- package/lib/commonjs/websocket/protocol.js.map +1 -0
- package/lib/commonjs/websocket/serial-device-bridge.js +130 -0
- package/lib/commonjs/websocket/serial-device-bridge.js.map +1 -0
- package/lib/typescript/src/UsbSerial.d.ts +1 -1
- package/lib/typescript/src/WebSerial.d.ts +7 -7
- package/lib/typescript/src/WebSerial.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/lib/event-target.d.ts +2 -0
- package/lib/typescript/src/lib/event-target.d.ts.map +1 -1
- package/lib/typescript/src/lib/web-streams.d.ts +9 -0
- package/lib/typescript/src/lib/web-streams.d.ts.map +1 -0
- package/lib/typescript/src/testing/device-fixture.d.ts +70 -0
- package/lib/typescript/src/testing/device-fixture.d.ts.map +1 -0
- package/lib/typescript/src/testing/expose.d.ts +71 -0
- package/lib/typescript/src/testing/expose.d.ts.map +1 -0
- package/lib/typescript/src/testing/harness.d.ts +34 -0
- package/lib/typescript/src/testing/harness.d.ts.map +1 -0
- package/lib/typescript/src/testing/{virtual-serial.d.ts → in-memory-serial-transport.d.ts} +37 -26
- package/lib/typescript/src/testing/in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/index.d.ts +18 -8
- package/lib/typescript/src/testing/index.d.ts.map +1 -1
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts +25 -0
- package/lib/typescript/src/testing/install-in-memory-serial-transport.d.ts.map +1 -0
- package/lib/typescript/src/testing/serial-client.d.ts +62 -0
- package/lib/typescript/src/testing/serial-client.d.ts.map +1 -0
- package/lib/typescript/src/testing/{serial-device.d.ts → simulated-device.d.ts} +23 -23
- package/lib/typescript/src/testing/simulated-device.d.ts.map +1 -0
- package/lib/typescript/src/testing/test-suite.d.ts +75 -0
- package/lib/typescript/src/testing/test-suite.d.ts.map +1 -0
- package/lib/typescript/src/transport.d.ts +3 -3
- package/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts +111 -0
- package/lib/typescript/src/websocket/WebSocketSerialTransport.d.ts.map +1 -0
- package/lib/typescript/src/websocket/bridge.d.ts +66 -0
- package/lib/typescript/src/websocket/bridge.d.ts.map +1 -0
- package/lib/typescript/src/websocket/index.d.ts +19 -0
- package/lib/typescript/src/websocket/index.d.ts.map +1 -0
- package/lib/typescript/src/websocket/protocol.d.ts +64 -0
- package/lib/typescript/src/websocket/protocol.d.ts.map +1 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts +32 -0
- package/lib/typescript/src/websocket/serial-device-bridge.d.ts.map +1 -0
- package/package.json +21 -3
- package/src/UsbSerial.ts +1 -1
- package/src/WebSerial.ts +134 -35
- package/src/index.ts +4 -1
- package/src/lib/event-target.ts +12 -0
- package/src/lib/web-streams.ts +43 -0
- package/src/testing/device-fixture.ts +150 -0
- package/src/testing/expose.ts +147 -0
- package/src/testing/harness.ts +124 -0
- package/src/testing/{virtual-serial.ts → in-memory-serial-transport.ts} +95 -56
- package/src/testing/index.ts +69 -21
- package/src/testing/install-in-memory-serial-transport.ts +65 -0
- package/src/testing/serial-client.ts +313 -0
- package/src/testing/{serial-device.ts → simulated-device.ts} +23 -23
- package/src/testing/test-suite.ts +186 -0
- package/src/transport.ts +3 -3
- package/src/websocket/WebSocketSerialTransport.ts +796 -0
- package/src/websocket/bridge.ts +299 -0
- package/src/websocket/index.ts +38 -0
- package/src/websocket/protocol.ts +101 -0
- package/src/websocket/serial-device-bridge.ts +160 -0
- package/lib/commonjs/testing/install.js +0 -54
- package/lib/commonjs/testing/install.js.map +0 -1
- package/lib/commonjs/testing/serial-device.js.map +0 -1
- package/lib/commonjs/testing/virtual-serial.js.map +0 -1
- package/lib/typescript/src/testing/install.d.ts +0 -25
- package/lib/typescript/src/testing/install.d.ts.map +0 -1
- package/lib/typescript/src/testing/serial-device.d.ts.map +0 -1
- package/lib/typescript/src/testing/virtual-serial.d.ts.map +0 -1
- package/src/testing/install.ts +0 -65
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A runtime-agnostic suite runner for serial device tests. Write a set of
|
|
3
|
+
* `SerialTest`s once and run them anywhere a {@link SerialPort} exists:
|
|
4
|
+
*
|
|
5
|
+
* - in Jest against a {@link DeviceHandle} simulator (in-memory),
|
|
6
|
+
* - on a device / emulator over a real or WebSocket-backed port, and
|
|
7
|
+
* - against BOTH, then {@link compareTestResults} to prove the device matches
|
|
8
|
+
* the simulator case-for-case.
|
|
9
|
+
*
|
|
10
|
+
* Each test receives an opened, host-side {@link SerialClient}; by default one
|
|
11
|
+
* client is shared across the whole suite (open once, close at the end), which
|
|
12
|
+
* matches how a request/response protocol session behaves. Pass `shared: false`
|
|
13
|
+
* to open and close a fresh client per test. The runner never throws — it
|
|
14
|
+
* collects a {@link SerialTestResult} per case — so it is safe to drive an
|
|
15
|
+
* on-device Self-Test screen.
|
|
16
|
+
*
|
|
17
|
+
* @example One suite, two transports, compared
|
|
18
|
+
* const sim = await runTestSuite(myTests, await virtualPort());
|
|
19
|
+
* const dev = await runTestSuite(myTests, realPort);
|
|
20
|
+
* const rows = compareTestResults(sim, dev); // a row passes when both agree
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import type {SerialOptions, SerialPort} from '../WebSerial';
|
|
24
|
+
import {errorMessage} from './harness';
|
|
25
|
+
import {SerialClient} from './serial-client';
|
|
26
|
+
|
|
27
|
+
export type SerialTestResult = {
|
|
28
|
+
name: string;
|
|
29
|
+
passed: boolean;
|
|
30
|
+
error?: string;
|
|
31
|
+
durationMs: number;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** One test case. `run` receives an already-opened client (typically per the
|
|
35
|
+
* suite's {@link TestClient}; `SerialClient` by default). */
|
|
36
|
+
export type SerialTest<C = SerialClient> = {
|
|
37
|
+
name: string;
|
|
38
|
+
run(client: C): Promise<void>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Progress hooks so a UI can render results live as each test completes. */
|
|
42
|
+
export type SerialTestProgress = {
|
|
43
|
+
/** Called just before a test starts running. */
|
|
44
|
+
onStart?: (name: string, index: number, total: number) => void;
|
|
45
|
+
/** Called after each test completes (pass or fail). */
|
|
46
|
+
onResult?: (result: SerialTestResult) => void;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* How to build (and tear down) the per-suite client from a port. Provide this to
|
|
51
|
+
* run a higher-level protocol client (e.g. an HCI / NMEA framer built on a
|
|
52
|
+
* {@link SerialClient}) instead of the raw client. `connect` must also open the
|
|
53
|
+
* port; `disconnect` must release it (a `SerialClient.close()` does both).
|
|
54
|
+
*/
|
|
55
|
+
export type TestClient<C> = {
|
|
56
|
+
connect(port: SerialPort): Promise<C>;
|
|
57
|
+
disconnect(client: C): Promise<void>;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export type RunTestSuiteOptions<C = SerialClient> = {
|
|
61
|
+
/** `SerialOptions` for the default client's `open()`. Default {baudRate: 115200}. */
|
|
62
|
+
open?: SerialOptions;
|
|
63
|
+
/** Open/close a fresh client per test instead of sharing one. Default false. */
|
|
64
|
+
shared?: boolean;
|
|
65
|
+
/** Build a protocol client over the port (defaults to an opened SerialClient). */
|
|
66
|
+
client?: TestClient<C>;
|
|
67
|
+
progress?: SerialTestProgress;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
function defaultClientFactory(open?: SerialOptions): TestClient<SerialClient> {
|
|
71
|
+
return {
|
|
72
|
+
async connect(port) {
|
|
73
|
+
const client = new SerialClient(port);
|
|
74
|
+
await client.open(open);
|
|
75
|
+
return client;
|
|
76
|
+
},
|
|
77
|
+
disconnect: client => client.close(),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Run `tests` against an already-acquired `port`, collecting one result per
|
|
83
|
+
* case. With the default (shared) client the port is opened once and closed at
|
|
84
|
+
* the end; with `shared: false` each test gets a fresh open/close. Never throws.
|
|
85
|
+
*/
|
|
86
|
+
export async function runTestSuite<C = SerialClient>(
|
|
87
|
+
tests: SerialTest<C>[],
|
|
88
|
+
port: SerialPort,
|
|
89
|
+
options: RunTestSuiteOptions<C> = {},
|
|
90
|
+
): Promise<SerialTestResult[]> {
|
|
91
|
+
const factory = (options.client ??
|
|
92
|
+
defaultClientFactory(options.open)) as TestClient<C>;
|
|
93
|
+
const shared = options.shared ?? true;
|
|
94
|
+
const progress = options.progress;
|
|
95
|
+
const results: SerialTestResult[] = [];
|
|
96
|
+
|
|
97
|
+
let sharedClient: C | null = null;
|
|
98
|
+
if (shared) {
|
|
99
|
+
try {
|
|
100
|
+
sharedClient = await factory.connect(port);
|
|
101
|
+
} catch (e) {
|
|
102
|
+
const result: SerialTestResult = {
|
|
103
|
+
name: 'open serial port',
|
|
104
|
+
passed: false,
|
|
105
|
+
error: errorMessage(e),
|
|
106
|
+
durationMs: 0,
|
|
107
|
+
};
|
|
108
|
+
progress?.onResult?.(result);
|
|
109
|
+
return [result];
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const total = tests.length;
|
|
114
|
+
try {
|
|
115
|
+
for (let i = 0; i < total; i++) {
|
|
116
|
+
const test = tests[i];
|
|
117
|
+
progress?.onStart?.(test.name, i, total);
|
|
118
|
+
const start = Date.now();
|
|
119
|
+
let perTest: C | null = null;
|
|
120
|
+
let result: SerialTestResult;
|
|
121
|
+
try {
|
|
122
|
+
if (!shared) perTest = await factory.connect(port);
|
|
123
|
+
const client = shared ? (sharedClient as C) : (perTest as C);
|
|
124
|
+
await test.run(client);
|
|
125
|
+
result = {
|
|
126
|
+
name: test.name,
|
|
127
|
+
passed: true,
|
|
128
|
+
durationMs: Date.now() - start,
|
|
129
|
+
};
|
|
130
|
+
} catch (e) {
|
|
131
|
+
result = {
|
|
132
|
+
name: test.name,
|
|
133
|
+
passed: false,
|
|
134
|
+
error: errorMessage(e),
|
|
135
|
+
durationMs: Date.now() - start,
|
|
136
|
+
};
|
|
137
|
+
} finally {
|
|
138
|
+
if (perTest) await factory.disconnect(perTest).catch(() => {});
|
|
139
|
+
}
|
|
140
|
+
results.push(result);
|
|
141
|
+
progress?.onResult?.(result);
|
|
142
|
+
}
|
|
143
|
+
} finally {
|
|
144
|
+
if (sharedClient) await factory.disconnect(sharedClient).catch(() => {});
|
|
145
|
+
}
|
|
146
|
+
return results;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Compare two runs of the same suite case-by-case. A row passes when both runs
|
|
151
|
+
* agree (both passed or both failed), so the `candidate` is judged equivalent to
|
|
152
|
+
* the `reference` rather than judged on its own. Cases the candidate produced
|
|
153
|
+
* that the reference never ran are surfaced as failing `candidate: …` rows.
|
|
154
|
+
*/
|
|
155
|
+
export function compareTestResults(
|
|
156
|
+
reference: SerialTestResult[],
|
|
157
|
+
candidate: SerialTestResult[],
|
|
158
|
+
): SerialTestResult[] {
|
|
159
|
+
const candidateByName = new Map(candidate.map(r => [r.name, r]));
|
|
160
|
+
|
|
161
|
+
const candidateOnly = candidate
|
|
162
|
+
.filter(r => !reference.some(s => s.name === r.name))
|
|
163
|
+
.map(r => ({
|
|
164
|
+
name: `candidate: ${r.name}`,
|
|
165
|
+
passed: false,
|
|
166
|
+
error: r.error ?? 'candidate produced an unexpected result',
|
|
167
|
+
durationMs: r.durationMs,
|
|
168
|
+
}));
|
|
169
|
+
|
|
170
|
+
const compared = reference.map(s => {
|
|
171
|
+
const r = candidateByName.get(s.name);
|
|
172
|
+
const identical = !!r && r.passed === s.passed;
|
|
173
|
+
return {
|
|
174
|
+
name: s.name,
|
|
175
|
+
passed: identical,
|
|
176
|
+
error: identical
|
|
177
|
+
? undefined
|
|
178
|
+
: `reference ${s.passed ? 'passed' : 'failed'}, candidate ${
|
|
179
|
+
r ? (r.passed ? 'passed' : 'failed') : 'did not respond'
|
|
180
|
+
}${r?.error ? `: ${r.error}` : ''}`,
|
|
181
|
+
durationMs: r?.durationMs ?? 0,
|
|
182
|
+
};
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
return [...candidateOnly, ...compared];
|
|
186
|
+
}
|
package/src/transport.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Hardware-transport
|
|
2
|
+
* Hardware-transport abstraction for the Web Serial polyfill.
|
|
3
3
|
*
|
|
4
4
|
* `SerialTransport` is the single interface that the `Serial`/`SerialPort`
|
|
5
5
|
* classes depend on to talk to "the device". The production implementation
|
|
6
6
|
* (`UsbSerialModule`, backed by the `NativeUsbSerial` TurboModule — see
|
|
7
7
|
* {@link ./UsbSerial}) and the in-memory test/dev double
|
|
8
|
-
* (`
|
|
8
|
+
* (`InMemorySerialTransport` — see {@link ./testing/in-memory-serial-transport}) both
|
|
9
9
|
* implement it.
|
|
10
10
|
*
|
|
11
11
|
* This module is intentionally free of any `react-native` import. That is what
|
|
@@ -115,7 +115,7 @@ export type Subscription = {remove: () => void};
|
|
|
115
115
|
* The contract every serial transport must satisfy. It mirrors the JS-friendly
|
|
116
116
|
* surface of `UsbSerialModule` exactly, so `UsbSerialModule implements
|
|
117
117
|
* SerialTransport` is a faithful 1:1 and any conforming double (e.g.
|
|
118
|
-
* `
|
|
118
|
+
* `InMemorySerialTransport`) is a drop-in replacement.
|
|
119
119
|
*
|
|
120
120
|
* Ports are addressed by the pair `(deviceId, portNumber)`. Inbound bytes,
|
|
121
121
|
* read errors and device attach/detach arrive through the `on*` subscriptions.
|