From 479f1052d1ab7f77ee8d9fa993995fd5157f7704 Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Wed, 3 Jan 2024 16:49:58 +0000 Subject: [PATCH] Add tests for pretalx --- .../backends/pretalx/PretalxBackend.test.ts | 104 +++++++++++ .../backends/pretalx/PretalxParser.test.ts | 40 +++++ src/__tests__/backends/pretalx/anyconf.json | 168 ++++++++++++++++++ 3 files changed, 312 insertions(+) create mode 100644 src/__tests__/backends/pretalx/PretalxBackend.test.ts create mode 100644 src/__tests__/backends/pretalx/PretalxParser.test.ts create mode 100644 src/__tests__/backends/pretalx/anyconf.json diff --git a/src/__tests__/backends/pretalx/PretalxBackend.test.ts b/src/__tests__/backends/pretalx/PretalxBackend.test.ts new file mode 100644 index 0000000..610b9e1 --- /dev/null +++ b/src/__tests__/backends/pretalx/PretalxBackend.test.ts @@ -0,0 +1,104 @@ +import { test, expect, afterEach, beforeEach, describe } from "@jest/globals"; +import { PretalxScheduleBackend } from "../../../backends/pretalx/PretalxBackend"; +import { Server, createServer } from "node:http"; +import { AddressInfo } from "node:net"; +import path from "node:path"; + +const pretalxSpeakers = [{ + code: "37RA83", + name: "AnyConf Staff", + biography: null, + submissions: [], + avatar: "", + answers: [], + email: "staff@anyconf.example.com", + availabilities: [], +},{ + code: "YT3EFD", + name: "Alice AnyConf", + biography: "Alice is a test user with a big robotic brain.", + submissions: [], + avatar: "", + answers: [], + email: "alice@anyconf.example.com", + availabilities: [], +}]; + +function fakePretalxServer() { + return new Promise(resolve => { const server = createServer((req, res) => { + if (req.url?.startsWith('/speakers/?')) { + res.writeHead(200); + res.end(JSON.stringify({ + count: pretalxSpeakers.length, + next: null, + previous: null, + results: pretalxSpeakers, + })); + } else if (req.url?.startsWith('/speakers/')) { + const speakerCode = req.url.slice('/speakers/'.length); + const speaker = pretalxSpeakers.find(s => s.code === speakerCode); + if (speaker) { + res.writeHead(200); + res.end(speaker); + } else { + res.writeHead(404); + res.end(`Speaker "${speakerCode}" not found`); + } + } else { + console.log(req.url); + res.writeHead(404); + res.end("Not found"); + } + }).listen(undefined, '127.0.0.1', undefined, () => { + resolve(server); + })}); +} + +const prefixConfig = { + // Unused here. + aliases: "", displayNameSuffixes: {}, suffixes: {}, physicalAuditoriumRooms: [], + + auditoriumRooms: [ + "AW1.", + "D.", + "H.", + "Janson", + "K.", + "M.", + "UA2.", + "UB2.252A", + "UB4.", + "UB5.", + "UD2." + ], + qaAuditoriumRooms: [ + "AQ.", + ], + interestRooms: [ + "I." + ], + nameOverrides: { + "A.special": "special-room", + }, +}; +describe('PretalxBackend', () => { + let pretalxServ; + beforeEach(async () => { + pretalxServ = await fakePretalxServer(); + }); + afterEach(async () => { + pretalxServ.close(); + + }); + test("can parse a standard JSON format", async () => { + const pretalxServ = await fakePretalxServer(); + const backend = await PretalxScheduleBackend.new("/dev/null", { + backend: "pretalx", + scheduleDefinition: path.join(__dirname, 'anyconf.json'), + pretalxAccessToken: "123456", + pretalxApiEndpoint: `http://localhost:${(pretalxServ.address() as AddressInfo).port}`, + }, prefixConfig); + expect(backend.conference.title).toBe('AnyConf 2024'); + expect(backend.conference.auditoriums).toHaveLength(7); + }); +}); diff --git a/src/__tests__/backends/pretalx/PretalxParser.test.ts b/src/__tests__/backends/pretalx/PretalxParser.test.ts new file mode 100644 index 0000000..975d458 --- /dev/null +++ b/src/__tests__/backends/pretalx/PretalxParser.test.ts @@ -0,0 +1,40 @@ +import path from "node:path"; +import { test, expect } from "@jest/globals"; +import { parseFromJSON } from "../../../backends/pretalx/PretalxParser"; +import { readFile } from "node:fs/promises"; +import { IPrefixConfig } from "../../../config"; + +const prefixConfig: IPrefixConfig = { + // Unused here. + aliases: "", displayNameSuffixes: {}, suffixes: {}, physicalAuditoriumRooms: [], + + auditoriumRooms: [ + "AW1.", + "D.", + "H.", + "Janson", + "K.", + "M.", + "UA2.", + "UB2.252A", + "UB4.", + "UB5.", + "UD2." + ], + qaAuditoriumRooms: [ + "AQ.", + ], + interestRooms: [ + "I." + ], + nameOverrides: { + "A.special": "special-room", + }, +}; + +test("can parse a standard XML format", async () => { + const xml = await readFile(path.join(__dirname, "anyconf.json"), 'utf-8'); + const { title, talks } = await parseFromJSON(xml, prefixConfig); + expect(talks.size).toBe(2); + expect(title).toBe('AnyConf 2024'); +}); diff --git a/src/__tests__/backends/pretalx/anyconf.json b/src/__tests__/backends/pretalx/anyconf.json new file mode 100644 index 0000000..69eae41 --- /dev/null +++ b/src/__tests__/backends/pretalx/anyconf.json @@ -0,0 +1,168 @@ +{ + "_note": "This a snapshot of the FOSDEM 2024 schedule schema, with fake data.", + "schedule": { + "version": "2024-01-02 20:35", + "base_url": "https://pretalx.example.com/conf/schedule/", + "conference": { + "acronym": "anyconf-2024", + "title": "AnyConf 2024", + "start": "2024-02-03", + "end": "2024-02-04", + "daysCount": 2, + "timeslot_duration": "00:05", + "time_zone_name": "Europe/Brussels", + "rooms": [ + { + "name": "janson", + "guid": null, + "description": "Janson", + "capacity": 1415 + }, + { + "name": "j1106", + "guid": null, + "description": "J.1.106", + "capacity": 70 + }, + { + "name": "k1105", + "guid": null, + "description": "K.1.105 (A room)", + "capacity": 805 + }, + { + "name": "h1301", + "guid": null, + "description": "H.1301 (Another room)", + "capacity": 189 + }, + { + "name": "aw1120", + "guid": null, + "description": "AW1.120", + "capacity": 74 + }, + { + "name": "ua2114", + "guid": null, + "description": "UA2.114 (Wow)", + "capacity": 207 + }, + { + "name": "ub2252a", + "guid": null, + "description": "UB2.252A (More)", + "capacity": 532 + }, + { + "name": "ud2120", + "guid": null, + "description": "UD2.120 (Rooms)", + "capacity": 446 + }, + { + "name": "a01", + "guid": null, + "description": "A.01 (Someroom)", + "capacity": 175 + } + ], + "days": [ + { + "index": 1, + "date": "2024-02-03", + "day_start": "2024-02-03T04:00:00+01:00", + "day_end": "2024-02-04T03:59:00+01:00", + "rooms":{ + "janson": [{ + "id": 1234, + "guid": "5bb0aa4e-0c0e-5ccd-9523-0ed1ca679039", + "logo": "", + "date": "2024-02-03T09:00:00+01:00", + "start": "09:00", + "duration": "00:25", + "room": "janson", + "slug": "welcome talk", + "url": "https://pretalx.example.com/conf/talk/GK99DE/", + "title": "Welcome to AnyConf 2024", + "subtitle": "", + "track": "Keynotes", + "type": "Talk", + "language": "en", + "abstract": "AnyConf welcome and opening talk.", + "description": null, + "recording_license": "", + "do_not_record": false, + "persons": [ + { + "id": 2, + "code": "37RA83", + "public_name": "AnyConf Staff", + "biography": null, + "answers": [] + }, + { + "id": 1324, + "code": "YT3EFD", + "public_name": "Alice AnyConf", + "biography": "Alice is a test user with a big robotic brain.", + "answers": [] + } + ], + "links": [], + "attachments": [], + "answers": [] + }] + } + }, + { + "index": 2, + "date": "2024-02-04", + "day_start": "2024-02-04T04:00:00+01:00", + "day_end": "2024-02-05T03:59:00+01:00", + "rooms":{ + "janson": [{ + "id": 1235, + "guid": "b8325b99-84af-45a7-806b-5819c79b6dc5", + "logo": "", + "date": "2024-02-03T09:00:00+01:00", + "start": "09:00", + "duration": "00:25", + "room": "janson", + "slug": "welcome talk", + "url": "https://pretalx.example.com/conf/talk/GK99DE/", + "title": "Welcome to AnyConf 2024", + "subtitle": "", + "track": "Keynotes", + "type": "Talk", + "language": "en", + "abstract": "AnyConf welcome and opening talk.", + "description": null, + "recording_license": "", + "do_not_record": false, + "persons": [ + { + "id": 2, + "code": "37RA83", + "public_name": "AnyConf Staff", + "biography": null, + "answers": [] + }, + { + "id": 1324, + "code": "YT3EFD", + "public_name": "Alice AnyConf", + "biography": "Alice is a test user with a big robotic brain.", + "answers": [] + } + ], + "links": [], + "attachments": [], + "answers": [] + }] + } + } + ] + } + } + } \ No newline at end of file