|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { crawl, extract, getCredits, getHistory, monitor, scrape, search } from "../src/index.js"; |
| 3 | + |
| 4 | +const API_KEY = process.env.SGAI_API_KEY || "sgai-669918e5-55be-4752-a684-f6da788d1384"; |
| 5 | + |
| 6 | +describe("integration", () => { |
| 7 | + test("getCredits", async () => { |
| 8 | + const res = await getCredits(API_KEY); |
| 9 | + console.log("getCredits:", res); |
| 10 | + expect(res.status).toBe("success"); |
| 11 | + expect(res.data).toHaveProperty("remaining"); |
| 12 | + expect(res.data).toHaveProperty("plan"); |
| 13 | + }); |
| 14 | + |
| 15 | + test("scrape markdown", async () => { |
| 16 | + const res = await scrape(API_KEY, { |
| 17 | + url: "https://example.com", |
| 18 | + formats: [{ type: "markdown" }], |
| 19 | + }); |
| 20 | + console.log("scrape:", res.status, res.error); |
| 21 | + expect(res.status).toBe("success"); |
| 22 | + expect(res.data?.results.markdown).toBeDefined(); |
| 23 | + }); |
| 24 | + |
| 25 | + test("extract", async () => { |
| 26 | + const res = await extract(API_KEY, { |
| 27 | + url: "https://example.com", |
| 28 | + prompt: "What is this page about?", |
| 29 | + }); |
| 30 | + console.log("extract:", res.status, res.error); |
| 31 | + expect(res.status).toBe("success"); |
| 32 | + }); |
| 33 | + |
| 34 | + test("search", async () => { |
| 35 | + const res = await search(API_KEY, { |
| 36 | + query: "anthropic claude", |
| 37 | + numResults: 2, |
| 38 | + }); |
| 39 | + console.log("search:", res.status, res.error); |
| 40 | + expect(res.status).toBe("success"); |
| 41 | + expect(res.data?.results.length).toBeGreaterThan(0); |
| 42 | + }); |
| 43 | + |
| 44 | + test("getHistory", async () => { |
| 45 | + const res = await getHistory(API_KEY, { limit: 5 }); |
| 46 | + console.log("getHistory:", res.status, res.data?.pagination); |
| 47 | + expect(res.status).toBe("success"); |
| 48 | + }); |
| 49 | + |
| 50 | + test("crawl.start and crawl.get", async () => { |
| 51 | + const startRes = await crawl.start(API_KEY, { |
| 52 | + url: "https://example.com", |
| 53 | + maxPages: 2, |
| 54 | + }); |
| 55 | + console.log("crawl.start:", startRes.status, startRes.data?.id, startRes.error); |
| 56 | + |
| 57 | + if ( |
| 58 | + startRes.status === "error" && |
| 59 | + (startRes.error?.includes("Max") || startRes.error?.includes("Rate")) |
| 60 | + ) { |
| 61 | + console.log("Skipping - rate limited"); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + expect(startRes.status).toBe("success"); |
| 66 | + |
| 67 | + if (startRes.data?.id) { |
| 68 | + const getRes = await crawl.get(API_KEY, startRes.data.id); |
| 69 | + console.log("crawl.get:", getRes.status, getRes.data?.status); |
| 70 | + expect(getRes.status).toBe("success"); |
| 71 | + } |
| 72 | + }); |
| 73 | +}); |
0 commit comments