@ac-kit/browser 0.1.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/README.md +325 -4
  3. package/dist/animation-frame/frame-scheduler.d.ts +91 -0
  4. package/dist/animation-frame/frame-scheduler.d.ts.map +1 -0
  5. package/dist/animation-frame/frame-scheduler.js +181 -0
  6. package/dist/animation-frame/frame-scheduler.js.map +1 -0
  7. package/dist/canvas/canvas-backing-size.d.ts +58 -0
  8. package/dist/canvas/canvas-backing-size.d.ts.map +1 -0
  9. package/dist/canvas/canvas-backing-size.js +141 -0
  10. package/dist/canvas/canvas-backing-size.js.map +1 -0
  11. package/dist/canvas/image-data-pixels.d.ts +61 -0
  12. package/dist/canvas/image-data-pixels.d.ts.map +1 -0
  13. package/dist/canvas/image-data-pixels.js +71 -0
  14. package/dist/canvas/image-data-pixels.js.map +1 -0
  15. package/dist/canvas/read-image-data.d.ts +120 -0
  16. package/dist/canvas/read-image-data.d.ts.map +1 -0
  17. package/dist/canvas/read-image-data.js +147 -0
  18. package/dist/canvas/read-image-data.js.map +1 -0
  19. package/dist/dom/index.d.ts +4 -0
  20. package/dist/dom/index.d.ts.map +1 -0
  21. package/dist/dom/index.js +4 -0
  22. package/dist/dom/index.js.map +1 -0
  23. package/dist/dom/media-query/watch-media-query.d.ts +26 -0
  24. package/dist/dom/media-query/watch-media-query.d.ts.map +1 -0
  25. package/dist/dom/media-query/watch-media-query.js +42 -0
  26. package/dist/dom/media-query/watch-media-query.js.map +1 -0
  27. package/dist/dom/resize-observer/observe-canvas-size.d.ts +54 -0
  28. package/dist/dom/resize-observer/observe-canvas-size.d.ts.map +1 -0
  29. package/dist/dom/resize-observer/observe-canvas-size.js +189 -0
  30. package/dist/dom/resize-observer/observe-canvas-size.js.map +1 -0
  31. package/dist/dom/web-animations/wait-for-transitions.d.ts +59 -0
  32. package/dist/dom/web-animations/wait-for-transitions.d.ts.map +1 -0
  33. package/dist/dom/web-animations/wait-for-transitions.js +40 -0
  34. package/dist/dom/web-animations/wait-for-transitions.js.map +1 -0
  35. package/dist/index.d.ts +6 -0
  36. package/dist/index.d.ts.map +1 -1
  37. package/dist/index.js +6 -0
  38. package/dist/index.js.map +1 -1
  39. package/dist/indexeddb/open-indexed-db.d.ts +1 -1
  40. package/dist/indexeddb/open-indexed-db.d.ts.map +1 -1
  41. package/dist/indexeddb/open-indexed-db.js +2 -1
  42. package/dist/indexeddb/open-indexed-db.js.map +1 -1
  43. package/dist/media-capture/microphone-access.d.ts +116 -0
  44. package/dist/media-capture/microphone-access.d.ts.map +1 -0
  45. package/dist/media-capture/microphone-access.js +222 -0
  46. package/dist/media-capture/microphone-access.js.map +1 -0
  47. package/dist/testing/fake-animation-frames.d.ts +31 -0
  48. package/dist/testing/fake-animation-frames.d.ts.map +1 -0
  49. package/dist/testing/fake-animation-frames.js +75 -0
  50. package/dist/testing/fake-animation-frames.js.map +1 -0
  51. package/dist/testing/fake-audio-context.d.ts +174 -0
  52. package/dist/testing/fake-audio-context.d.ts.map +1 -0
  53. package/dist/testing/fake-audio-context.js +314 -0
  54. package/dist/testing/fake-audio-context.js.map +1 -0
  55. package/dist/testing/fake-match-media.d.ts +19 -0
  56. package/dist/testing/fake-match-media.d.ts.map +1 -0
  57. package/dist/testing/fake-match-media.js +135 -0
  58. package/dist/testing/fake-match-media.js.map +1 -0
  59. package/dist/testing/fake-media-devices.d.ts +90 -0
  60. package/dist/testing/fake-media-devices.d.ts.map +1 -0
  61. package/dist/testing/fake-media-devices.js +147 -0
  62. package/dist/testing/fake-media-devices.js.map +1 -0
  63. package/dist/testing/fake-resize-observer.d.ts +58 -0
  64. package/dist/testing/fake-resize-observer.d.ts.map +1 -0
  65. package/dist/testing/fake-resize-observer.js +118 -0
  66. package/dist/testing/fake-resize-observer.js.map +1 -0
  67. package/dist/testing/fake-web-midi.d.ts +25 -0
  68. package/dist/testing/fake-web-midi.d.ts.map +1 -0
  69. package/dist/testing/fake-web-midi.js +52 -0
  70. package/dist/testing/fake-web-midi.js.map +1 -0
  71. package/dist/testing/index.d.ts +7 -0
  72. package/dist/testing/index.d.ts.map +1 -0
  73. package/dist/testing/index.js +7 -0
  74. package/dist/testing/index.js.map +1 -0
  75. package/dist/web-audio/audio-clock-mapping.d.ts +124 -0
  76. package/dist/web-audio/audio-clock-mapping.d.ts.map +1 -0
  77. package/dist/web-audio/audio-clock-mapping.js +245 -0
  78. package/dist/web-audio/audio-clock-mapping.js.map +1 -0
  79. package/package.json +23 -7
