fix: headendId when OTA, add tests

This commit is contained in:
Jef LeCompte
2025-07-21 22:44:27 -07:00
parent ad7aa8e581
commit 1696b15712
2 changed files with 46 additions and 2 deletions

40
src/config.test.ts Normal file
View File

@@ -0,0 +1,40 @@
import { describe, it, expect } from "vitest";
import { processLineupId, getHeadendId } from "./config.js";
describe("processLineupId", () => {
it("returns env LINEUP_ID if set", () => {
process.env.LINEUP_ID = "USA-12345";
expect(processLineupId()).toBe("USA-12345");
delete process.env.LINEUP_ID;
});
it("returns argv --lineupId if set", () => {
process.argv.push("--lineupId=USA-54321");
expect(processLineupId()).toBe("USA-54321");
process.argv = process.argv.filter((arg) => !arg.startsWith("--lineupId="));
});
it("returns default if nothing set", () => {
expect(processLineupId()).toBe("USA-lineupId-DEFAULT");
});
it("returns default if lineupId contains OTA", () => {
process.env.LINEUP_ID = "USA-OTA12345";
expect(processLineupId()).toBe("USA-lineupId-DEFAULT");
delete process.env.LINEUP_ID;
});
});
describe("getHeadendId", () => {
it("extracts headend from valid lineupId", () => {
expect(getHeadendId("USA-OTA12345")).toBe("lineupId");
expect(getHeadendId("USA-NY31587-L")).toBe("NY31587");
expect(getHeadendId("CAN-OTAT1L0A1")).toBe("lineupId");
expect(getHeadendId("CAN-0008861-X")).toBe("0008861");
});
it("returns 'lineup' if no match", () => {
expect(getHeadendId("INVALID")).toBe("lineup");
expect(getHeadendId("")).toBe("lineup");
});
});

View File

@@ -1,6 +1,6 @@
import { UserAgent } from "./useragents.js";
function processLineupId(): string {
export function processLineupId(): string {
const lineupId =
process.env["LINEUP_ID"] ||
process.argv.find((arg) => arg.startsWith("--lineupId="))?.split("=")[1] ||
@@ -13,7 +13,11 @@ function processLineupId(): string {
return lineupId;
}
function getHeadendId(lineupId: string): string {
export function getHeadendId(lineupId: string): string {
if (lineupId.includes("OTA")) {
return "lineupId";
}
const match = lineupId.match(/^(USA|CAN)-(.*?)(?:-[A-Z]+)?$/);
return match?.[2] || "lineup";