From 5bef2090d0e772e2f5d192f4cb19437e789a6dbe Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 27 Jan 2024 14:31:03 -0600 Subject: [PATCH] Basic test --- routes/api/user.ts | 10 +++++++--- tests/main_test.ts | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/main_test.ts diff --git a/routes/api/user.ts b/routes/api/user.ts index f1d7051..67f08e9 100644 --- a/routes/api/user.ts +++ b/routes/api/user.ts @@ -87,7 +87,8 @@ export const handler: Handlers = { // TODO: json or query params const accept = req.headers.get('accept') if (accept === 'text/event-stream') { - const stream = kv.watch([['last_user_updated']]).getReader() + const stream = kv.watch([['last_user_updated']]) + .getReader() const body = new ReadableStream({ async start(controller) { console.log( @@ -100,12 +101,15 @@ export const handler: Handlers = { return } - if (typeof entry.value !== 'string') { + let v: string + if (typeof entry.value === 'string') { + v = z.object({ value: z.string() }).parse(entry).value + } else { continue } const user = await db.users.findFirst({ - where: { id: entry.value }, + where: { id: v }, }) const chunk = `data: ${JSON.stringify(user)}\n\n` controller.enqueue(new TextEncoder().encode(chunk)) diff --git a/tests/main_test.ts b/tests/main_test.ts new file mode 100644 index 0000000..38e8888 --- /dev/null +++ b/tests/main_test.ts @@ -0,0 +1,23 @@ +import { createHandler, ServeHandlerInfo } from '$fresh/server.ts' +import manifest from '@homeman/fresh.gen.ts' +import config from '@homeman/fresh.config.ts' +import { assert, assertEquals } from '$std/assert/mod.ts' + +const hostname = '127.0.0.1' +const port = 53496 + +const CONN_INFO: ServeHandlerInfo = { + remoteAddr: { hostname, port, transport: 'tcp' }, +} + +Deno.test('HTTP assert test.', async (t) => { + const handler = await createHandler(manifest, config) + + await t.step('#1 GET /', async () => { + const resp = await handler(new Request(`http://${hostname}/`), CONN_INFO) + const body = await resp.text() + assertEquals(resp.status, 200) + assert(body.includes('Fam')) + assert(body.includes('Flan')) + }) +})