@@ -0,0 +1,174 @@
1
+ /** An audio node that records what it was connected to, and its disconnections. */
2
+ export declare class FakeAudioNode {
3
+ /** The nodes it was connected to, in order. */
4
+ readonly connections: unknown[];
5
+ /** How many times the page called {@link disconnect}. */
6
+ disconnectCount: number;
7
+ connect(destination: unknown): void;
8
+ disconnect(): void;
9
+ }
10
+ /** The node a microphone stream enters the graph by. */
11
+ export declare class FakeMediaStreamSource extends FakeAudioNode {
12
+ readonly stream: MediaStream;
13
+ constructor(stream: MediaStream);
14
+ }
15
+ /**
16
+ * A message port the page posts to and listens on. What arrives before there is
17
+ * a listener waits for it, and is handed over after the current task, as a real
18
+ * port does.
19
+ */
20
+ export declare class FakeMessagePort {
21
+ /** What the page posted, in order. */
22
+ readonly posted: unknown[];
23
+ /**
24
+ * The other end, given each message the page posts once it is recorded: set
25
+ * it to feed a real processor's logic, rather than replacing
26
+ * {@link postMessage}, whose replacement would leave {@link posted} empty.
27
+ */
28
+ onPostMessage: ((message: unknown) => void) | undefined;
29
+ private handler;
30
+ private readonly pending;
31
+ postMessage(message: unknown): void;
32
+ get onmessage(): ((event: MessageEvent) => void) | null;
33
+ set onmessage(handler: ((event: MessageEvent) => void) | null);
34
+ /** `data` arrives from the other end. */
35
+ receive(data: unknown): void;
36
+ private flush;
37
+ }
38
+ /**
39
+ * An `AudioWorkletNode` whose processor is the test: what the page posts is in
40
+ * {@link posted}, and {@link emit} posts to the page. The processor options it
41
+ * was made with are in {@link configuration}.
42
+ */
43
+ export declare class FakeAudioWorkletNode extends FakeAudioNode {
44
+ readonly configuration?: unknown;
45
+ onprocessorerror: ((event: Event) => void) | null;
46
+ readonly port: FakeMessagePort;
47
+ constructor(configuration?: unknown);
48
+ /** What the page posted to the processor, in order. */
49
+ get posted(): unknown[];
50
+ /** The messages of `type` the page posted. */
51
+ postedOf(type: string): Record<string, unknown>[];
52
+ /** The processor posts `data` to the page. */
53
+ emit(data: unknown): void;
54
+ /** The processor threw. */
55
+ fail(): void;
56
+ }
57
+ /** An audio buffer holding the samples copied into it. */
58
+ export declare class FakeAudioBuffer {
59
+ readonly length: number;
60
+ readonly sampleRate: number;
61
+ readonly data: Float32Array;
62
+ constructor(length: number, sampleRate: number);
63
+ copyToChannel(samples: Float32Array, _channel: number): void;
64
+ }
65
+ /** A gain node: its value is what a test reads. */
66
+ export declare class FakeGainNode extends FakeAudioNode {
67
+ readonly gain: {
68
+ value: number;
69
+ };
70
+ }
71
+ /**
72
+ * A buffer source: {@link start} records when it was asked to start, and
73
+ * {@link stop} ends it, as a real one does.
74
+ */
75
+ export declare class FakeBufferSource extends FakeAudioNode {
76
+ buffer: FakeAudioBuffer | null;
77
+ onended: (() => void) | null;
78
+ /** The node it was connected to. */
79
+ output: FakeGainNode | undefined;
80
+ /** The times it was asked to start at, in order; `undefined` for now. */
81
+ readonly starts: (number | undefined)[];
82
+ /** How many times the page called {@link stop}. */
83
+ stopCount: number;
84
+ connect(node: FakeGainNode): void;
85
+ start(when?: number): void;
86
+ stop(): void;
87
+ }
88
+ /**
89
+ * An `AudioContext` that starts suspended, as before a gesture, and answers
90
+ * `resume`, `suspend` and `close` by its state and a `statechange` event.
91
+ */
92
+ export declare class FakeAudioContext extends EventTarget {
93
+ readonly sampleRate: number;
94
+ state: AudioContextState;
95
+ /** Whether the page's `resume()` is allowed: false before a gesture. */
96
+ isResumeAllowed: boolean;
97
+ /** What the next `addModule` and `resume` calls wait for, in turn. */
98
+ moduleGates: Promise<void>[];
99
+ resumeGates: Promise<void>[];
100
+ /** The URLs of every `addModule` call, in order. */
101
+ readonly modules: string[];
102
+ readonly audioWorklet: {
103
+ addModule: (url: string) => Promise<void>;
104
+ };
105
+ isModuleFailing: boolean;
106
+ /** Whether a stream whose rate is not the context's is refused, as Firefox. */
107
+ isRateMatchRequired: boolean;
108
+ /** The name of the exception `createMediaStreamSource` throws, if any. */
109
+ sourceErrorName: string | undefined;
110
+ readonly sources: FakeMediaStreamSource[];
111
+ /** The context's clock, seconds: tests set it. */
112
+ currentTime: number;
113
+ baseLatency: number;
114
+ /** What the browser reports of the output; `undefined` when it does not. */
115
+ outputLatency: number | undefined;
116
+ readonly destination: {};
117
+ /** The sounds played, in order. */
118
+ readonly bufferSources: FakeBufferSource[];
119
+ /** How many times the page called {@link resume}. */
120
+ resumeCount: number;
121
+ /** How many times the page called {@link suspend}. */
122
+ suspendCount: number;
123
+ /** How many times the page called {@link close}. */
124
+ closeCount: number;
125
+ constructor(sampleRate: number);
126
+ createBuffer(_channels: number, length: number, sampleRate: number): FakeAudioBuffer;
127
+ createBufferSource(): FakeBufferSource;
128
+ createGain(): FakeGainNode;
129
+ createMediaStreamSource(stream: MediaStream): FakeMediaStreamSource;
130
+ resume(): Promise<void>;
131
+ suspend(): Promise<void>;
132
+ close(): Promise<void>;
133
+ /** The browser changes the context's state: another tab, a phone call. */
134
+ setState(state: string): void;
135
+ }
136
+ /**
137
+ * What a browser makes audio contexts and worklet nodes of: records every one,
138
+ * and refuses the sampling rates it is told to, as Firefox does.
139
+ */
140
+ export declare class FakeAudioEnvironment {
141
+ readonly contexts: FakeAudioContext[];
142
+ readonly nodes: FakeAudioWorkletNode[];
143
+ /** The rates `createContext` refuses with a `NotSupportedError`. */
144
+ readonly unsupportedRates: Set<number>;
145
+ /** The rate of a context made with none. */
146
+ defaultRate: number;
147
+ /** Whether the contexts made from now on are Firefox's about rates. */
148
+ isRateMatchRequired: boolean;
149
+ /** Whether the contexts made from now on fail to load the module. */
150
+ isModuleFailing: boolean;
151
+ /** The exception the contexts made from now on throw making a source. */
152
+ sourceErrorName: string | undefined;
153
+ /** Whether the contexts made from now on may run: false before a gesture. */
154
+ isResumeAllowed: boolean;
155
+ private readonly moduleGates;
156
+ private readonly resumeGates;
157
+ /**
158
+ * Makes the next `addModule` of any context wait.
159
+ *
160
+ * @returns What lets it go on.
161
+ */
162
+ holdNextModule(): () => void;
163
+ /**
164
+ * Makes the next `resume` of any context wait, as before a gesture.
165
+ *
166
+ * @returns What lets it go on.
167
+ */
168
+ holdNextResume(): () => void;
169
+ readonly createContext: (sampleRate?: number) => AudioContext;
170
+ readonly createWorkletNode: (_context: AudioContext, configuration?: unknown) => AudioWorkletNode;
171
+ get context(): FakeAudioContext;
172
+ get node(): FakeAudioWorkletNode;
173
+ }
174
+ //# sourceMappingURL=fake-audio-context.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-audio-context.d.ts","sourceRoot":"","sources":["../../src/testing/fake-audio-context.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,qBAAa,aAAa;IACzB,+CAA+C;IAC/C,QAAQ,CAAC,WAAW,EAAE,OAAO,EAAE,CAAM;IACrC,yDAAyD;IACzD,eAAe,SAAK;IAEpB,OAAO,CAAC,WAAW,EAAE,OAAO,GAAG,IAAI,CAElC;IAED,UAAU,IAAI,IAAI,CAEjB;CACD;AAED,wDAAwD;AACxD,qBAAa,qBAAsB,SAAQ,aAAa;IAC3C,QAAQ,CAAC,MAAM,EAAE,WAAW;IAAxC,YAAqB,MAAM,EAAE,WAAW,EAEvC;CACD;AAED;;;;GAIG;AACH,qBAAa,eAAe;IAC3B,sCAAsC;IACtC,QAAQ,CAAC,MAAM,EAAE,OAAO,EAAE,CAAM;IAChC;;;;OAIG;IACH,aAAa,EAAE,CAAC,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC;IACxD,OAAO,CAAC,OAAO,C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ACH,cAAc,IAAI,MAAM,IAAI,CAK3B;IAED;;;;OAIG;IACH,cAAc,IAAI,MAAM,IAAI,CAK3B;IAED,QAAQ,CAAC,aAAa,gBAAiB,MAAM,KAAG,YAAY,CAe1D;IAEF,QAAQ,CAAC,iBAAiB,aACf,YAAY,kBACN,OAAO,KACrB,gBAAgB,CAKjB;IAEF,IAAI,OAAO,IAAI,gBAAgB,CAO9B;IAED,IAAI,IAAI,IAAI,oBAAoB,CAO/B;CACD"}
@@ -0,0 +1,314 @@
1
+ /** An audio node that records what it was connected to, and its disconnections. */
2
+ export class FakeAudioNode {
3
+ /** The nodes it was connected to, in order. */
4
+ connections = [];
5
+ /** How many times the page called {@link disconnect}. */
6
+ disconnectCount = 0;
7
+ connect(destination) {
8
+ this.connections.push(destination);
9
+ }
10
+ disconnect() {
11
+ this.disconnectCount += 1;
12
+ }
13
+ }
14
+ /** The node a microphone stream enters the graph by. */
15
+ export class FakeMediaStreamSource extends FakeAudioNode {
16
+ stream;
17
+ constructor(stream) {
18
+ super();
19
+ this.stream = stream;
20
+ }
21
+ }
22
+ /**
23
+ * A message port the page posts to and listens on. What arrives before there is
24
+ * a listener waits for it, and is handed over after the current task, as a real
25
+ * port does.
26
+ */
27
+ export class FakeMessagePort {
28
+ /** What the page posted, in order. */
29
+ posted = [];
30
+ /**
31
+ * The other end, given each message the page posts once it is recorded: set
32
+ * it to feed a real processor's logic, rather than replacing
33
+ * {@link postMessage}, whose replacement would leave {@link posted} empty.
34
+ */
35
+ onPostMessage;
36
+ handler = null;
37
+ pending = [];
38
+ postMessage(message) {
39
+ this.posted.push(message);
40
+ this.onPostMessage?.(message);
41
+ }
42
+ get onmessage() {
43
+ return this.handler;
44
+ }
45
+ set onmessage(handler) {
46
+ this.handler = handler;
47
+ if (handler !== null && this.pending.length > 0) {
48
+ queueMicrotask(() => this.flush());
49
+ }
50
+ }
51
+ /** `data` arrives from the other end. */
52
+ receive(data) {
53
+ if (this.handler === null) {
54
+ this.pending.push(data);
55
+ }
56
+ else {
57
+ this.handler(new MessageEvent("message", { data }));
58
+ }
59
+ }
60
+ flush() {
61
+ for (const data of this.pending.splice(0)) {
62
+ this.handler?.(new MessageEvent("message", { data }));
63
+ }
64
+ }
65
+ }
66
+ /**
67
+ * An `AudioWorkletNode` whose processor is the test: what the page posts is in
68
+ * {@link posted}, and {@link emit} posts to the page. The processor options it
69
+ * was made with are in {@link configuration}.
70
+ */
71
+ export class FakeAudioWorkletNode extends FakeAudioNode {
72
+ configuration;
73
+ onprocessorerror = null;
74
+ port = new FakeMessagePort();
75
+ constructor(configuration) {
76
+ super();
77
+ this.configuration = configuration;
78
+ }
79
+ /** What the page posted to the processor, in order. */
80
+ get posted() {
81
+ return this.port.posted;
82
+ }
83
+ /** The messages of `type` the page posted. */
84
+ postedOf(type) {
85
+ return this.posted.filter((message) => typeof message === "object" &&
86
+ message !== null &&
87
+ "type" in message &&
88
+ message.type === type);
89
+ }
90
+ /** The processor posts `data` to the page. */
91
+ emit(data) {
92
+ this.port.receive(data);
93
+ }
94
+ /** The processor threw. */
95
+ fail() {
96
+ this.onprocessorerror?.(new Event("processorerror"));
97
+ }
98
+ }
99
+ /** An audio buffer holding the samples copied into it. */
100
+ export class FakeAudioBuffer {
101
+ length;
102
+ sampleRate;
103
+ data;
104
+ constructor(length, sampleRate) {
105
+ this.length = length;
106
+ this.sampleRate = sampleRate;
107
+ this.data = new Float32Array(length);
108
+ }
109
+ copyToChannel(samples, _channel) {
110
+ this.data.set(samples);
111
+ }
112
+ }
113
+ /** A gain node: its value is what a test reads. */
114
+ export class FakeGainNode extends FakeAudioNode {
115
+ gain = { value: 1 };
116
+ }
117
+ /**
118
+ * A buffer source: {@link start} records when it was asked to start, and
119
+ * {@link stop} ends it, as a real one does.
120
+ */
121
+ export class FakeBufferSource extends FakeAudioNode {
122
+ buffer = null;
123
+ onended = null;
124
+ /** The node it was connected to. */
125
+ output;
126
+ /** The times it was asked to start at, in order; `undefined` for now. */
127
+ starts = [];
128
+ /** How many times the page called {@link stop}. */
129
+ stopCount = 0;
130
+ connect(node) {
131
+ super.connect(node);
132
+ this.output = node;
133
+ }
134
+ start(when) {
135
+ this.starts.push(when);
136
+ }
137
+ stop() {
138
+ this.stopCount += 1;
139
+ this.onended?.();
140
+ }
141
+ }
142
+ /**
143
+ * An `AudioContext` that starts suspended, as before a gesture, and answers
144
+ * `resume`, `suspend` and `close` by its state and a `statechange` event.
145
+ */
146
+ export class FakeAudioContext extends EventTarget {
147
+ sampleRate;
148
+ state = "suspended";
149
+ /** Whether the page's `resume()` is allowed: false before a gesture. */
150
+ isResumeAllowed = true;
151
+ /** What the next `addModule` and `resume` calls wait for, in turn. */
152
+ moduleGates = [];
153
+ resumeGates = [];
154
+ /** The URLs of every `addModule` call, in order. */
155
+ modules = [];
156
+ audioWorklet = {
157
+ addModule: async (url) => {
158
+ this.modules.push(url);
159
+ await this.moduleGates.shift();
160
+ if (this.isModuleFailing) {
161
+ throw new DOMException("fake module failure", "AbortError");
162
+ }
163
+ },
164
+ };
165
+ isModuleFailing = false;
166
+ /** Whether a stream whose rate is not the context's is refused, as Firefox. */
167
+ isRateMatchRequired = false;
168
+ /** The name of the exception `createMediaStreamSource` throws, if any. */
169
+ sourceErrorName;
170
+ sources = [];
171
+ /** The context's clock, seconds: tests set it. */
172
+ currentTime = 0;
173
+ baseLatency = 0.01;
174
+ /** What the browser reports of the output; `undefined` when it does not. */
175
+ outputLatency = 0.02;
176
+ destination = {};
177
+ /** The sounds played, in order. */
178
+ bufferSources = [];
179
+ /** How many times the page called {@link resume}. */
180
+ resumeCount = 0;
181
+ /** How many times the page called {@link suspend}. */
182
+ suspendCount = 0;
183
+ /** How many times the page called {@link close}. */
184
+ closeCount = 0;
185
+ constructor(sampleRate) {
186
+ super();
187
+ this.sampleRate = sampleRate;
188
+ }
189
+ createBuffer(_channels, length, sampleRate) {
190
+ return new FakeAudioBuffer(length, sampleRate);
191
+ }
192
+ createBufferSource() {
193
+ const source = new FakeBufferSource();
194
+ this.bufferSources.push(source);
195
+ return source;
196
+ }
197
+ createGain() {
198
+ return new FakeGainNode();
199
+ }
200
+ createMediaStreamSource(stream) {
201
+ if (this.sourceErrorName !== undefined) {
202
+ throw new DOMException("fake source failure", this.sourceErrorName);
203
+ }
204
+ const [track] = stream.getAudioTracks();
205
+ const rate = track?.getSettings().sampleRate;
206
+ if (this.isRateMatchRequired &&
207
+ rate !== undefined &&
208
+ rate !== this.sampleRate) {
209
+ throw new DOMException("sample rates differ", "NotSupportedError");
210
+ }
211
+ const source = new FakeMediaStreamSource(stream);
212
+ this.sources.push(source);
213
+ return source;
214
+ }
215
+ async resume() {
216
+ this.resumeCount += 1;
217
+ await this.resumeGates.shift();
218
+ if (this.isResumeAllowed) {
219
+ this.setState("running");
220
+ }
221
+ }
222
+ async suspend() {
223
+ this.suspendCount += 1;
224
+ this.setState("suspended");
225
+ }
226
+ async close() {
227
+ this.closeCount += 1;
228
+ this.setState("closed");
229
+ }
230
+ /** The browser changes the context's state: another tab, a phone call. */
231
+ setState(state) {
232
+ if (state !== this.state) {
233
+ this.state = state;
234
+ this.dispatchEvent(new Event("statechange"));
235
+ }
236
+ }
237
+ }
238
+ /**
239
+ * What a browser makes audio contexts and worklet nodes of: records every one,
240
+ * and refuses the sampling rates it is told to, as Firefox does.
241
+ */
242
+ export class FakeAudioEnvironment {
243
+ contexts = [];
244
+ nodes = [];
245
+ /** The rates `createContext` refuses with a `NotSupportedError`. */
246
+ unsupportedRates = new Set();
247
+ /** The rate of a context made with none. */
248
+ defaultRate = 48000;
249
+ /** Whether the contexts made from now on are Firefox's about rates. */
250
+ isRateMatchRequired = false;
251
+ /** Whether the contexts made from now on fail to load the module. */
252
+ isModuleFailing = false;
253
+ /** The exception the contexts made from now on throw making a source. */
254
+ sourceErrorName;
255
+ /** Whether the contexts made from now on may run: false before a gesture. */
256
+ isResumeAllowed = true;
257
+ moduleGates = [];
258
+ resumeGates = [];
259
+ /**
260
+ * Makes the next `addModule` of any context wait.
261
+ *
262
+ * @returns What lets it go on.
263
+ */
264
+ holdNextModule() {
265
+ const gate = Promise.withResolvers();
266
+ this.moduleGates.push(gate.promise);
267
+ return gate.resolve;
268
+ }
269
+ /**
270
+ * Makes the next `resume` of any context wait, as before a gesture.
271
+ *
272
+ * @returns What lets it go on.
273
+ */
274
+ holdNextResume() {
275
+ const gate = Promise.withResolvers();
276
+ this.resumeGates.push(gate.promise);
277
+ return gate.resolve;
278
+ }
279
+ createContext = (sampleRate) => {
280
+ const rate = sampleRate ?? this.defaultRate;
281
+ if (this.unsupportedRates.has(rate)) {
282
+ throw new DOMException(`rate ${rate} refused`, "NotSupportedError");
283
+ }
284
+ const context = new FakeAudioContext(rate);
285
+ context.isRateMatchRequired = this.isRateMatchRequired;
286
+ context.isModuleFailing = this.isModuleFailing;
287
+ context.sourceErrorName = this.sourceErrorName;
288
+ context.isResumeAllowed = this.isResumeAllowed;
289
+ context.moduleGates = this.moduleGates;
290
+ context.resumeGates = this.resumeGates;
291
+ this.contexts.push(context);
292
+ return context;
293
+ };
294
+ createWorkletNode = (_context, configuration) => {
295
+ const node = new FakeAudioWorkletNode(configuration);
296
+ this.nodes.push(node);
297
+ return node;
298
+ };
299
+ get context() {
300
+ const context = this.contexts.at(-1);
301
+ if (context === undefined) {
302
+ throw new Error("no context was made");
303
+ }
304
+ return context;
305
+ }
306
+ get node() {
307
+ const node = this.nodes.at(-1);
308
+ if (node === undefined) {
309
+ throw new Error("no worklet node was made");
310
+ }
311
+ return node;
312
+ }
313
+ }
314
+ //# sourceMappingURL=fake-audio-context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-audio-context.js","sourceRoot":"","sources":["../../src/testing/fake-audio-context.ts"],"names":[],"mappings":"AAAA,mFAAmF;AACnF,MAAM,OAAO,aAAa;IACzB,+CAA+C;IACtC,WAAW,GAAc,EAAE,CAAC;IACrC,yDAAyD;IACzD,eAAe,GAAG,CAAC,CAAC;IAEpB,OAAO,CAAC,WAAoB;QAC3B,IAA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AAI,CAAC,eAAe,CAAC;QAC/C,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;QAC/C,OAAO,CAAC,eAAe,GAAG,IAAI,CAAC,eAAe,CAAC;QAC/C,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACvC,OAAO,CAAC,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACvC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE5B,OAAO,OAAkC,CAAC;IAC3C,CAAC,CAAC;IAEO,iBAAiB,GAAG,CAC5B,QAAsB,EACtB,aAAuB,EACJ,EAAE;QACrB,MAAM,IAAI,GAAG,IAAI,oBAAoB,CAAC,aAAa,CAAC,CAAC;QACrD,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAEtB,OAAO,IAAmC,CAAC;IAC5C,CAAC,CAAC;IAEF,IAAI,OAAO;QACV,MAAM,OAAO,GAAG,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QACrC,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACxC,CAAC;QAED,OAAO,OAAO,CAAC;IAChB,CAAC;IAED,IAAI,IAAI;QACP,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;QAC/B,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;QAC7C,CAAC;QAED,OAAO,IAAI,CAAC;IACb,CAAC;CACD"}
@@ -0,0 +1,19 @@
1
+ /**
2
+ * A window's `matchMedia` for tests under Node: every query starts as not
3
+ * matching, and the test flips it, which fires `change` on the lists given.
4
+ *
5
+ * @example
6
+ * const media = new FakeMatchMedia();
7
+ * const watch = watchMediaQuery(media, "(prefers-reduced-motion: reduce)");
8
+ * media.set("(prefers-reduced-motion: reduce)", true);
9
+ */
10
+ export declare class FakeMatchMedia implements Pick<Window, "matchMedia"> {
11
+ private readonly matches_;
12
+ private readonly lists_;
13
+ /** Sets whether `query` matches and tells the lists made for it. */
14
+ set(query: string, matches: boolean): void;
15
+ /** How many `change` listeners are attached, across every query. */
16
+ get listenerCount(): number;
17
+ matchMedia: (query: string) => MediaQueryList;
18
+ }
19
+ //# sourceMappingURL=fake-match-media.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-match-media.d.ts","sourceRoot":"","sources":["../../src/testing/fake-match-media.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,qBAAa,cAAe,YAAW,IAAI,CAAC,MAAM,EAAE,YAAY,CAAC;IAChE,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA8B;IACvD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA+C;IAEtE,oEAAoE;IACpE,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CASzC;IAED,oEAAoE;IACpE,IAAI,aAAa,IAAI,MAAM,CAS1B;IAED,UAAU,UAAW,MAAM,KAAG,cAAc,CAa1C;CACF"}
@@ -0,0 +1,135 @@
1
+ /**
2
+ * A window's `matchMedia` for tests under Node: every query starts as not
3
+ * matching, and the test flips it, which fires `change` on the lists given.
4
+ *
5
+ * @example
6
+ * const media = new FakeMatchMedia();
7
+ * const watch = watchMediaQuery(media, "(prefers-reduced-motion: reduce)");
8
+ * media.set("(prefers-reduced-motion: reduce)", true);
9
+ */
10
+ export class FakeMatchMedia {
11
+ matches_ = new Map();
12
+ lists_ = new Map();
13
+ /** Sets whether `query` matches and tells the lists made for it. */
14
+ set(query, matches) {
15
+ const wasMatching = this.matches_.get(query) ?? false;
16
+ this.matches_.set(query, matches);
17
+ if (wasMatching === matches) {
18
+ return;
19
+ }
20
+ for (const list of this.lists_.get(query) ?? []) {
21
+ list.fire(matches);
22
+ }
23
+ }
24
+ /** How many `change` listeners are attached, across every query. */
25
+ get listenerCount() {
26
+ let count = 0;
27
+ for (const lists of this.lists_.values()) {
28
+ for (const list of lists) {
29
+ count += list.listenerCount;
30
+ }
31
+ }
32
+ return count;
33
+ }
34
+ matchMedia = (query) => {
35
+ const list = new FakeMediaQueryList_(query, () => this.matches_.get(query) ?? false);
36
+ let lists = this.lists_.get(query);
37
+ if (lists === undefined) {
38
+ lists = new Set();
39
+ this.lists_.set(query, lists);
40
+ }
41
+ lists.add(list);
42
+ return list;
43
+ };
44
+ }
45
+ class FakeMediaQueryList_ extends EventTarget {
46
+ media;
47
+ read_;
48
+ registrations_ = [];
49
+ constructor(media, read_) {
50
+ super();
51
+ this.media = media;
52
+ this.read_ = read_;
53
+ }
54
+ get matches() {
55
+ return this.read_();
56
+ }
57
+ get listenerCount() {
58
+ return this.registrations_.length;
59
+ }
60
+ addEventListener(type, callback, options) {
61
+ if (callback === null) {
62
+ return;
63
+ }
64
+ const capture = typeof options === "boolean" ? options : (options?.capture ?? false);
65
+ // As `EventTarget` does: the same type, callback and capture flag once.
66
+ if (this.indexOf_(type, callback, capture) >= 0) {
67
+ return;
68
+ }
69
+ const signal = typeof options === "object" ? options.signal : undefined;
70
+ if (signal?.aborted) {
71
+ return;
72
+ }
73
+ const once = typeof options === "object" && options.once === true;
74
+ const registration = {
75
+ type,
76
+ callback,
77
+ capture,
78
+ listener: (event) => {
79
+ // `EventTarget` forgets a `once` listener before invoking it, so one
80
+ // that registers again from its callback is counted again.
81
+ if (once) {
82
+ this.forget_(registration);
83
+ }
84
+ if (typeof callback === "function") {
85
+ callback.call(this, event);
86
+ }
87
+ else {
88
+ callback.handleEvent(event);
89
+ }
90
+ },
91
+ };
92
+ this.registrations_.push(registration);
93
+ super.addEventListener(type, registration.listener, {
94
+ capture,
95
+ once,
96
+ passive: typeof options === "object" ? options.passive : undefined,
97
+ signal,
98
+ });
99
+ signal?.addEventListener("abort", () => this.forget_(registration), {
100
+ once: true,
101
+ });
102
+ }
103
+ removeEventListener(type, callback, options) {
104
+ if (callback === null) {
105
+ return;
106
+ }
107
+ const capture = typeof options === "boolean" ? options : (options?.capture ?? false);
108
+ const index = this.indexOf_(type, callback, capture);
109
+ if (index < 0) {
110
+ // `EventTarget` itself removes the listener it holds when a signal aborts.
111
+ super.removeEventListener(type, callback, options);
112
+ return;
113
+ }
114
+ const [registration] = this.registrations_.splice(index, 1);
115
+ super.removeEventListener(type, registration.listener, capture);
116
+ }
117
+ fire(matches) {
118
+ const event = new Event("change");
119
+ event.matches = matches;
120
+ event.media = this.media;
121
+ this.dispatchEvent(event);
122
+ }
123
+ indexOf_(type, callback, capture) {
124
+ return this.registrations_.findIndex((registration) => registration.type === type &&
125
+ registration.callback === callback &&
126
+ registration.capture === capture);
127
+ }
128
+ forget_(registration) {
129
+ const index = this.registrations_.indexOf(registration);
130
+ if (index >= 0) {
131
+ this.registrations_.splice(index, 1);
132
+ }
133
+ }
134
+ }
135
+ //# sourceMappingURL=fake-match-media.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fake-match-media.js","sourceRoot":"","sources":["../../src/testing/fake-match-media.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,OAAO,cAAc;IACT,QAAQ,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtC,MAAM,GAAG,IAAI,GAAG,EAAoC,CAAC;IAEtE,oEAAoE;IACpE,GAAG,CAAC,KAAa,EAAE,OAAgB;QAClC,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC;QACtD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAClC,IAAI,WAAW,KAAK,OAAO,EAAE,CAAC;YAC7B,OAAO;QACR,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,C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