Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add test coverage for file methods
  • Loading branch information
mattt committed Jul 3, 2024
commit f063ba3b6ba168d2dbba9aa06898921a12c1f7ab
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ declare module "replicate" {
): Promise<FileObject>;
list(): Promise<Page<FileObject>>;
get(file_id: string): Promise<FileObject>;
delete(file_id: string): Promise<void>;
delete(file_id: string): Promise<boolean>;
};

hardware: {
Expand Down
140 changes: 115 additions & 25 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ const BASE_URL = "https://api.replicate.com/v1";

nock.disableNetConnect();

const fileTestCases = [
// Skip test case if File type is not available
...(typeof File !== "undefined"
? [
{
type: "file",
value: new File(["hello world"], "file_hello.txt", {
type: "text/plain",
}),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
]
: []),
{
type: "blob",
value: new Blob(["hello world"], { type: "text/plain" }),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
{
type: "buffer",
value: Buffer.from("hello world"),
expected: "data:application/octet-stream;base64,aGVsbG8gd29ybGQ=",
},
];

describe("Replicate client", () => {
let unmatched: any[] = [];
const handleNoMatch = (req: unknown, options: any, body: string) =>
Expand Down Expand Up @@ -264,31 +289,6 @@ describe("Replicate client", () => {
}
);

const fileTestCases = [
// Skip test case if File type is not available
...(typeof File !== "undefined"
? [
{
type: "file",
value: new File(["hello world"], "file_hello.txt", {
type: "text/plain",
}),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
]
: []),
{
type: "blob",
value: new Blob(["hello world"], { type: "text/plain" }),
expected: "data:text/plain;base64,aGVsbG8gd29ybGQ=",
},
{
type: "buffer",
value: Buffer.from("hello world"),
expected: "data:application/octet-stream;base64,aGVsbG8gd29ybGQ=",
},
];

test.each(fileTestCases)(
"converts a $type input into a Replicate file URL",
async ({ value: data, type }) => {
Expand Down Expand Up @@ -1058,6 +1058,96 @@ describe("Replicate client", () => {
// Add more tests for error handling, edge cases, etc.
});

describe("files.create", () => {
test("Calls the correct API route with the correct payload", async () => {
for (const testCase of fileTestCases) {
nock(BASE_URL)
.post("/files")
.reply(200, {
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
});
const file = await client.files.create(testCase.value);
expect(file.id).toBe("123");
expect(file.name).toBe("test-file");
}
});
});

describe("files.get", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
.get("/files/123")
.reply(200, {
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
});

const file = await client.files.get("123");
expect(file.id).toBe("123");
expect(file.name).toBe("test-file");
});
});

describe("files.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
.get("/files")
.reply(200, {
next: null,
previous: null,
results: [
{
id: "123",
name: "test-file",
content_type: "application/octet-stream",
size: 1024,
etag: "abc123",
checksum: "sha256:1234567890abcdef",
metadata: {},
created_at: "2023-01-01T00:00:00Z",
expires_at: null,
urls: {
get: "https://api.replicate.com/v1/files/123",
},
},
],
});

const files = await client.files.list();
expect(files.results.length).toBe(1);
expect(files.results[0].id).toBe("123");
});
});

describe("files.delete", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL).delete("/files/123").reply(204);
const success = await client.files.delete("123");
expect(success).toBe(true);
});
});

describe("hardware.list", () => {
test("Calls the correct API route", async () => {
nock(BASE_URL)
Expand Down
4 changes: 2 additions & 2 deletions lib/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ async function getFile(file_id) {
* Delete a file
*
* @param {string} file_id - Required. The ID of the file.
* @returns {Promise<object>} - Resolves with the deletion confirmation
* @returns {Promise<boolean>} - Resolves with true if the file was deleted
*/
async function deleteFile(file_id) {
const response = await this.request(`/files/${file_id}`, {
method: "DELETE",
});

return response.json();
return response.status === 204;
}

module.exports = {
Expand Down