Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:

- run: deno check prodia.ts

- run: deno test --allow-env --allow-net
- run: deno test --allow-env --allow-net --allow-read --coverage
env:
PRODIA_TOKEN: ${{ secrets.PRODIA_TOKEN }}

- run: deno coverage
Binary file added test/landscape.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/man.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 43 additions & 2 deletions test/v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertEquals } from "jsr:@std/assert";
import { createProdia } from "../v2/index.ts";
import { assert, assertEquals } from "jsr:@std/assert";
import { createProdia, ProdiaUserError } from "../v2/index.ts";

const token = Deno.env.get("PRODIA_TOKEN");

Expand Down Expand Up @@ -32,3 +32,44 @@ await Deno.test("Example Job: JPEG Output", async () => {

assertEquals(isJpeg(image), true, "Image should be a JPEG");
});

await Deno.test("Example Job: Bad Face Swap", async () => {
const client = createProdia({
token,
});

try {
const job = await client.job(
{
type: "inference.faceswap.v1",
config: {},
},
{
inputs: [
new Blob([await Deno.readFile("./test/landscape.jpg")], {
type: "image/jpeg",
}),
new Blob([await Deno.readFile("./test/man.jpg")], {
type: "image/jpeg",
}),
],
},
);

const image = await job.arrayBuffer();

assertEquals(isJpeg(image), true, "Image should be a JPEG");

throw new Error("FaceSwap should fail");
} catch (error) {
assert(
error instanceof ProdiaUserError,
`Error should be a ProdiaUserError, got ${error}`,
);
assertEquals(
error.message,
"No face detected in the source image.",
"Error message should be 'No face detected in the source image.'",
);
}
});
26 changes: 20 additions & 6 deletions v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,28 +162,42 @@ export const createProdia = ({
retries <= maxRetries
);

if (response.headers.get("Content-Type") === "application/json") {
const body = await response.json() as ProdiaJob;

if ("error" in body && typeof body.error === "string") {
throw new ProdiaUserError(body.error);
} else {
throw new ProdiaBadResponseError(
`${response.status} ${response.statusText}`,
);
}
}

if (response.status === 429) {
throw new ProdiaCapacityError(
"Unable to schedule the job with current token.",
);
}

if (response.status < 200 || response.status > 299) {
throw new ProdiaBadResponseError(
`${response.status} ${response.statusText}`,
);
}

const body = await response.formData();

const job = JSON.parse(
new TextDecoder().decode(
await (body.get("job") as Blob).arrayBuffer(),
),
) as ProdiaJob;

if ("error" in job && typeof job.error === "string") {
throw new ProdiaUserError(job.error);
}

if (response.status < 200 || response.status > 299) {
throw new ProdiaBadResponseError(
`${response.status} ${response.statusText}`,
);
}

const buffer = await new Promise<ArrayBuffer>((resolve, reject) => {
const output = body.get("output") as File;
const reader = new FileReader();
Expand Down
Loading