From 3a73a35bdeffa7481e1125dd751df528720e8d2c Mon Sep 17 00:00:00 2001 From: Monty Anderson Date: Thu, 3 Oct 2024 11:34:06 +0100 Subject: [PATCH] `*`: add simple test --- .github/workflows/validate.yml | 24 +++++++++++++----------- deno.lock | 29 +++++++++++++++++++++++++++++ test/v2.test.ts | 34 ++++++++++++++++++++++++++++++++++ v2/index.ts | 9 +++++---- 4 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 deno.lock create mode 100644 test/v2.test.ts diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index e65fdf1..1f82d9f 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -1,18 +1,20 @@ -name: Validate Formatting & Types +name: Formatting, Types, & Test on: [push, pull_request] jobs: - validate: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 + validate: + runs-on: ubuntu-latest - - uses: denoland/setup-deno@v1 - with: - deno-version: v1.x + steps: + - uses: actions/checkout@v3 - - run: deno fmt --check + - uses: denoland/setup-deno@v1 + with: + deno-version: v1.x - - run: deno check prodia.ts + - run: deno fmt --check + + - run: deno check prodia.ts + + - run: deno test --allow-env --allow-net diff --git a/deno.lock b/deno.lock new file mode 100644 index 0000000..3928a55 --- /dev/null +++ b/deno.lock @@ -0,0 +1,29 @@ +{ + "version": "3", + "packages": { + "specifiers": { + "jsr:@std/assert": "jsr:@std/assert@0.225.3", + "jsr:@std/internal@^1.0.0": "jsr:@std/internal@1.0.4" + }, + "jsr": { + "@std/assert@0.225.3": { + "integrity": "b3c2847aecf6955b50644cdb9cf072004ea3d1998dd7579fc0acb99dbb23bd4f", + "dependencies": [ + "jsr:@std/internal@^1.0.0" + ] + }, + "@std/internal@1.0.4": { + "integrity": "62e8e4911527e5e4f307741a795c0b0a9e6958d0b3790716ae71ce085f755422" + } + } + }, + "remote": {}, + "workspace": { + "packageJson": { + "dependencies": [ + "npm:prettier@^3.0.0", + "npm:typescript@^5.1.6" + ] + } + } +} diff --git a/test/v2.test.ts b/test/v2.test.ts new file mode 100644 index 0000000..879abe4 --- /dev/null +++ b/test/v2.test.ts @@ -0,0 +1,34 @@ +import { assertEquals } from "jsr:@std/assert"; +import { createProdia } from "../v2/index.ts"; + +const token = Deno.env.get("PRODIA_TOKEN"); + +if(typeof token !== "string") { + throw new Error("PRODIA_TOKEN is not set"); +} + +const isJpeg = (image: ArrayBuffer): boolean => { + const view = new Uint8Array(image); + + return view[0] === 0xFF && view[1] === 0xD8; +}; + +await Deno.test("Example Job: JPEG Output", async () => { + const client = createProdia({ + token + }); + + const job = await client.job({ + "type": "inference.flux.dev.txt2img.v1", + "config": { + "prompt": "puppies in a cloud, 4k", + "steps": 1, + "width": 1024, + "height": 1024 + } + }); + + const image = await job.arrayBuffer(); + + assertEquals(isJpeg(image), true, "Image should be a JPEG"); +}); diff --git a/v2/index.ts b/v2/index.ts index ca38ed3..76c7e56 100644 --- a/v2/index.ts +++ b/v2/index.ts @@ -71,8 +71,9 @@ export const createProdia = ({ response = await fetch(`${baseUrl}/job`, { method: "POST", headers: { - Authorization: `Bearer ${token}`, - Accept: options.accept, + "Authorization": `Bearer ${token}`, + "Accept": options.accept, + "Content-Type": "application/json" }, body: JSON.stringify(params), }); @@ -95,13 +96,13 @@ export const createProdia = ({ if (response.status === 429) { throw new ProdiaCapacityError( - "CapacityError: Unable to schedule job with current token", + "ProdiaCapacityError: Unable to schedule job with current token", ); } if (response.status < 200 || response.status > 299) { throw new ProdiaBadResponseError( - "BadResponseError: Invalid response from Prodia API", + `ProdiaBadResponseError: ${response.status} ${response.statusText}`, ); }