Basic test

This commit is contained in:
Daniel Flanagan 2024-01-27 14:31:03 -06:00
parent 88889fe443
commit 5bef2090d0
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 30 additions and 3 deletions

View file

@ -87,7 +87,8 @@ export const handler: Handlers<User | null> = {
// TODO: json or query params // TODO: json or query params
const accept = req.headers.get('accept') const accept = req.headers.get('accept')
if (accept === 'text/event-stream') { if (accept === 'text/event-stream') {
const stream = kv.watch([['last_user_updated']]).getReader() const stream = kv.watch<readonly unknown[]>([['last_user_updated']])
.getReader()
const body = new ReadableStream({ const body = new ReadableStream({
async start(controller) { async start(controller) {
console.log( console.log(
@ -100,12 +101,15 @@ export const handler: Handlers<User | null> = {
return 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 continue
} }
const user = await db.users.findFirst({ const user = await db.users.findFirst({
where: { id: entry.value }, where: { id: v },
}) })
const chunk = `data: ${JSON.stringify(user)}\n\n` const chunk = `data: ${JSON.stringify(user)}\n\n`
controller.enqueue(new TextEncoder().encode(chunk)) controller.enqueue(new TextEncoder().encode(chunk))

23
tests/main_test.ts Normal file
View file

@ -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'))
})
})