| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | type MicrophoneEntry, |
| 5 | microphoneEntryKey, |
| 6 | microphoneOrder, |
| 7 | preferredMicrophoneInput, |
| 8 | rememberMicrophones, |
| 9 | } from "./microphonePriority"; |
| 10 | |
| 11 | const iPhone = { uid: "Built-In Microphone", name: "iPhone Microphone", type: "MicrophoneBuiltIn" }; |
| 12 | const airPods = { uid: "airpods", name: "Julius’s AirPods Pro", type: "BluetoothHFP" }; |
| 13 | const bose = { uid: "bose", name: "Julius’s Bose QC", type: "BluetoothHFP" }; |
| 14 | const civic = { uid: "civic", name: "Honda Civic", type: "BluetoothHFP" }; |
| 15 | const carPlay = { uid: "carplay", name: "CarPlay", type: "CarAudio" }; |
| 16 | const usb = { uid: "usb", name: "Shure MV7", type: "USBAudio" }; |
| 17 | |
| 18 | const keys = (order: ReadonlyArray<MicrophoneEntry>) => order.map(microphoneEntryKey); |
| 19 | |
| 20 | describe("microphoneOrder", () => { |
| 21 | it("lists every kind once, CarPlay below the built-in microphone by default", () => { |
| 22 | expect(keys(microphoneOrder())).toEqual([ |
| 23 | "kind:wired", |
| 24 | "kind:bluetooth", |
| 25 | "kind:builtIn", |
| 26 | "kind:carPlay", |
| 27 | ]); |
| 28 | }); |
| 29 | |
| 30 | it("keeps a saved order and files missing kinds after their devices", () => { |
| 31 | const saved: MicrophoneEntry[] = [ |
| 32 | { kind: "builtIn" }, |
| 33 | { kind: "bluetooth", uid: "airpods", name: "AirPods" }, |
| 34 | { kind: "builtIn" }, |
| 35 | ]; |
| 36 | expect(keys(microphoneOrder(saved))).toEqual([ |
| 37 | "kind:wired", |
| 38 | "kind:builtIn", |
| 39 | "airpods", |
| 40 | "kind:bluetooth", |
| 41 | "kind:carPlay", |
| 42 | ]); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | describe("rememberMicrophones", () => { |
| 47 | it("lists a new device just above its kind and leaves built-in and CarPlay as kinds", () => { |
| 48 | const order = microphoneOrder(); |
| 49 | expect(keys(rememberMicrophones(order, [iPhone, carPlay, airPods]))).toEqual([ |
| 50 | "kind:wired", |
| 51 | "airpods", |
| 52 | "kind:bluetooth", |
| 53 | "kind:builtIn", |
| 54 | "kind:carPlay", |
| 55 | ]); |
| 56 | }); |
| 57 | |
| 58 | it("refreshes renamed devices and returns the same order when nothing changed", () => { |
| 59 | const order = rememberMicrophones(microphoneOrder(), [airPods]); |
| 60 | expect(rememberMicrophones(order, [airPods, iPhone])).toBe(order); |
| 61 | |
| 62 | const renamed = rememberMicrophones(order, [{ ...airPods, name: "AirPods Max" }]); |
| 63 | expect(renamed.find((entry) => microphoneEntryKey(entry) === "airpods")).toEqual({ |
| 64 | kind: "bluetooth", |
| 65 | uid: "airpods", |
| 66 | name: "AirPods Max", |
| 67 | }); |
| 68 | }); |
| 69 | }); |
| 70 | |
| 71 | describe("preferredMicrophoneInput", () => { |
| 72 | // Built-in, Bose, Wired, other Bluetooth, AirPods, CarPlay. |
| 73 | const order: MicrophoneEntry[] = [ |
| 74 | { kind: "builtIn" }, |
| 75 | { kind: "bluetooth", uid: "bose", name: bose.name }, |
| 76 | { kind: "wired" }, |
| 77 | { kind: "bluetooth" }, |
| 78 | { kind: "bluetooth", uid: "airpods", name: airPods.name }, |
| 79 | { kind: "carPlay" }, |
| 80 | ]; |
| 81 | |
| 82 | it("ranks unlisted devices of a kind at that kind, ahead of a device listed below it", () => { |
| 83 | expect(preferredMicrophoneInput([airPods, civic], order)).toBe(civic); |
| 84 | expect(preferredMicrophoneInput([airPods, bose], order)).toBe(bose); |
| 85 | expect(preferredMicrophoneInput([airPods, usb], order)).toBe(usb); |
| 86 | expect(preferredMicrophoneInput([airPods, carPlay], order)).toBe(airPods); |
| 87 | expect(preferredMicrophoneInput([airPods, iPhone], order)).toBe(iPhone); |
| 88 | }); |
| 89 | |
| 90 | it("leaves inputs of unknown kinds to iOS", () => { |
| 91 | expect(preferredMicrophoneInput([{ uid: "x", name: "x", type: "Virtual" }], order)).toBeNull(); |
| 92 | }); |
| 93 | }); |