|
1 | 1 | import { expect, jest, test } from "@jest/globals"; |
2 | | -import Replicate, { ApiError, Model, Prediction } from "replicate"; |
| 2 | +import Replicate, { |
| 3 | + ApiError, |
| 4 | + Model, |
| 5 | + Prediction, |
| 6 | + validateWebhook, |
| 7 | +} from "replicate"; |
3 | 8 | import nock from "nock"; |
4 | 9 | import fetch from "cross-fetch"; |
5 | 10 |
|
@@ -67,6 +72,22 @@ describe("Replicate client", () => { |
67 | 72 | }); |
68 | 73 | }); |
69 | 74 |
|
| 75 | + describe("account.get", () => { |
| 76 | + test("Calls the correct API route", async () => { |
| 77 | + nock(BASE_URL).get("/account").reply(200, { |
| 78 | + type: "organization", |
| 79 | + username: "replicate", |
| 80 | + name: "Replicate", |
| 81 | + github_url: "https://github.com/replicate", |
| 82 | + }); |
| 83 | + |
| 84 | + const account = await client.accounts.current(); |
| 85 | + expect(account.type).toBe("organization"); |
| 86 | + expect(account.username).toBe("replicate"); |
| 87 | + }); |
| 88 | + // Add more tests for error handling, edge cases, etc. |
| 89 | + }); |
| 90 | + |
70 | 91 | describe("collections.list", () => { |
71 | 92 | test("Calls the correct API route", async () => { |
72 | 93 | nock(BASE_URL) |
@@ -741,6 +762,47 @@ describe("Replicate client", () => { |
741 | 762 | // Add more tests for error handling, edge cases, etc. |
742 | 763 | }); |
743 | 764 |
|
| 765 | + describe("deployments.get", () => { |
| 766 | + test("Calls the correct API route with the correct payload", async () => { |
| 767 | + nock(BASE_URL) |
| 768 | + .get("/deployments/acme/my-app-image-generator") |
| 769 | + .reply(200, { |
| 770 | + owner: "acme", |
| 771 | + name: "my-app-image-generator", |
| 772 | + current_release: { |
| 773 | + number: 1, |
| 774 | + model: "stability-ai/sdxl", |
| 775 | + version: |
| 776 | + "da77bc59ee60423279fd632efb4795ab731d9e3ca9705ef3341091fb989b7eaf", |
| 777 | + created_at: "2024-02-15T16:32:57.018467Z", |
| 778 | + created_by: { |
| 779 | + type: "organization", |
| 780 | + username: "acme", |
| 781 | + name: "Acme Corp, Inc.", |
| 782 | + github_url: "https://github.com/acme", |
| 783 | + }, |
| 784 | + configuration: { |
| 785 | + hardware: "gpu-t4", |
| 786 | + scaling: { |
| 787 | + min_instances: 1, |
| 788 | + max_instances: 5, |
| 789 | + }, |
| 790 | + }, |
| 791 | + }, |
| 792 | + }); |
| 793 | + |
| 794 | + const deployment = await client.deployments.get( |
| 795 | + "acme", |
| 796 | + "my-app-image-generator" |
| 797 | + ); |
| 798 | + |
| 799 | + expect(deployment.owner).toBe("acme"); |
| 800 | + expect(deployment.name).toBe("my-app-image-generator"); |
| 801 | + expect(deployment.current_release.model).toBe("stability-ai/sdxl"); |
| 802 | + }); |
| 803 | + // Add more tests for error handling, edge cases, etc. |
| 804 | + }); |
| 805 | + |
744 | 806 | describe("predictions.create with model", () => { |
745 | 807 | test("Calls the correct API route with the correct payload", async () => { |
746 | 808 | nock(BASE_URL) |
@@ -1021,5 +1083,39 @@ describe("Replicate client", () => { |
1021 | 1083 | }); |
1022 | 1084 | }); |
1023 | 1085 |
|
| 1086 | + describe("webhooks.default.secret.get", () => { |
| 1087 | + test("Calls the correct API route", async () => { |
| 1088 | + nock(BASE_URL).get("/webhooks/default/secret").reply(200, { |
| 1089 | + key: "whsec_5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH", |
| 1090 | + }); |
| 1091 | + |
| 1092 | + const secret = await client.webhooks.default.secret.get(); |
| 1093 | + expect(secret.key).toBe("whsec_5WbX5kEWLlfzsGNjH64I8lOOqUB6e8FH"); |
| 1094 | + }); |
| 1095 | + |
| 1096 | + test("Can be used to validate webhook", async () => { |
| 1097 | + // Test case from https://github.com/svix/svix-webhooks/blob/b41728cd98a7e7004a6407a623f43977b82fcba4/javascript/src/webhook.test.ts#L190-L200 |
| 1098 | + const request = new Request("http://test.host/webhook", { |
| 1099 | + method: "POST", |
| 1100 | + headers: { |
| 1101 | + "Content-Type": "application/json", |
| 1102 | + "Webhook-ID": "msg_p5jXN8AQM9LWM0D4loKWxJek", |
| 1103 | + "Webhook-Timestamp": "1614265330", |
| 1104 | + "Webhook-Signature": |
| 1105 | + "v1,g0hM9SsE+OTPJTGt/tmIKtSyZlE3uFJELVlNIOLJ1OE=", |
| 1106 | + }, |
| 1107 | + body: `{"test": 2432232314}`, |
| 1108 | + }); |
| 1109 | + |
| 1110 | + // This is a test secret and should not be used in production |
| 1111 | + const secret = "whsec_MfKQ9r8GKYqrTwjUPD8ILPZIo2LaLaSw"; |
| 1112 | + |
| 1113 | + const isValid = await validateWebhook(request, secret); |
| 1114 | + expect(isValid).toBe(true); |
| 1115 | + }); |
| 1116 | + |
| 1117 | + // Add more tests for error handling, edge cases, etc. |
| 1118 | + }); |
| 1119 | + |
1024 | 1120 | // Continue with tests for other methods |
1025 | 1121 | }); |
0 commit comments