Skip to content

Commit d5fc7ba

Browse files
committed
Add browser integration tests
1 parent b06a5f8 commit d5fc7ba

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-0
lines changed

integration/browser/.npmrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package-lock=false
2+
audit=false
3+
fund=false

integration/browser/index.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Replicate from "replicate";
2+
3+
/**
4+
* @param {string} - token the REPLICATE_API_TOKEN
5+
*/
6+
window.main = async (token) => {
7+
const replicate = new Replicate({ auth: token });
8+
const stream = replicate.stream(
9+
"replicate/canary:30e22229542eb3f79d4f945dacb58d32001b02cc313ae6f54eef27904edf3272",
10+
{
11+
input: {
12+
text: "Betty Browser",
13+
},
14+
}
15+
);
16+
17+
const output = [];
18+
for await (const event of stream) {
19+
output.push(String(event));
20+
}
21+
return output.join("");
22+
};

integration/browser/index.test.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { test, expect } from "@playwright/test";
2+
import { build } from "esbuild";
3+
4+
// Convert the source file from commonjs to a browser script.
5+
const result = await build({
6+
entryPoints: ["index.js"],
7+
bundle: true,
8+
platform: "browser",
9+
external: ["node:crypto"],
10+
write: false,
11+
});
12+
const source = new TextDecoder().decode(result.outputFiles[0].contents);
13+
14+
// https://playwright.dev/docs/network#modify-requests
15+
16+
test("browser", async ({ page }) => {
17+
// Patch the API endpoint to work around CORS for now.
18+
await page.route(
19+
"https://api.replicate.com/v1/predictions",
20+
async (route) => {
21+
// Fetch original response.
22+
const response = await route.fetch();
23+
// Add a prefix to the title.
24+
return route.fulfill({ response });
25+
}
26+
);
27+
28+
await page.addScriptTag({ content: source });
29+
const result = await page.evaluate(
30+
(token) => window.main(token),
31+
[process.env.REPLICATE_API_TOKEN]
32+
);
33+
expect(result).toBe("hello there, Betty Browser");
34+
});

integration/browser/package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "replicate-app-browser",
3+
"private": true,
4+
"version": "0.0.0",
5+
"description": "",
6+
"main": "index.js",
7+
"type": "module",
8+
"scripts": {
9+
"test": "node index.test.js"
10+
},
11+
"license": "ISC",
12+
"dependencies": {
13+
"replicate": "../../"
14+
},
15+
"devDependencies": {
16+
"@playwright/test": "^1.42.1",
17+
"esbuild": "^0.20.1"
18+
}
19+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from "@playwright/test";
2+
3+
export default defineConfig({});

0 commit comments

Comments
 (0)