Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

microphonePriority.test.ts

79 lines2.8 KB

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