Skip to content

Commit

Permalink
Add initial test coverage for saving a new custom emoji
Browse files Browse the repository at this point in the history
This is mostly to show the fake storage in action, as well as setup the helpers required for writing these kinds of tests
  • Loading branch information
ThisIsMissEm committed Feb 23, 2025
1 parent d5bfb8b commit 4916908
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
ALLOW_PRIVATE_ADDRESS: false

DRIVE_DISK: "fs"
FS_STORAGE_PATH: ./tmp/test_storage
FS_STORAGE_PATH: ./tmp/fakes
STORAGE_URL_BASE: "http://hollo.test/"
steps:
- uses: actions/checkout@v4
Expand All @@ -71,8 +71,6 @@ jobs:
node-version: 23
cache: pnpm
- run: pnpm install
- name: Ensure the directory for test uploads
run: mkdir -p tmp/test_storage
- name: Run the database migrations
run: pnpm run migrate
- name: Run the tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ assets/
fedify-hollo-*.tgz
*.jsonl
node_modules/
tmp/
58 changes: 58 additions & 0 deletions src/pages/emojis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { afterEach, beforeEach, describe, it } from "node:test";
import type { TestContext } from "node:test";

import { getFixtureFile, getLoginCookie } from "../../tests/helpers";

import db from "../db";
import { customEmojis } from "../schema";

import { drive } from "../storage";
import app from "./index";

const emojiFile = await getFixtureFile("emoji.png", "image/png");

describe("emojis", () => {
beforeEach(async () => {
await db.delete(customEmojis);
});

afterEach(() => {
drive.restore();
});

it("Successfully saves a new emoji", async (t: TestContext) => {
const disk = drive.fake();
const testShortCode = ":test-emoji:";

const formData = new FormData();
formData.append("shortcode", testShortCode);
formData.append("image", emojiFile);

const cookie = await getLoginCookie();

const response = await app.request("/emojis", {
method: "POST",
body: formData,
headers: {
Cookie: cookie,
},
});

t.assert.equal(response.status, 302);
t.assert.equal(response.headers.get("Location"), "/emojis");

// Assert we uploaded the file:
t.assert.doesNotThrow(() => disk.assertExists("emojis/test-emoji.png"));

const emoji = await db.query.customEmojis.findFirst();

t.assert.ok(emoji, "Successfully saves the emoji");
t.assert.equal(emoji.category, null, "Defaults category to null");
t.assert.equal(
emoji.url,
"http://hollo.test/assets/emojis/test-emoji.png",
"Sets the file URL correctly",
);
t.assert.equal(emoji.shortcode, "test-emoji");
});
});
Binary file added tests/fixtures/files/emoji.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { after } from "node:test";
import { serializeSigned } from "hono/utils/cookie";
import db from "../src/db";
import { drive } from "../src/storage";

const fixtureFiles = join(import.meta.dirname, "fixtures", "files");

export async function getFixtureFile(
name: string,
type: string,
): Promise<File> {
const filePath = join(fixtureFiles, name);
const data = await readFile(filePath);

return new File([data], name, {
type,
});
}

// biome-ignore lint/complexity/useLiteralKeys: tsc complains about this (TS4111)
const SECRET_KEY = process.env["SECRET_KEY"];

export async function getLoginCookie() {
// Same logic as in src/pages/login.tsx
return serializeSigned("login", new Date().toISOString(), SECRET_KEY!, {
path: "/",
});
}

// Automatically close the database and remove test file uploads
// Without this the tests hang due to the database
after(async () => {
await db.$client.end({ timeout: 5 });

const disk = drive.fake();
await disk.deleteAll();
});

0 comments on commit 4916908

Please sign in to comment.