media-fragment-identifier.test.js 1.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import test from 'ava';
  2. import {
  3. createMediaFragmentIdentifier,
  4. parseMediaFragmentIdentifier,
  5. } from './media-fragment-identifier.js';
  6. const creatorResultInputPairs = {
  7. 't=12.345,67.89': { start: 12.345, end: 67.89 },
  8. 't=,67.89': { end: 67.89 },
  9. 't=0,67.89': { start: 0, end: 67.89 },
  10. 't=12.345': { start: 12.345 },
  11. };
  12. const parserInputResultPairs = {
  13. 't=1,2': { start: 1, end: 2 },
  14. 't=npt:1,2': { start: 1, end: 2 },
  15. 't=,2': { start: 0, end: 2 },
  16. 't=1': { start: 1, end: undefined },
  17. 't=1.234': { start: 1.234, end: undefined },
  18. 't=1.234,5.678': { start: 1.234, end: 5.678 },
  19. 't=1:2': { start: 62, end: undefined },
  20. 't=1:2.5': { start: 62.5, end: undefined },
  21. 't=1:2:3.4': { start: 3723.4, end: undefined },
  22. 't=20,1:2:3.4': { start: 20, end: 3723.4 },
  23. 't=20.,1:2.': { start: 20, end: 62 },
  24. 'track=audio&t=10,20': { start: 10, end: 20 },
  25. 't=10,20&track=audio': { start: 10, end: 20 },
  26. 't=1,2&t=3': { start: 3, end: undefined },
  27. // Below are examples from the spec.
  28. 't=npt:10,20': { start: 10, end: 20 },
  29. 't=npt:,121.5': { start: 0, end: 121.5 },
  30. 't=0:02:00,121.5': { start: 120, end: 121.5 },
  31. 't=npt:120,0:02:01.5': { start: 120, end: 121.5 },
  32. '%74=10,20': { start: 10, end: 20 },
  33. 't=%31%30': { start: 10, end: undefined },
  34. 't=10%2C20': { start: 10, end: 20 },
  35. 't=%6ept:10': { start: 10, end: undefined },
  36. 't=npt%3a10': { start: 10, end: undefined },
  37. };
  38. // Test creating.
  39. for (const [expectedResult, input] of Object.entries(creatorResultInputPairs)) {
  40. test(`Correctly create fragment '${expectedResult}'`, t => {
  41. t.is(createMediaFragmentIdentifier(input), expectedResult);
  42. });
  43. }
  44. // Test parsing.
  45. for (const [input, expectedResult] of Object.entries(parserInputResultPairs)) {
  46. test(`Correctly parse input '${input}'`, t => {
  47. t.deepEqual(parseMediaFragmentIdentifier(input), expectedResult);
  48. });
  49. }