All checks were successful
Build and Publish Docker Image / build-and-push (push) Successful in 50s
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
37 lines
1.3 KiB
TypeScript
37 lines
1.3 KiB
TypeScript
import { createUser, db, get, test } from "../helpers.ts";
|
|
import { assertEquals } from "@std/assert";
|
|
import type { ChatMessage } from "../../../shared/chat.ts";
|
|
|
|
function insertMessage(
|
|
userId: string,
|
|
body: string,
|
|
createdAt: string,
|
|
id?: string,
|
|
) {
|
|
const messageId = id ?? crypto.randomUUID();
|
|
db.prepare(
|
|
`INSERT INTO chat_messages (id, user_id, body, created_at) VALUES (?, ?, ?, ?);`,
|
|
).run(messageId, userId, body, createdAt);
|
|
return messageId;
|
|
}
|
|
|
|
const T = (n: number) => new Date(Date.UTC(2024, 0, 1, 0, 0, n)).toISOString();
|
|
|
|
test("GET /api/chat: members only, with limit and both cursors", async () => {
|
|
const user = await createUser();
|
|
const ids = [1, 2, 3].map((n) => insertMessage(user.id, `m${n}`, T(n)));
|
|
assertEquals((await get("/api/chat")).status, 401);
|
|
const bodies = async (q: string) =>
|
|
(await get<ChatMessage[]>(`/api/chat${q}`, { auth: user.auth })).data.map(
|
|
(m) => m.body,
|
|
);
|
|
assertEquals(await bodies(""), ["m1", "m2", "m3"]);
|
|
assertEquals(await bodies("?limit=1"), ["m3"]);
|
|
assertEquals(await bodies("?limit=abc"), ["m1", "m2", "m3"]);
|
|
assertEquals(await bodies(`?before=${T(3)}&beforeId=${ids[2]}`), [
|
|
"m1",
|
|
"m2",
|
|
]);
|
|
assertEquals(await bodies(`?after=${T(1)}&afterId=${ids[0]}`), ["m2", "m3"]);
|
|
});
|