|
| 1 | +import assert from "node:assert"; |
| 2 | +import sinon from "sinon"; |
| 3 | +import * as scraper from "../../../../src/core/scraper/srf.js"; |
| 4 | + |
| 5 | +describe("core/scraper/srf.js", function () { |
| 6 | + describe("extract()", function () { |
| 7 | + it("should return null when it's a unsupported URL", async function () { |
| 8 | + const url = new URL("https://www.srf.ch/foo"); |
| 9 | + |
| 10 | + const file = await scraper.extract(url); |
| 11 | + assert.strictEqual(file, null); |
| 12 | + }); |
| 13 | + |
| 14 | + it("should return null when there isn't urn", async function () { |
| 15 | + const url = new URL("https://www.srf.ch/play/tv/foo"); |
| 16 | + |
| 17 | + const file = await scraper.extract(url); |
| 18 | + assert.strictEqual(file, null); |
| 19 | + }); |
| 20 | + |
| 21 | + it("should return null when urn is invalid", async function () { |
| 22 | + const stub = sinon.stub(globalThis, "fetch").resolves(new Response( |
| 23 | + JSON.stringify({ status: "foo" }), |
| 24 | + )); |
| 25 | + |
| 26 | + const url = new URL("https://www.srf.ch/play/tv/bar?urn=baz"); |
| 27 | + |
| 28 | + const file = await scraper.extract(url); |
| 29 | + assert.strictEqual(file, null); |
| 30 | + |
| 31 | + assert.strictEqual(stub.callCount, 1); |
| 32 | + assert.deepStrictEqual(stub.firstCall.args, [ |
| 33 | + "https://il.srgssr.ch/integrationlayer/2.0/mediaComposition" + |
| 34 | + "/byUrn/baz", |
| 35 | + ]); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should return video URL", async function () { |
| 39 | + const stub = sinon.stub(globalThis, "fetch").resolves(new Response( |
| 40 | + JSON.stringify({ |
| 41 | + chapterList: [{ |
| 42 | + resourceList: [{ |
| 43 | + analyticsMetadata: { |
| 44 | + // eslint-disable-next-line camelcase |
| 45 | + media_url: "http://foo.ch/bar.m3u8", |
| 46 | + }, |
| 47 | + }], |
| 48 | + }], |
| 49 | + }), |
| 50 | + )); |
| 51 | + |
| 52 | + const url = new URL("https://www.srf.ch/play/tv/bar?urn=baz"); |
| 53 | + |
| 54 | + const file = await scraper.extract(url); |
| 55 | + assert.strictEqual(file, "http://foo.ch/bar.m3u8"); |
| 56 | + |
| 57 | + assert.strictEqual(stub.callCount, 1); |
| 58 | + assert.deepStrictEqual(stub.firstCall.args, [ |
| 59 | + "https://il.srgssr.ch/integrationlayer/2.0/mediaComposition" + |
| 60 | + "/byUrn/baz", |
| 61 | + ]); |
| 62 | + }); |
| 63 | + }); |
| 64 | +}); |
0 commit comments