@dion-js/extension-test-utils 0.2.0 → 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 +6 -6
  2. package/src/main.ts +93 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dion-js/extension-test-utils",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./src/main.ts"
@@ -9,13 +9,13 @@
9
9
  "image-url-validator": "^1.0.4",
10
10
  "m3u8-parser": "^7.2.0",
11
11
  "node-fetch": "^3.3.2",
12
- "@dion-js/extension-types": "0.4.0",
13
- "@dion-js/runtime-types": "0.4.0"
12
+ "@dion-js/extension-types": "workspace:*",
13
+ "@dion-js/runtime-types": "workspace:*"
14
14
  },
15
15
  "devDependencies": {
16
16
  "@types/m3u8-parser": "^7.2.2",
17
- "@dion-js/config": "0.1.0",
18
- "@dion-js/extension-types": "0.4.0",
19
- "@types/bun": "1.3.3"
17
+ "@dion-js/config": "workspace:*",
18
+ "@dion-js/extension-types": "workspace:*",
19
+ "@types/bun": "catalog:"
20
20
  }
21
21
  }
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,18 +119,31 @@ 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
 
143
+ function assertWarn(condition: boolean, message: string): asserts condition {
144
+ if (!condition) console.warn(message);
145
+ }
146
+
105
147
  export async function assertValidM3U8(
106
148
  link: Link,
107
149
  options?: {
@@ -111,7 +153,7 @@ export async function assertValidM3U8(
111
153
  console.log(`Checking ${link.url}`);
112
154
  await ratelimit(time);
113
155
  const response = await fetch(link.url, { headers: link.header ?? undefined });
114
- assert(
156
+ assertWarn(
115
157
  response.ok,
116
158
  `${options?.assertmsg} ${link.url} is not a valid url or not reachable`,
117
159
  );
@@ -120,9 +162,9 @@ export async function assertValidM3U8(
120
162
  });
121
163
  parser.push(await response.text());
122
164
  parser.end();
123
- assert(
165
+ assertWarn(
124
166
  parser.manifest.segments.length > 0,
125
- `${options} ${link.url} is not a valid m3u8 url`,
167
+ `${options?.assertmsg} ${link.url} is not a valid m3u8 url`,
126
168
  );
127
169
  }
128
170
 
@@ -134,11 +176,11 @@ export async function assertValidImageURL(
134
176
  ) {
135
177
  await ratelimit(time);
136
178
  const response = await fetch(link.url, { headers: link.header ?? undefined });
137
- assert(
179
+ assertWarn(
138
180
  response.ok,
139
181
  `${options?.assertmsg} ${link.url} is not a valid url or not reachable`,
140
182
  );
141
- assert(
183
+ assertWarn(
142
184
  response.headers
143
185
  .get("content-type")
144
186
  ?.toLocaleLowerCase()
@@ -155,7 +197,7 @@ export async function assertValidURL(
155
197
  ) {
156
198
  await ratelimit(time);
157
199
  const response = await fetch(link.url, { headers: link.header ?? undefined });
158
- assert(
200
+ assertWarn(
159
201
  response.ok,
160
202
  `${options?.assertmsg} ${link.url} is not a valid url or not reachable`,
161
203
  );
@@ -199,27 +241,41 @@ export async function assertValidEntry(entry: Entry | EntryDetailed) {
199
241
  }
200
242
  }
201
243
  if ("ui" in entry && entry.ui !== undefined && entry.ui !== null) {
202
- assertValidUI(entry.ui);
244
+ await assertValidUI(entry.ui);
203
245
  }
204
246
  }
205
247
 
206
- function assertValidUI(ui: CustomUI) {
248
+ async function assertValidUI(ui: CustomUI) {
207
249
  if ("children" in ui && ui.children !== undefined && ui.children !== null) {
208
250
  for (const child of ui.children) {
209
- 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);
210
266
  }
211
267
  }
212
268
  switch (ui.type) {
213
269
  case "EntryCard":
214
- assertValidEntry(ui.entry);
270
+ await assertValidEntry(ui.entry);
215
271
  break;
216
272
  case "Image":
217
- assertValidImageURL(ui.image, {
273
+ await assertValidImageURL(ui.image, {
218
274
  assertmsg: `CustomUI has an invalid image url ${ui.image}`,
219
275
  });
220
276
  break;
221
277
  case "Link":
222
- assertValidURL(
278
+ await assertValidURL(
223
279
  { url: ui.link },
224
280
  {
225
281
  assertmsg: `CustomUI has an invalid link ${ui.link}`,
@@ -274,6 +330,26 @@ export async function assertValidSource(source: Source) {
274
330
  });
275
331
  }
276
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;
277
353
  }
278
354
  }
279
355