Repository navigation
Expand file tree
/
Copy pathlib_test.ts
More file actions
218 lines (210 loc) · 7.83 KB
/
Copy pathlib_test.ts
File metadata and controls
218 lines (210 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { assert, assertEquals } from "@std/assert";
import {
busValue,
deviceUses,
mergeUses,
parseFuser,
parseGeoclueClients,
parsePs,
parsePwDump,
parseSourceOutputs,
prettyApp,
since,
subtitle,
type Use,
} from "./lib.ts";
// pw-dump of PipeWire 1.0, trimmed: Firefox uses the camera and the microphone, cava reads the
// speakers' monitor (not the mic), OBS records a screencast from xdg-desktop-portal-wlr.
const PW = JSON.stringify([
{ id: 0, type: "PipeWire:Interface:Core", info: { name: "pipewire-0" } },
{
id: 33,
type: "PipeWire:Interface:Client",
info: { props: { "application.name": "WirePlumber", "application.process.binary": "wireplumber" } },
},
{
id: 75,
type: "PipeWire:Interface:Client",
info: {
props: {
"application.name": "Firefox",
"application.process.binary": "firefox",
"application.process.id": 4242,
"pipewire.access.portal.app_id": "org.mozilla.firefox",
},
},
},
{
id: 90,
type: "PipeWire:Interface:Client",
info: { props: { "application.name": "xdg-desktop-portal-wlr", "application.process.id": 1500 } },
},
{
id: 91,
type: "PipeWire:Interface:Client",
info: {
props: { "application.name": "OBS Studio", "application.process.binary": "obs", "application.process.id": 5100 },
},
},
{
id: 45,
type: "PipeWire:Interface:Node",
info: {
state: "running",
props: {
"media.class": "Video/Source",
"node.name": "v4l2_input.pci-0000_00_14.0-usb-0_6_1.0",
"node.description": "Integrated Camera (V4L2)",
"device.api": "v4l2",
"client.id": 33,
},
},
},
{
id: 56,
type: "PipeWire:Interface:Node",
info: { props: { "media.class": "Audio/Source", "node.description": "Built-in Audio Analog Stereo" } },
},
{
id: 57,
type: "PipeWire:Interface:Node",
info: { props: { "media.class": "Audio/Sink", "node.description": "Speakers" } },
},
{
id: 79,
type: "PipeWire:Interface:Node",
info: { state: "running", props: { "media.class": "Stream/Input/Video", "client.id": 75 } },
},
{
id: 82,
type: "PipeWire:Interface:Node",
info: { state: "running", props: { "media.class": "Stream/Input/Audio", "client.id": 75 } },
},
{
id: 83,
type: "PipeWire:Interface:Node",
info: { state: "running", props: { "media.class": "Stream/Input/Audio", "application.name": "cava" } },
},
{
id: 95,
type: "PipeWire:Interface:Node",
info: { state: "running", props: { "media.class": "Video/Source", "node.name": "xdpw_stream", "client.id": 90 } },
},
{
id: 96,
type: "PipeWire:Interface:Node",
info: { state: "running", props: { "media.class": "Stream/Input/Video", "node.name": "OBS", "client.id": 91 } },
},
{ id: 100, type: "PipeWire:Interface:Link", info: { "output-node-id": 45, "input-node-id": 79, state: "active" } },
{ id: 101, type: "PipeWire:Interface:Link", info: { "output-node-id": 56, "input-node-id": 82, state: "active" } },
{ id: 102, type: "PipeWire:Interface:Link", info: { "output-node-id": 57, "input-node-id": 83, state: "active" } },
{ id: 103, type: "PipeWire:Interface:Link", info: { "output-node-id": 95, "input-node-id": 96, state: "active" } },
]);
Deno.test("parsePwDump finds camera, microphone and screen users", () => {
const uses = parsePwDump(PW);
assertEquals(uses.map((u) => [u.kind, u.app, u.pid, u.device]), [
["camera", "Firefox", 4242, "Integrated Camera (V4L2)"],
["screen", "OBS Studio", 5100, "xdpw_stream"],
["microphone", "Firefox", 4242, "Built-in Audio Analog Stereo"],
]);
assertEquals(uses[0].appId, "org.mozilla.firefox");
assertEquals(parsePwDump("not json"), []);
});
Deno.test("parsePwDump shows a running screencast even without a viewer", () => {
const dump = JSON.parse(PW).filter((o: { id: number }) => o.id !== 103);
const uses = parsePwDump(JSON.stringify(dump));
const screen = uses.find((u) => u.kind === "screen")!;
assertEquals([screen.app, screen.pid], ["xdg-desktop-portal-wlr", 1500]);
});
Deno.test("parseSourceOutputs keeps mic recordings, drops monitors and level meters", () => {
const outputs = JSON.stringify([
{
index: 42,
source: 56,
corked: false,
mute: false,
properties: {
"application.name": "Firefox",
"application.process.id": "4242",
"application.process.binary": "firefox",
},
},
{ index: 43, source: 57, properties: { "application.name": "cava" } },
{ index: 44, source: 56, properties: { "media.name": "Peak detect", "application.process.binary": "pavucontrol" } },
{
index: 45,
source: 56,
corked: true,
properties: { "application.name": "Discord", "application.process.id": "777" },
},
]);
const sources = JSON.stringify([
{ index: 56, name: "alsa_input.pci-0000_00_1f.3.analog-stereo", description: "Built-in Audio", mute: true },
{ index: 57, name: "alsa_output.pci-0000_00_1f.3.analog-stereo.monitor", description: "Monitor of Speakers" },
]);
const uses = parseSourceOutputs(outputs, sources);
assertEquals(uses.map((u) => [u.app, u.pid, u.streamIndex, u.paused]), [["Firefox", 4242, 42, false], [
"Discord",
777,
45,
true,
]]);
assertEquals(uses[0].sourceName, "alsa_input.pci-0000_00_1f.3.analog-stereo");
assertEquals(uses[0].sourceMuted, true);
assertEquals(parseSourceOutputs("[]", ""), []);
});
Deno.test("fuser, ps and device holders", () => {
assertEquals(parseFuser(" 1180m 7777"), [1180, 7777]);
assertEquals(parseFuser(""), []);
const procs = parsePs(
" 1180 9000 ada pipewire /usr/bin/pipewire\n 7777 65 ada zoom /opt/zoom/zoom --x\n",
);
assertEquals(procs.get(7777), { pid: 7777, elapsed: 65, user: "ada", comm: "zoom", args: "/opt/zoom/zoom --x" });
const uses = deviceUses([{ device: "/dev/video0", pids: [1180, 7777] }], procs);
assertEquals(uses.map((u) => [u.app, u.pid, u.device]), [["Zoom", 7777, "/dev/video0"]]);
});
Deno.test("mergeUses keeps one row per app, filling blanks", () => {
const a: Use = { kind: "microphone", key: "m1", app: "Firefox", pid: 4242, via: "pulse", streamIndex: 42 };
const b: Use = {
kind: "microphone",
key: "m2",
app: "Firefox",
pid: 4242,
via: "pipewire",
appId: "org.mozilla.firefox",
};
const c: Use = { kind: "camera", key: "c", app: "Firefox", pid: 4242, via: "pipewire" };
const merged = mergeUses([a], [b, c]);
assertEquals(merged.length, 2);
assertEquals([merged[0].via, merged[0].streamIndex, merged[0].appId, merged[0].key], [
"pulse",
42,
"org.mozilla.firefox",
"microphone:4242",
]);
});
Deno.test("GeoClue client listing", () => {
assertEquals(
parseGeoclueClients(
"/org/freedesktop/GeoClue2\n/org/freedesktop/GeoClue2/Client\n/org/freedesktop/GeoClue2/Client/3\n",
),
["/org/freedesktop/GeoClue2/Client/3"],
);
assertEquals(parseGeoclueClients("└─ /org/freedesktop/GeoClue2/Client/1"), ["/org/freedesktop/GeoClue2/Client/1"]);
assertEquals(busValue('{"type":"b","data":true}'), true);
assertEquals(busValue("nope"), undefined);
});
Deno.test("words", () => {
const u = (kind: Use["kind"], paused = false): Use => ({ kind, key: kind, app: "x", via: "pipewire", paused });
assertEquals(subtitle([]), null);
assertEquals(subtitle([u("camera")]), "Camera in use");
assertEquals(subtitle([u("camera"), u("microphone")]), "Camera and microphone in use");
assertEquals(subtitle([u("screen")]), "Screen shared");
assertEquals(subtitle([u("microphone"), u("screen")]), "Microphone in use, screen shared");
assertEquals(subtitle([u("microphone", true)]), null);
assertEquals(since(42), "42 s");
assertEquals(since(3900), "1 h 05 min");
assertEquals(prettyApp("google-chrome"), "Google Chrome");
assertEquals(prettyApp("OBS"), "OBS");
assert(since(90000).startsWith("1 d"));
});