From 287765abbd02b6c4bb6f79bc638dfd7a96203ba2 Mon Sep 17 00:00:00 2001 From: "Michael J. Roberts" Date: Fri, 12 Apr 2024 15:27:02 +0100 Subject: [PATCH] feat: add client.camera.connect() route handler to @observerly/hyper. feat: add client.camera.connect() route handler to @observerly/hyper. Includes associated test suite for module export definition and expected output from API route. --- src/routes/camera.ts | 23 +++++++++++++++++++++++ tests/camera.spec.ts | 8 ++++++++ tests/mocks/camera.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/src/routes/camera.ts b/src/routes/camera.ts index 879d2dd..bcd2494 100644 --- a/src/routes/camera.ts +++ b/src/routes/camera.ts @@ -137,6 +137,29 @@ export const camera = (base: URL, init?: RequestInit, headers?: () => Promise(url, { ...init, method: 'PUT' }, headers) } }, + { + name: 'connect', + action: < + T = { + connected: boolean + } + >() => { + const url = new URL('camera/connect', base) + + const data = JSON.stringify({ connect: true }) + + return dispatchRequest( + url, + { + ...init, + method: 'PUT', + body: JSON.stringify({ connect: true }) + }, + headers, + data + ) + } + }, { name: 'turnCoolerOn', action: < diff --git a/tests/camera.spec.ts b/tests/camera.spec.ts index adc07ef..bda7797 100644 --- a/tests/camera.spec.ts +++ b/tests/camera.spec.ts @@ -26,6 +26,14 @@ suite('@observerly/hyper Fiber API Observing Camera Client', () => { expect(isConnected).toStrictEqual({ connected: true }) }) + it('should be able to connect the camera', async () => { + const client = setupClient(getURL('/api/v1/')) + const status = await client.camera.connect() + expect(isDataResult(status)).toBe(true) + if (!isDataResult(status)) return + expect(status).toStrictEqual({ connected: true }) + }) + it('should be able to get the configuration of the camera', async () => { const client = setupClient(getURL('/api/v1/')) const configuration = await client.camera.getConfiguration() diff --git a/tests/mocks/camera.ts b/tests/mocks/camera.ts index 80d2ccb..f0ae10b 100644 --- a/tests/mocks/camera.ts +++ b/tests/mocks/camera.ts @@ -112,6 +112,33 @@ export const cameraHandlers: Handler[] = [ } }) }, + { + method: 'PUT', + url: '/api/v1/camera/connect', + handler: eventHandler(async event => { + const method = getMethod(event) + + if (method !== 'PUT') { + return new Response('Method Not Allowed', { + status: 405, + statusText: 'Method Not Allowed' + }) + } + + const body = await readBody<{ connect: boolean }>(event) + + if (!body) { + return new Response('Bad Request', { + status: 400, + statusText: 'Bad Request' + }) + } + + return { + connected: body.connect + } + }) + }, { method: ['PUT', 'DELETE'], url: '/api/v1/camera/cooler',