@dion-js/extension-test-utils 0.2.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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/main.ts +84 -12
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dion-js/extension-test-utils",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/main.ts"
package/src/main.ts CHANGED
@@ -5,9 +5,12 @@ import type {
5
5
  CustomUI,
6
6
  Entry,
7
7
  EntryDetailed,
8
+ EntryId,
8
9
  ExtensionData,
9
10
  Link,
11
+ Paragraph,
10
12
  Permission,
13
+ SettingValue,
11
14
  Source,
12
15
  } from "@dion-js/runtime-types/runtime";
13
16
  import { Parser } from "m3u8-parser";
@@ -27,6 +30,15 @@ export class MockExtensionClient {
27
30
  ) => boolean
28
31
  >;
29
32
  getPath: Mock<(err: Error | null) => string>;
33
+ setEntrySetting: Mock<
34
+ (
35
+ err: Error | null,
36
+ entry: EntryId,
37
+ key: string,
38
+ value: SettingValue,
39
+ ) => void
40
+ >;
41
+ storeSet: Mock<(err: Error | null, key: string, value: unknown) => void>;
30
42
  constructor(extdata: ExtensionData, basepath: string) {
31
43
  this.name = extdata.name;
32
44
  this.loadData = mock((_err: Error | null, _arg: unknown) => "");
@@ -39,17 +51,34 @@ export class MockExtensionClient {
39
51
  // Empty mock implementation
40
52
  });
41
53
  this.requestPermission = mock((_err: Error | null, _asd: Permission) => {
42
- return false;
54
+ return true;
43
55
  });
44
56
  this.getPath = mock(() => {
45
57
  return `${basepath}/${extdata.name}`;
46
58
  });
59
+ this.setEntrySetting = mock(
60
+ (
61
+ _err: Error | null,
62
+ _entry: EntryId,
63
+ _key: string,
64
+ _value: SettingValue,
65
+ ): void => {
66
+ // Empty mock implementation
67
+ },
68
+ );
69
+ this.storeSet = mock(
70
+ (_err: Error | null, _key: string, _value: unknown): void => {
71
+ // Empty mock implementation
72
+ },
73
+ );
47
74
  this.client = new ExtensionClient(
48
75
  this.loadData,
49
76
  this.storeData,
50
77
  this.doAction,
51
78
  this.requestPermission,
52
79
  this.getPath,
80
+ this.setEntrySetting,
81
+ this.storeSet,
53
82
  );
54
83
  }
55
84
  }
@@ -90,20 +119,29 @@ export async function wait(ms: number): Promise<void> {
90
119
  });
91
120
  }
92
121
 
93
- let lock: Promise<void> | undefined;
122
+ let lock: Promise<void> = Promise.resolve();
94
123
  export async function ratelimit(ms: number) {
95
- if (lock !== undefined) await lock;
96
- lock = wait(ms);
124
+ const previous = lock;
125
+ let release!: () => void;
126
+ lock = new Promise<void>((resolve) => {
127
+ release = resolve;
128
+ });
129
+ try {
130
+ await previous;
131
+ await wait(ms);
132
+ } finally {
133
+ release();
134
+ }
97
135
  }
98
136
 
99
137
  const time = 50;
100
138
 
101
- function assert(condition: boolean, message: string): asserts condition {
139
+ export function assert(condition: boolean, message: string): asserts condition {
102
140
  if (!condition) throw new Error(message);
103
141
  }
104
142
 
105
143
  function assertWarn(condition: boolean, message: string): asserts condition {
106
- if (!condition) console.warn(message);
144
+ if (!condition) console.warn(message);
107
145
  }
108
146
 
109
147
  export async function assertValidM3U8(
@@ -203,27 +241,41 @@ export async function assertValidEntry(entry: Entry | EntryDetailed) {
203
241
  }
204
242
  }
205
243
  if ("ui" in entry && entry.ui !== undefined && entry.ui !== null) {
206
- assertValidUI(entry.ui);
244
+ await assertValidUI(entry.ui);
207
245
  }
208
246
  }
209
247
 
210
- function assertValidUI(ui: CustomUI) {
248
+ async function assertValidUI(ui: CustomUI) {
211
249
  if ("children" in ui && ui.children !== undefined && ui.children !== null) {
212
250
  for (const child of ui.children) {
213
- assertValidUI(child);
251
+ await assertValidUI(child);
252
+ }
253
+ }
254
+ if ("child" in ui && ui.child !== undefined && ui.child !== null) {
255
+ await assertValidUI(ui.child);
256
+ }
257
+ if (ui.type === "Card") {
258
+ if (ui.top !== undefined && ui.top !== null) await assertValidUI(ui.top);
259
+ if (ui.bottom !== undefined && ui.bottom !== null) {
260
+ await assertValidUI(ui.bottom);
261
+ }
262
+ }
263
+ if (ui.type === "ListTile") {
264
+ for (const slot of [ui.leading, ui.title, ui.subtitle, ui.trailing]) {
265
+ if (slot !== undefined && slot !== null) await assertValidUI(slot);
214
266
  }
215
267
  }
216
268
  switch (ui.type) {
217
269
  case "EntryCard":
218
- assertValidEntry(ui.entry);
270
+ await assertValidEntry(ui.entry);
219
271
  break;
220
272
  case "Image":
221
- assertValidImageURL(ui.image, {
273
+ await assertValidImageURL(ui.image, {
222
274
  assertmsg: `CustomUI has an invalid image url ${ui.image}`,
223
275
  });
224
276
  break;
225
277
  case "Link":
226
- assertValidURL(
278
+ await assertValidURL(
227
279
  { url: ui.link },
228
280
  {
229
281
  assertmsg: `CustomUI has an invalid link ${ui.link}`,
@@ -278,6 +330,26 @@ export async function assertValidSource(source: Source) {
278
330
  });
279
331
  }
280
332
  break;
333
+ case "Paragraphlist":
334
+ for (const paragraph of sample(source.paragraphs, 5)) {
335
+ await assertValidParagraph(paragraph);
336
+ }
337
+ break;
338
+ }
339
+ }
340
+
341
+ async function assertValidParagraph(paragraph: Paragraph) {
342
+ switch (paragraph.type) {
343
+ case "CustomUI":
344
+ await assertValidUI(paragraph.ui);
345
+ break;
346
+ case "Table":
347
+ for (const row of paragraph.columns) {
348
+ for (const cell of sample(row.cells, 5)) {
349
+ await assertValidParagraph(cell);
350
+ }
351
+ }
352
+ break;
281
353
  }
282
354
  }
283
355