feat: initial prototype
Node.js CI / Lint and Test (push) Successful in 41s

This commit is contained in:
2025-03-06 17:41:16 -08:00
parent 8b4ccadcfe
commit e53db48154
16 changed files with 5348 additions and 15 deletions
+15
View File
@@ -0,0 +1,15 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, expect, it } from "vitest";
import { html } from "../src/config/html.ts";
describe("html", () => {
it(`should be a string`, () => {
expect.assertions(1);
expect(html, "what did you do").toBeTypeOf("string");
});
});
+83
View File
@@ -0,0 +1,83 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { describe, it, expect } from "vitest";
import { html } from "../src/config/html.ts";
import { mommy } from "../src/config/mommy.ts";
import { phrases } from "../src/config/phrases.ts";
import { main } from "../src/index.ts";
/* eslint-disable max-nested-callbacks, max-lines-per-function -- Not sure how to better optimise this. */
describe("fastify Server", () => {
it("should return HTML content on /", async() => {
expect.assertions(3);
const server = await main();
const response = await server.inject({
method: "GET",
url: "/",
});
expect(response.statusCode, "wrong status").toBe(200);
expect(response.headers["content-type"], "wrong content type").
toContain("text/html");
expect(response.body, "wrong response").toBe(html);
await server.close();
});
it("should return a formatted phrase on /api", async() => {
expect.assertions(3);
const server = await main();
const response = await server.inject({
method: "GET",
url: "/api",
});
expect(response.statusCode, "wrong status").toBe(200);
expect(response.headers["content-type"], "wrong content type").
toContain("text/plain");
const possibleResponses = phrases.flatMap((phrase) => {
return mommy.map((mom) => {
return phrase.
replaceAll("{{ name }}", "dear").
replaceAll("{{ mommy }}", mom).
toLowerCase();
});
});
expect(possibleResponses, "where did it come from").
toContain(response.body);
await server.close();
});
it("should return a formatted phrase with a name query on /api", async() => {
expect.assertions(3);
const server = await main();
const testName = "Naomi";
const response = await server.inject({
method: "GET",
url: `/api?name=${testName}`,
});
expect(response.statusCode, "wrong status").toBe(200);
expect(response.headers["content-type"], "wrong content type").
toContain("text/plain");
const possibleResponses = phrases.flatMap((phrase) => {
return mommy.map((mom) => {
return phrase.
replaceAll("{{ name }}", testName).
replaceAll("{{ mommy }}", mom).
toLowerCase();
});
});
expect(possibleResponses, "where did it come from").
toContain(response.body);
await server.close();
});
});
+20
View File
@@ -0,0 +1,20 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/* eslint-disable max-nested-callbacks -- Not sure how to better optimise this. */
import { describe, expect, it } from "vitest";
import { mommy } from "../src/config/mommy.ts";
describe("mommy", () => {
it.each(mommy)(`%s should be unique`, (mom) => {
expect.assertions(1);
const matches = mommy.filter((m) => {
return m === mom;
});
expect(matches.length, `${mom} is not unique!`).toBeLessThan(2);
});
});
+35
View File
@@ -0,0 +1,35 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/* eslint-disable max-nested-callbacks -- Not sure how to better optimise this. */
import compare from "string-comparison";
import { describe, expect, it } from "vitest";
import { phrases } from "../src/config/phrases.ts";
describe("phrases", () => {
it("should have enough phrases", () => {
expect.assertions(1);
expect(phrases.length, "less than 100 phrases").toBeGreaterThan(100);
});
it.each(phrases)(`%s should be unique`, (phrase) => {
expect.assertions(1);
const filtered = phrases.filter((p) => {
return p !== phrase;
}).map((line) => {
return line.
replaceAll("{{ mommy }}", "").
replaceAll("{{ name }}", "");
});
const matches = compare.levenshtein.sortMatch(phrase.
replaceAll("{{ mommy }}", "").
replaceAll("{{ name }}", ""), filtered);
const closest = matches.reverse()[0];
expect(closest?.rating, `${phrase} is not unique! Matches ${closest?.member}`).toBeLessThan(0.8);
});
});