@editframe/elements 0.18.19-beta.0 → 0.18.20-beta.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.
@@ -1,6 +1,6 @@
1
1
  import { LitElement } from "lit";
2
2
  import { customElement } from "lit/decorators/custom-element.js";
3
- import { describe, expect, test, vi } from "vitest";
3
+ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
4
4
 
5
5
  import { ContextMixin } from "./ContextMixin.js";
6
6
 
@@ -10,9 +10,17 @@ import "../elements/EFTimegroup.js";
10
10
  @customElement("test-context")
11
11
  class TestContext extends ContextMixin(LitElement) {}
12
12
 
13
+ @customElement("test-context-reactivity")
14
+ class TestContextElement extends ContextMixin(LitElement) {
15
+ render() {
16
+ return document.createElement("ef-timegroup");
17
+ }
18
+ }
19
+
13
20
  declare global {
14
21
  interface HTMLElementTagNameMap {
15
22
  "test-context": TestContext;
23
+ "test-context-reactivity": TestContextElement;
16
24
  }
17
25
  }
18
26
 
@@ -21,6 +29,482 @@ describe("ContextMixin", () => {
21
29
  expect(ContextMixin).toBeDefined();
22
30
  });
23
31
 
32
+ describe("Token age checking", () => {
33
+ let element: TestContext & { fetch: typeof fetch };
34
+ let mockFetch: any;
35
+ let originalFetch: any;
36
+
37
+ // Helper to create a JWT token with specific expiration time
38
+ const createJWTToken = (
39
+ expirationMs: number,
40
+ issuedAtMs?: number,
41
+ ): string => {
42
+ const iat = issuedAtMs
43
+ ? Math.floor(issuedAtMs / 1000)
44
+ : Math.floor(Date.now() / 1000);
45
+ const header = btoa(JSON.stringify({ typ: "JWT", alg: "HS256" }));
46
+ const payload = btoa(
47
+ JSON.stringify({
48
+ type: "url",
49
+ url: "test-url",
50
+ exp: Math.floor(expirationMs / 1000), // JWT exp is in seconds
51
+ iat: iat,
52
+ }),
53
+ );
54
+ const signature = "mock-signature";
55
+ return `${header}.${payload}.${signature}`;
56
+ };
57
+
58
+ beforeEach(() => {
59
+ originalFetch = window.fetch;
60
+ mockFetch = vi.fn();
61
+ window.fetch = mockFetch;
62
+
63
+ element = document.createElement("test-context") as TestContext & {
64
+ fetch: typeof fetch;
65
+ };
66
+ element.signingURL = "https://test.com/api/v1/url-token";
67
+ document.body.appendChild(element);
68
+
69
+ vi.useFakeTimers();
70
+ });
71
+
72
+ afterEach(() => {
73
+ if (element.parentNode) {
74
+ document.body.removeChild(element);
75
+ }
76
+ window.fetch = originalFetch;
77
+ vi.useRealTimers();
78
+ });
79
+
80
+ test("should fetch new token when none exists", async () => {
81
+ const futureTime = Date.now() + 60 * 60 * 1000; // 1 hour from now
82
+ const token = createJWTToken(futureTime);
83
+
84
+ mockFetch.mockResolvedValueOnce({
85
+ ok: true,
86
+ json: () => Promise.resolve({ token }),
87
+ });
88
+
89
+ mockFetch.mockResolvedValueOnce({
90
+ ok: true,
91
+ text: () => Promise.resolve("success"),
92
+ });
93
+
94
+ await element.fetch("https://example.com/media.mp4");
95
+
96
+ expect(mockFetch).toHaveBeenCalledWith(
97
+ "https://test.com/api/v1/url-token",
98
+ {
99
+ method: "POST",
100
+ body: JSON.stringify({ url: "https://example.com/media.mp4" }),
101
+ },
102
+ );
103
+
104
+ expect(mockFetch).toHaveBeenCalledWith("https://example.com/media.mp4", {
105
+ headers: {
106
+ "Content-Type": "application/json",
107
+ authorization: `Bearer ${token}`,
108
+ },
109
+ });
110
+ }, 1000);
111
+
112
+ test("should reuse cached token when fresh", async () => {
113
+ const futureTime = Date.now() + 60 * 60 * 1000; // 1 hour from now
114
+ const token = createJWTToken(futureTime);
115
+
116
+ mockFetch.mockResolvedValueOnce({
117
+ ok: true,
118
+ json: () => Promise.resolve({ token }),
119
+ });
120
+
121
+ mockFetch.mockResolvedValueOnce({
122
+ ok: true,
123
+ text: () => Promise.resolve("success1"),
124
+ });
125
+
126
+ await element.fetch("https://example.com/media.mp4");
127
+
128
+ // Advance time by 30 minutes (token still valid for 30 more minutes)
129
+ vi.advanceTimersByTime(30 * 60 * 1000);
130
+
131
+ mockFetch.mockResolvedValueOnce({
132
+ ok: true,
133
+ text: () => Promise.resolve("success2"),
134
+ });
135
+
136
+ await element.fetch("https://example.com/media.mp4");
137
+
138
+ // Should only call signing URL once
139
+ expect(mockFetch).toHaveBeenCalledTimes(3); // 1 signing + 2 media requests
140
+ }, 1000);
141
+
142
+ test("should fetch new token when cached token is expired", async () => {
143
+ const now = Date.now();
144
+ const issuedAt = now;
145
+ const expiresAt = now + 30 * 60 * 1000; // 30 minutes from now
146
+
147
+ // For a 30-minute token: 10% = 3 minutes buffer (smaller than 5 minutes)
148
+ // Token will be considered expired at 27 minutes
149
+ const token1 = createJWTToken(expiresAt, issuedAt);
150
+
151
+ mockFetch.mockResolvedValueOnce({
152
+ ok: true,
153
+ json: () => Promise.resolve({ token: token1 }),
154
+ });
155
+
156
+ mockFetch.mockResolvedValueOnce({
157
+ ok: true,
158
+ text: () => Promise.resolve("success1"),
159
+ });
160
+
161
+ await element.fetch("https://example.com/media.mp4");
162
+
163
+ // Advance time by 28 minutes (past the 27-minute buffer threshold)
164
+ vi.advanceTimersByTime(28 * 60 * 1000);
165
+
166
+ const newExpiry = Date.now() + 60 * 60 * 1000; // 1 hour from current time
167
+ const token2 = createJWTToken(newExpiry);
168
+
169
+ mockFetch.mockResolvedValueOnce({
170
+ ok: true,
171
+ json: () => Promise.resolve({ token: token2 }),
172
+ });
173
+
174
+ mockFetch.mockResolvedValueOnce({
175
+ ok: true,
176
+ text: () => Promise.resolve("success2"),
177
+ });
178
+
179
+ await element.fetch("https://example.com/media.mp4");
180
+
181
+ // Should call signing URL twice
182
+ expect(mockFetch).toHaveBeenCalledTimes(4); // 2 signing + 2 media requests
183
+
184
+ expect(mockFetch).toHaveBeenCalledWith(
185
+ "https://test.com/api/v1/url-token",
186
+ {
187
+ method: "POST",
188
+ body: JSON.stringify({ url: "https://example.com/media.mp4" }),
189
+ },
190
+ );
191
+
192
+ expect(mockFetch).toHaveBeenCalledWith("https://example.com/media.mp4", {
193
+ headers: {
194
+ "Content-Type": "application/json",
195
+ authorization: `Bearer ${token2}`,
196
+ },
197
+ });
198
+ }, 1000);
199
+
200
+ test("should handle different URLs with separate token expiry", async () => {
201
+ const url1 = "https://example.com/media1.mp4";
202
+ const url2 = "https://example.com/media2.mp4";
203
+
204
+ const startTime = Date.now();
205
+
206
+ // URL1 gets a 30-minute token (10% buffer = 3 min, so expires at 27 min)
207
+ const url1IssuedAt = startTime;
208
+ const url1ExpiresAt = startTime + 30 * 60 * 1000; // 30 minutes
209
+ const token1 = createJWTToken(url1ExpiresAt, url1IssuedAt);
210
+
211
+ mockFetch.mockResolvedValueOnce({
212
+ ok: true,
213
+ json: () => Promise.resolve({ token: token1 }),
214
+ });
215
+ mockFetch.mockResolvedValueOnce({
216
+ ok: true,
217
+ text: () => Promise.resolve("success1"),
218
+ });
219
+
220
+ await element.fetch(url1);
221
+
222
+ // Advance time by 15 minutes
223
+ vi.advanceTimersByTime(15 * 60 * 1000);
224
+
225
+ // URL2 gets a 60-minute token (5 min buffer is smaller than 10% = 6 min, so expires at 55 min)
226
+ const url2IssuedAt = Date.now();
227
+ const url2ExpiresAt = url2IssuedAt + 60 * 60 * 1000; // 60 minutes from current time
228
+ const token2 = createJWTToken(url2ExpiresAt, url2IssuedAt);
229
+
230
+ mockFetch.mockResolvedValueOnce({
231
+ ok: true,
232
+ json: () => Promise.resolve({ token: token2 }),
233
+ });
234
+ mockFetch.mockResolvedValueOnce({
235
+ ok: true,
236
+ text: () => Promise.resolve("success2"),
237
+ });
238
+
239
+ await element.fetch(url2);
240
+
241
+ // Advance time by another 15 minutes (total 30 min from start)
242
+ // URL1 token: 30 min from start, past its 27-min threshold → expired
243
+ // URL2 token: 15 min from its creation, well within its 55-min threshold → still valid
244
+ vi.advanceTimersByTime(15 * 60 * 1000);
245
+
246
+ // Request URL1 again (should get new token - expired)
247
+ const url1NewExpiry = Date.now() + 60 * 60 * 1000;
248
+ const token3 = createJWTToken(url1NewExpiry);
249
+
250
+ mockFetch.mockResolvedValueOnce({
251
+ ok: true,
252
+ json: () => Promise.resolve({ token: token3 }),
253
+ });
254
+ mockFetch.mockResolvedValueOnce({
255
+ ok: true,
256
+ text: () => Promise.resolve("success3"),
257
+ });
258
+
259
+ await element.fetch(url1);
260
+
261
+ // Request URL2 again (should reuse token - still valid)
262
+ mockFetch.mockResolvedValueOnce({
263
+ ok: true,
264
+ text: () => Promise.resolve("success4"),
265
+ });
266
+
267
+ await element.fetch(url2);
268
+
269
+ expect(mockFetch).toHaveBeenCalledTimes(7); // 3 signing + 4 media requests (URL2 reused token)
270
+ }, 1000);
271
+
272
+ test("should clear token cache on component disconnect", async () => {
273
+ const futureTime = Date.now() + 60 * 60 * 1000; // 1 hour from now
274
+ const token1 = createJWTToken(futureTime);
275
+
276
+ mockFetch.mockResolvedValueOnce({
277
+ ok: true,
278
+ json: () => Promise.resolve({ token: token1 }),
279
+ });
280
+
281
+ mockFetch.mockResolvedValueOnce({
282
+ ok: true,
283
+ text: () => Promise.resolve("success"),
284
+ });
285
+
286
+ await element.fetch("https://example.com/media.mp4");
287
+
288
+ if (element.parentNode) {
289
+ document.body.removeChild(element);
290
+ }
291
+
292
+ // Create new element and add back to DOM
293
+ element = document.createElement("test-context") as TestContext & {
294
+ fetch: typeof fetch;
295
+ };
296
+ element.signingURL = "https://test.com/api/v1/url-token";
297
+ document.body.appendChild(element);
298
+
299
+ const token2 = createJWTToken(futureTime);
300
+
301
+ mockFetch.mockResolvedValueOnce({
302
+ ok: true,
303
+ json: () => Promise.resolve({ token: token2 }),
304
+ });
305
+
306
+ mockFetch.mockResolvedValueOnce({
307
+ ok: true,
308
+ text: () => Promise.resolve("success2"),
309
+ });
310
+
311
+ await element.fetch("https://example.com/media.mp4");
312
+
313
+ expect(mockFetch).toHaveBeenCalledTimes(4); // 2 signing + 2 media requests
314
+ }, 1000);
315
+
316
+ test("should use single token for multiple transcode segments with same source URL", async () => {
317
+ const futureTime = Date.now() + 60 * 60 * 1000;
318
+ const token = createJWTToken(futureTime);
319
+
320
+ const sourceUrl = "https://example.com/source-video.mp4";
321
+ const segment1 = `https://editframe.dev/api/v1/transcode/video-1080p/1.mp4?url=${encodeURIComponent(sourceUrl)}`;
322
+ const segment2 = `https://editframe.dev/api/v1/transcode/video-1080p/2.mp4?url=${encodeURIComponent(sourceUrl)}`;
323
+ const segment3 = `https://editframe.dev/api/v1/transcode/audio-44100/1.m4a?url=${encodeURIComponent(sourceUrl)}`;
324
+
325
+ // Should only sign once for the base URL + params combination
326
+ mockFetch.mockResolvedValueOnce({
327
+ ok: true,
328
+ json: () => Promise.resolve({ token }),
329
+ });
330
+
331
+ // Mock all segment requests
332
+ mockFetch.mockResolvedValueOnce({
333
+ ok: true,
334
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
335
+ });
336
+ mockFetch.mockResolvedValueOnce({
337
+ ok: true,
338
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
339
+ });
340
+ mockFetch.mockResolvedValueOnce({
341
+ ok: true,
342
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
343
+ });
344
+
345
+ // Fetch multiple segments
346
+ await element.fetch(segment1);
347
+ await element.fetch(segment2);
348
+ await element.fetch(segment3);
349
+
350
+ // Should only call signing URL once + 3 segment requests
351
+ expect(mockFetch).toHaveBeenCalledTimes(4);
352
+
353
+ // Verify the signing request used base URL + params format
354
+ expect(mockFetch).toHaveBeenCalledWith(
355
+ "https://test.com/api/v1/url-token",
356
+ {
357
+ method: "POST",
358
+ body: JSON.stringify({
359
+ url: "https://editframe.dev/api/v1/transcode",
360
+ params: { url: sourceUrl },
361
+ }),
362
+ },
363
+ );
364
+
365
+ // All segment requests should use the same token
366
+ expect(mockFetch).toHaveBeenCalledWith(segment1, {
367
+ headers: {
368
+ "Content-Type": "application/json",
369
+ authorization: `Bearer ${token}`,
370
+ },
371
+ });
372
+ expect(mockFetch).toHaveBeenCalledWith(segment2, {
373
+ headers: {
374
+ "Content-Type": "application/json",
375
+ authorization: `Bearer ${token}`,
376
+ },
377
+ });
378
+ expect(mockFetch).toHaveBeenCalledWith(segment3, {
379
+ headers: {
380
+ "Content-Type": "application/json",
381
+ authorization: `Bearer ${token}`,
382
+ },
383
+ });
384
+ }, 1000);
385
+
386
+ test("should use different tokens for transcode segments with different source URLs", async () => {
387
+ const futureTime = Date.now() + 60 * 60 * 1000;
388
+ const token1 = createJWTToken(futureTime);
389
+ const token2 = createJWTToken(futureTime);
390
+
391
+ const sourceUrl1 = "https://example.com/video1.mp4";
392
+ const sourceUrl2 = "https://example.com/video2.mp4";
393
+
394
+ const segment1A = `https://editframe.dev/api/v1/transcode/video-1080p/1.mp4?url=${encodeURIComponent(sourceUrl1)}`;
395
+ const segment1B = `https://editframe.dev/api/v1/transcode/video-1080p/2.mp4?url=${encodeURIComponent(sourceUrl1)}`;
396
+ const segment2A = `https://editframe.dev/api/v1/transcode/video-720p/1.mp4?url=${encodeURIComponent(sourceUrl2)}`;
397
+
398
+ // First source URL token request
399
+ mockFetch.mockResolvedValueOnce({
400
+ ok: true,
401
+ json: () => Promise.resolve({ token: token1 }),
402
+ });
403
+
404
+ mockFetch.mockResolvedValueOnce({
405
+ ok: true,
406
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
407
+ });
408
+ mockFetch.mockResolvedValueOnce({
409
+ ok: true,
410
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
411
+ });
412
+
413
+ // Fetch segments for first source
414
+ await element.fetch(segment1A);
415
+ await element.fetch(segment1B);
416
+
417
+ // Second source URL token request
418
+ mockFetch.mockResolvedValueOnce({
419
+ ok: true,
420
+ json: () => Promise.resolve({ token: token2 }),
421
+ });
422
+
423
+ mockFetch.mockResolvedValueOnce({
424
+ ok: true,
425
+ arrayBuffer: () => Promise.resolve(new ArrayBuffer(1024)),
426
+ });
427
+
428
+ // Fetch segment for second source
429
+ await element.fetch(segment2A);
430
+
431
+ // Should call signing URL twice + 3 segment requests
432
+ expect(mockFetch).toHaveBeenCalledTimes(5);
433
+
434
+ // Verify separate signing requests
435
+ expect(mockFetch).toHaveBeenCalledWith(
436
+ "https://test.com/api/v1/url-token",
437
+ {
438
+ method: "POST",
439
+ body: JSON.stringify({
440
+ url: "https://editframe.dev/api/v1/transcode",
441
+ params: { url: sourceUrl1 },
442
+ }),
443
+ },
444
+ );
445
+ expect(mockFetch).toHaveBeenCalledWith(
446
+ "https://test.com/api/v1/url-token",
447
+ {
448
+ method: "POST",
449
+ body: JSON.stringify({
450
+ url: "https://editframe.dev/api/v1/transcode",
451
+ params: { url: sourceUrl2 },
452
+ }),
453
+ },
454
+ );
455
+ }, 1000);
456
+
457
+ test("should still sign individual URLs for non-transcode endpoints", async () => {
458
+ const futureTime = Date.now() + 60 * 60 * 1000;
459
+ const token1 = createJWTToken(futureTime);
460
+ const token2 = createJWTToken(futureTime);
461
+
462
+ const regularUrl1 = "https://example.com/api/v1/media/123";
463
+ const regularUrl2 = "https://example.com/api/v1/assets/456";
464
+
465
+ // First URL signing
466
+ mockFetch.mockResolvedValueOnce({
467
+ ok: true,
468
+ json: () => Promise.resolve({ token: token1 }),
469
+ });
470
+ mockFetch.mockResolvedValueOnce({
471
+ ok: true,
472
+ text: () => Promise.resolve("success1"),
473
+ });
474
+
475
+ // Second URL signing
476
+ mockFetch.mockResolvedValueOnce({
477
+ ok: true,
478
+ json: () => Promise.resolve({ token: token2 }),
479
+ });
480
+ mockFetch.mockResolvedValueOnce({
481
+ ok: true,
482
+ text: () => Promise.resolve("success2"),
483
+ });
484
+
485
+ await element.fetch(regularUrl1);
486
+ await element.fetch(regularUrl2);
487
+
488
+ expect(mockFetch).toHaveBeenCalledTimes(4); // 2 signing + 2 requests
489
+
490
+ // Should sign each URL individually (existing behavior)
491
+ expect(mockFetch).toHaveBeenCalledWith(
492
+ "https://test.com/api/v1/url-token",
493
+ {
494
+ method: "POST",
495
+ body: JSON.stringify({ url: regularUrl1 }),
496
+ },
497
+ );
498
+ expect(mockFetch).toHaveBeenCalledWith(
499
+ "https://test.com/api/v1/url-token",
500
+ {
501
+ method: "POST",
502
+ body: JSON.stringify({ url: regularUrl2 }),
503
+ },
504
+ );
505
+ }, 1000);
506
+ });
507
+
24
508
  describe("Playback", () => {
25
509
  test("should start playback", () => {
26
510
  const element = document.createElement("test-context");
@@ -73,4 +557,84 @@ describe("ContextMixin", () => {
73
557
  const event = await timeupdatePromise;
74
558
  expect(event.detail.currentTimeMs).toBe(1000);
75
559
  });
560
+
561
+ describe("Reactivity", () => {
562
+ test("should update durationMs when child tree changes", async () => {
563
+ const element = document.createElement("test-context-reactivity");
564
+ document.body.appendChild(element);
565
+
566
+ // Wait for initial render
567
+ await element.updateComplete;
568
+
569
+ // Create a timegroup with initial duration
570
+ const timegroup = document.createElement("ef-timegroup");
571
+ timegroup.mode = "contain";
572
+
573
+ const child = document.createElement("ef-timegroup");
574
+ child.mode = "fixed";
575
+ child.duration = "5s";
576
+ timegroup.appendChild(child);
577
+
578
+ element.appendChild(timegroup);
579
+
580
+ // Wait for updates
581
+ await element.updateComplete;
582
+ await timegroup.updateComplete;
583
+
584
+ // Initially, the timegroup should have 5s duration
585
+ expect(element.targetTimegroup?.durationMs).toBe(5000);
586
+
587
+ // Now change the child duration
588
+ child.duration = "10s";
589
+
590
+ // Wait for updates
591
+ await element.updateComplete;
592
+ await timegroup.updateComplete;
593
+
594
+ // The targetTimegroup should now have 10s duration
595
+ expect(element.targetTimegroup?.durationMs).toBe(10000);
596
+
597
+ document.body.removeChild(element);
598
+ });
599
+
600
+ test("should update when media elements are added/removed", async () => {
601
+ const element = document.createElement("test-context-reactivity");
602
+ document.body.appendChild(element);
603
+
604
+ await element.updateComplete;
605
+
606
+ // Create initial timegroup with 5s duration
607
+ const timegroup = document.createElement("ef-timegroup");
608
+ timegroup.mode = "contain";
609
+
610
+ const child = document.createElement("ef-timegroup");
611
+ child.mode = "fixed";
612
+ child.duration = "5s";
613
+ timegroup.appendChild(child);
614
+
615
+ element.appendChild(timegroup);
616
+
617
+ await element.updateComplete;
618
+ await timegroup.updateComplete;
619
+
620
+ // Initially duration should be 5s
621
+ expect(element.targetTimegroup?.durationMs).toBe(5000);
622
+
623
+ // Add a new child with longer duration
624
+ const newChild = document.createElement("ef-timegroup");
625
+ newChild.mode = "fixed";
626
+ newChild.duration = "15s";
627
+ timegroup.appendChild(newChild);
628
+
629
+ // Wait for updates
630
+ await element.updateComplete;
631
+ await timegroup.updateComplete;
632
+ await newChild.updateComplete;
633
+
634
+ // Duration should now be 15s (the max of all children)
635
+ expect(element.targetTimegroup?.durationMs).toBe(15000);
636
+
637
+ document.body.removeChild(element);
638
+ });
639
+ });
76
640
  });