Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions apps/web/src/core/connections/transports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { TransportWebSerial } from "@meshtastic/transport-web-serial";
import { Result } from "better-result";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { openTransport } from "./transports.ts";

vi.mock("@meshtastic/transport-web-serial", () => ({
SerialConnectError: class SerialConnectError extends Error {},
TransportWebSerial: { createFromPort: vi.fn() },
}));

describe("openTransport serial selection", () => {
const info = { usbVendorId: 0x303a, usbProductId: 0x1001 };
const portA = { getInfo: () => info } as SerialPort;
const portB = { getInfo: () => info } as SerialPort;
const selectedPort = { getInfo: () => info } as SerialPort;
const requestPort = vi.fn();

beforeEach(() => {
vi.clearAllMocks();
requestPort.mockResolvedValue(selectedPort);
Object.defineProperty(navigator, "serial", {
configurable: true,
value: {
getPorts: vi.fn().mockResolvedValue([portA, portB]),
requestPort,
},
});
vi.mocked(TransportWebSerial.createFromPort).mockResolvedValue(
Result.ok({} as never),
);
});

it("prompts when multiple permitted ports share the saved VID/PID", async () => {
await openTransport(
{
id: 1,
name: "Serial: 303a:1001",
type: "serial",
status: "disconnected",
usbVendorId: info.usbVendorId,
usbProductId: info.usbProductId,
},
{ allowPrompt: true },
);

expect(requestPort).toHaveBeenCalledOnce();
expect(requestPort).toHaveBeenCalledWith({
filters: [
{
usbVendorId: info.usbVendorId,
usbProductId: info.usbProductId,
},
],
});
expect(TransportWebSerial.createFromPort).toHaveBeenCalledWith(
selectedPort,
);
});
});
26 changes: 24 additions & 2 deletions apps/web/src/core/connections/transports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,24 @@ async function openSerial(
};
}
).serial;
const requestPortOptions =
conn.usbVendorId !== undefined && conn.usbProductId !== undefined
? {
filters: [
{
usbVendorId: conn.usbVendorId,
usbProductId: conn.usbProductId,
},
],
}
: {};

let port = opts.cachedSerialPort;
if (!port) {
const ports = await serial.getPorts();
log.debug("openSerial: getPorts", { count: ports.length });
if (ports && conn.usbVendorId && conn.usbProductId) {
port = ports.find((p: SerialPort) => {
const matchingPorts = ports.filter((p: SerialPort) => {
const info =
(
p as SerialPort & {
Expand All @@ -172,11 +183,22 @@ async function openSerial(
info.usbProductId === conn.usbProductId
);
});
if (matchingPorts.length === 1) {
port = matchingPorts[0];
} else if (matchingPorts.length > 1) {
log.info(
"openSerial: multiple permitted ports share VID/PID; user selection required",
{ count: matchingPorts.length },
);
if (opts.allowPrompt) {
port = await serial.requestPort(requestPortOptions);
}
}
}
}
if (!port && opts.allowPrompt) {
log.debug("openSerial: requesting port via picker");
port = await serial.requestPort({});
port = await serial.requestPort(requestPortOptions);
}
if (!port) {
log.warn("openSerial: no port resolved");
Expand Down
Loading