fiddy/apps/web/__tests__/groups.test.ts
2026-02-11 23:45:15 -08:00

110 lines
3.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import path from "node:path";
import { fileURLToPath } from "node:url";
import dotenv from "dotenv";
import getPool from "../lib/server/db";
import { setActiveGroupForUser } from "../lib/server/groups";
import { cleanupTestData, uniqueInviteCode } from "./test-helpers";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const envLoaded = dotenv.config({ path: path.resolve(__dirname, "../../../.env") });
const hasDb = Boolean(process.env.DATABASE_URL);
test("create group inserts membership", async t => {
if (!hasDb) {
t.skip("DATABASE_URL not set");
return;
}
if (envLoaded.error) t.diagnostic(String(envLoaded.error));
const pool = getPool();
const client = await pool.connect();
let userId: number | null = null;
let groupId: number | null = null;
try {
const userRes = await client.query(
"insert into users(email, password_hash) values($1,$2) returning id",
[`group_${Date.now()}@example.com`, "hash"]
);
userId = Number(userRes.rows[0].id);
const groupRes = await client.query(
"insert into groups(name, invite_code, created_by) values($1,$2,$3) returning id",
["Test Group", uniqueInviteCode("G"), userId]
);
groupId = groupRes.rows[0].id as number;
await client.query(
"insert into group_members(group_id, user_id, role) values($1,$2,'GROUP_OWNER')",
[groupId, userId]
);
const { rows } = await client.query(
"select role from group_members where group_id=$1 and user_id=$2",
[groupId, userId]
);
assert.equal(rows[0].role, "GROUP_OWNER");
} finally {
await cleanupTestData(client, { userIds: [userId], groupId });
client.release();
}
});
test("setActiveGroupForUser stores active group", async t => {
if (!hasDb) {
t.skip("DATABASE_URL not set");
return;
}
if (envLoaded.error) t.diagnostic(String(envLoaded.error));
const pool = getPool();
const client = await pool.connect();
let userId: number | null = null;
let groupId: number | null = null;
let otherUserId: number | null = null;
try {
const userRes = await client.query(
"insert into users(email, password_hash) values($1,$2) returning id",
[`active_${Date.now()}@example.com`, "hash"]
);
userId = userRes.rows[0].id as number;
const otherRes = await client.query(
"insert into users(email, password_hash) values($1,$2) returning id",
[`active_other_${Date.now()}@example.com`, "hash"]
);
otherUserId = Number(otherRes.rows[0].id);
const groupRes = await client.query(
"insert into groups(name, invite_code, created_by) values($1,$2,$3) returning id",
["Active Group", uniqueInviteCode("A"), userId]
);
groupId = Number(groupRes.rows[0].id);
await client.query(
"insert into group_members(group_id, user_id, role) values($1,$2,'GROUP_ADMIN')",
[groupId, userId]
);
await setActiveGroupForUser(userId, groupId);
const { rows } = await client.query(
"select data->>'activeGroupId' as active_group_id from user_settings where user_id=$1",
[userId]
);
assert.equal(Number(rows[0]?.active_group_id || 0), groupId);
await assert.rejects(
() => setActiveGroupForUser(otherUserId!, groupId!),
{ message: "FORBIDDEN" }
);
} finally {
await cleanupTestData(client, { userIds: [otherUserId, userId], groupId });
client.release();
}
});