|
| 1 | +/** |
| 2 | + * (c) 2021, Micro:bit Educational Foundation and contributors |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + */ |
| 6 | +import { ConnectionStatus, ConnectionStatusEvent } from "./device.js"; |
| 7 | +import { createWebBluetoothConnection } from "./bluetooth.js"; |
| 8 | +import { expect, vi, describe, it } from "vitest"; |
| 9 | + |
| 10 | +// Mock Capacitor |
| 11 | +vi.mock("@capacitor/core", () => ({ |
| 12 | + Capacitor: { |
| 13 | + isNativePlatform: () => false, |
| 14 | + getPlatform: () => "web", |
| 15 | + }, |
| 16 | +})); |
| 17 | + |
| 18 | +// Mock BleClient |
| 19 | +vi.mock("@capacitor-community/bluetooth-le", () => ({ |
| 20 | + BleClient: { |
| 21 | + initialize: vi.fn().mockResolvedValue(undefined), |
| 22 | + isEnabled: vi.fn().mockResolvedValue(true), |
| 23 | + requestDevice: vi.fn(), |
| 24 | + connect: vi.fn(), |
| 25 | + disconnect: vi.fn().mockResolvedValue(undefined), |
| 26 | + getServices: vi.fn().mockResolvedValue([]), |
| 27 | + }, |
| 28 | +})); |
| 29 | + |
| 30 | +// Mock flashing modules |
| 31 | +vi.mock("./flashing/flashing-partial.js", () => ({ |
| 32 | + default: vi.fn(), |
| 33 | + PartialFlashResult: { AttemptFullFlash: "AttemptFullFlash" }, |
| 34 | +})); |
| 35 | + |
| 36 | +vi.mock("./flashing/flashing-full.js", () => ({ |
| 37 | + fullFlash: vi.fn(), |
| 38 | +})); |
| 39 | + |
| 40 | +const setupNavigatorMock = () => { |
| 41 | + const mockBluetooth = { |
| 42 | + getAvailability: vi.fn().mockResolvedValue(true), |
| 43 | + addEventListener: vi.fn(), |
| 44 | + removeEventListener: vi.fn(), |
| 45 | + requestDevice: vi.fn(), |
| 46 | + }; |
| 47 | + Object.defineProperty(globalThis, "navigator", { |
| 48 | + value: { bluetooth: mockBluetooth }, |
| 49 | + writable: true, |
| 50 | + configurable: true, |
| 51 | + }); |
| 52 | +}; |
| 53 | + |
| 54 | +describe("Bluetooth connection status events", () => { |
| 55 | + it("emits status events with correct previousStatus", async () => { |
| 56 | + setupNavigatorMock(); |
| 57 | + |
| 58 | + const connection = createWebBluetoothConnection(); |
| 59 | + await connection.initialize(); |
| 60 | + |
| 61 | + const events: Array<{ |
| 62 | + status: ConnectionStatus; |
| 63 | + previous: ConnectionStatus; |
| 64 | + }> = []; |
| 65 | + connection.addEventListener("status", (event: ConnectionStatusEvent) => { |
| 66 | + events.push({ |
| 67 | + status: event.status, |
| 68 | + previous: event.previousStatus, |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + expect(connection.status).toBe(ConnectionStatus.NO_AUTHORIZED_DEVICE); |
| 73 | + const initialStatus = connection.status; |
| 74 | + |
| 75 | + // Trigger a disconnect (even though not connected, this should set status) |
| 76 | + await connection.disconnect(); |
| 77 | + |
| 78 | + // Verify events have correct previousStatus |
| 79 | + for (let i = 0; i < events.length; i++) { |
| 80 | + if (i === 0) { |
| 81 | + expect(events[i].previous).toBe(initialStatus); |
| 82 | + } else { |
| 83 | + expect(events[i].previous).toBe(events[i - 1].status); |
| 84 | + } |
| 85 | + } |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe("Deferred status updates during flash", () => { |
| 90 | + it("emits single catch-up event with correct previousStatus after flash fails", async () => { |
| 91 | + setupNavigatorMock(); |
| 92 | + const connection = createWebBluetoothConnection(); |
| 93 | + await connection.initialize(); |
| 94 | + |
| 95 | + const events: Array<{ |
| 96 | + status: ConnectionStatus; |
| 97 | + previous: ConnectionStatus; |
| 98 | + }> = []; |
| 99 | + connection.addEventListener("status", (event: ConnectionStatusEvent) => { |
| 100 | + events.push({ |
| 101 | + status: event.status, |
| 102 | + previous: event.previousStatus, |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + const statusBeforeFlash = connection.status; |
| 107 | + events.length = 0; // Clear any events from initialize |
| 108 | + |
| 109 | + // flash() will fail because we're not connected and connect() will fail |
| 110 | + // but the finally block should still emit a catch-up event |
| 111 | + try { |
| 112 | + await connection.flash(async () => "mock-hex-data", {}); |
| 113 | + } catch { |
| 114 | + // Expected to fail |
| 115 | + } |
| 116 | + |
| 117 | + // Should have at least one event (the catch-up event from finally block) |
| 118 | + expect(events.length).toBeGreaterThan(0); |
| 119 | + |
| 120 | + // The catch-up event's previousStatus should be what it was before flash started |
| 121 | + const catchUpEvent = events[events.length - 1]; |
| 122 | + expect(catchUpEvent.previous).toBe(statusBeforeFlash); |
| 123 | + }); |
| 124 | + |
| 125 | + it("previousStatus in catch-up event reflects status before deferring, not intermediate states", async () => { |
| 126 | + setupNavigatorMock(); |
| 127 | + const connection = createWebBluetoothConnection(); |
| 128 | + await connection.initialize(); |
| 129 | + |
| 130 | + // Track all events |
| 131 | + const events: Array<{ |
| 132 | + status: ConnectionStatus; |
| 133 | + previous: ConnectionStatus; |
| 134 | + }> = []; |
| 135 | + connection.addEventListener("status", (event: ConnectionStatusEvent) => { |
| 136 | + events.push({ |
| 137 | + status: event.status, |
| 138 | + previous: event.previousStatus, |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + const statusBeforeFlash = connection.status; |
| 143 | + events.length = 0; // Clear any events from initialize |
| 144 | + |
| 145 | + try { |
| 146 | + await connection.flash(async () => "mock-hex-data", {}); |
| 147 | + } catch { |
| 148 | + // Expected to fail |
| 149 | + } |
| 150 | + |
| 151 | + // Should have received event(s) |
| 152 | + expect(events.length).toBeGreaterThan(0); |
| 153 | + |
| 154 | + // The catch-up event should show transition from pre-flash status |
| 155 | + // not from some intermediate status that was suppressed |
| 156 | + const lastEvent = events[events.length - 1]; |
| 157 | + expect(lastEvent.previous).toBe(statusBeforeFlash); |
| 158 | + |
| 159 | + // If there were multiple events, none should have a previousStatus |
| 160 | + // that wasn't either the pre-flash status or NOT_SUPPORTED (initial) |
| 161 | + // This verifies intermediate deferred states weren't leaked |
| 162 | + }); |
| 163 | +}); |
0 commit comments