-
Notifications
You must be signed in to change notification settings - Fork 2
adds integration test check #430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { readFile, readdir } from "node:fs/promises"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { expect, test } from "vitest"; | ||
| import { z } from "zod"; | ||
|
|
||
| import { manualFixturesSchema, type ManualFixturesType } from "@/content.types"; | ||
|
|
||
| const isIsoDate = (value: string): boolean => z.iso.date().safeParse(value).success; | ||
|
|
||
| const fixturesDir = fileURLToPath( | ||
| new URL("./manual-fixtures", import.meta.url), | ||
| ); | ||
|
|
||
| const fixtureFileNames = async (): Promise<string[]> => { | ||
| const files = (await readdir(fixturesDir, { recursive: true })).filter( | ||
| (name) => name.endsWith(".json"), | ||
| ); | ||
| return files.sort(); | ||
| }; | ||
|
|
||
| const loadManualFixtures = async (): Promise<ManualFixturesType[]> => { | ||
| const fixtures: ManualFixturesType[] = []; | ||
| for (const file of await fixtureFileNames()) { | ||
| const raw = await readFile(`${fixturesDir}/${file}`, "utf8"); | ||
| fixtures.push(manualFixturesSchema.parse(JSON.parse(raw))); | ||
| } | ||
| return fixtures; | ||
| }; | ||
|
|
||
| const referencedImageUrls = (fixture: ManualFixturesType): string[] => { | ||
| const urls: string[] = []; | ||
| for (const match of fixture.matches) { | ||
| urls.push(match.area.flag, match.competition.emblem); | ||
| urls.push(match.homeTeam.crest, match.awayTeam.crest); | ||
| } | ||
| return urls; | ||
| }; | ||
|
|
||
| const knownBrokenImageUrls = new Set([ | ||
| "https://crests.football-data.org/FAC.png", | ||
| "https://crests.football-data.org/CARABAO_CUP.png", | ||
| ]); | ||
|
|
||
| test("each manual fixture file is a valid, parseable JSON document", async () => { | ||
| const files = await fixtureFileNames(); | ||
|
|
||
| expect(files.length).toBeGreaterThan(0); | ||
|
|
||
| for (const file of files) { | ||
| const raw = await readFile(`${fixturesDir}/${file}`, "utf8"); | ||
| expect(() => JSON.parse(raw), `${file} must be valid JSON`).not.toThrow(); | ||
| } | ||
| }); | ||
|
|
||
| test("each manual fixture filename is a valid date that ids the API route", async () => { | ||
| const files = await fixtureFileNames(); | ||
|
|
||
| for (const file of files) { | ||
| const date = file.replace(/\.json$/, ""); | ||
| expect(isIsoDate(date), `${file} must be named by a valid date`).toBe(true); | ||
| expect(date.length).toBe(10); | ||
| } | ||
| }); | ||
|
|
||
| test("each manual fixture conforms to the content collection schema", async () => { | ||
| const fixtures = await loadManualFixtures(); | ||
|
|
||
| expect(fixtures.length).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| test("each manual fixture's match count agrees with its declared result set", async () => { | ||
| const files = await fixtureFileNames(); | ||
|
|
||
| for (const file of files) { | ||
| const raw = await readFile(`${fixturesDir}/${file}`, "utf8"); | ||
| const fixture = JSON.parse(raw) as { | ||
| resultSet: { count?: number }; | ||
| matches?: unknown[]; | ||
| }; | ||
|
|
||
| if (typeof fixture.resultSet?.count === "number") { | ||
| expect( | ||
| fixture.matches?.length, | ||
| `${file}: resultSet.count must match the matches array length`, | ||
| ).toBe(fixture.resultSet.count); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("every referenced image URL is reachable", async () => { | ||
| const fixtures = await loadManualFixtures(); | ||
|
|
||
| const urls = new Set( | ||
| fixtures | ||
| .flatMap(referencedImageUrls) | ||
| .filter((url) => !knownBrokenImageUrls.has(url)), | ||
| ); | ||
| expect(urls.size).toBeGreaterThan(0); | ||
|
|
||
| const checkUrlReachability = async (url: string) => { | ||
| try { | ||
| const response = await fetch(url, { | ||
| signal: AbortSignal.timeout(15_000), | ||
| }); | ||
| return { url, reachable: response.ok, detail: response.statusText }; | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : String(error); | ||
| return { url, reachable: false, detail: message }; | ||
| } | ||
| }; | ||
|
|
||
| const results = await Promise.all([...urls].map(checkUrlReachability)); | ||
| const unreachable = results.filter((result) => !result.reachable); | ||
| const unreachableReport = unreachable.map( | ||
| (result) => `${result.url} (${result.detail})`, | ||
| ); | ||
|
|
||
| expect(unreachableReport).toEqual([]); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| import * as fs from "fs"; | ||
| import { mkdir, writeFile } from "node:fs/promises"; | ||
| import { dirname } from "node:path"; | ||
|
|
||
| export const stringifyToFile = async (filePath: string, data: any) => { | ||
| // Convert the JavaScript object to a JSON string | ||
| // The 'null' and '2' arguments format the JSON with an indentation of 2 spaces, making it readable | ||
| export const stringifyToFile = async ( | ||
| filePath: string, | ||
| data: unknown, | ||
| ): Promise<void> => { | ||
| const jsonString = JSON.stringify(data, null, 2); | ||
|
|
||
| // TODO: await fs.writeFile | ||
| fs.writeFileSync(filePath, jsonString, "utf-8"); | ||
| await mkdir(dirname(filePath), { recursive: true }); | ||
| await writeFile(filePath, jsonString, "utf-8"); | ||
|
jtzero marked this conversation as resolved.
|
||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.