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
51 changes: 51 additions & 0 deletions apps/web/src/core/connections/transports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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(TransportWebSerial.createFromPort).toHaveBeenCalledWith(
selectedPort,
);
});
});
13 changes: 12 additions & 1 deletion apps/web/src/core/connections/transports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async function openSerial(
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,6 +172,17 @@ 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({});
Comment on lines +182 to +183

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf '\n## transports.ts\n'
sed -n '1,260p' apps/web/src/core/connections/transports.ts

printf '\n## transports.test.ts\n'
sed -n '1,220p' apps/web/src/core/connections/transports.test.ts

printf '\n## search for requestPort usage\n'
rg -n "requestPort\\(" apps/web/src/core/connections -S

Repository: meshtastic/web

Length of output: 10620


Pass the saved VID/PID into the serial picker.
requestPort({}) can surface unrelated devices here; pass filters: [{ usbVendorId, usbProductId }] and assert that argument in the serial test.

📍 Affects 2 files
  • apps/web/src/core/connections/transports.ts#L182-L183 (this comment)
  • apps/web/src/core/connections/transports.test.ts#L46-L49
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/web/src/core/connections/transports.ts` around lines 182 - 183, Update
the allowPrompt branch in transports.ts to pass the saved usbVendorId and
usbProductId to serial.requestPort via its filters option. In
transports.test.ts, update the serial picker assertion to verify requestPort
receives the corresponding VID/PID filter.

}
}
}
}
if (!port && opts.allowPrompt) {
Expand Down
Loading