test: fix discovery test to boot up server instead of relying on 3rd party (#14327)

This commit is contained in:
Aiden Cline
2026-02-19 14:11:23 -06:00
committed by GitHub
parent 8b99648790
commit 00c079868a
5 changed files with 511 additions and 8 deletions

View File

@@ -1,9 +1,47 @@
import { describe, test, expect } from "bun:test"
import { describe, test, expect, beforeAll, afterAll } from "bun:test"
import { Discovery } from "../../src/skill/discovery"
import { Filesystem } from "../../src/util/filesystem"
import { rm } from "fs/promises"
import path from "path"
const CLOUDFLARE_SKILLS_URL = "https://developers.cloudflare.com/.well-known/skills/"
let CLOUDFLARE_SKILLS_URL: string
let server: ReturnType<typeof Bun.serve>
let downloadCount = 0
const fixturePath = path.join(import.meta.dir, "../fixture/skills")
beforeAll(async () => {
await rm(Discovery.dir(), { recursive: true, force: true })
server = Bun.serve({
port: 0,
async fetch(req) {
const url = new URL(req.url)
// route /.well-known/skills/* to the fixture directory
if (url.pathname.startsWith("/.well-known/skills/")) {
const filePath = url.pathname.replace("/.well-known/skills/", "")
const fullPath = path.join(fixturePath, filePath)
if (await Filesystem.exists(fullPath)) {
if (!fullPath.endsWith("index.json")) {
downloadCount++
}
return new Response(Bun.file(fullPath))
}
}
return new Response("Not Found", { status: 404 })
},
})
CLOUDFLARE_SKILLS_URL = `http://localhost:${server.port}/.well-known/skills/`
})
afterAll(async () => {
server?.stop()
await rm(Discovery.dir(), { recursive: true, force: true })
})
describe("Discovery.pull", () => {
test("downloads skills from cloudflare url", async () => {
@@ -14,7 +52,7 @@ describe("Discovery.pull", () => {
const md = path.join(dir, "SKILL.md")
expect(await Filesystem.exists(md)).toBe(true)
}
}, 30_000)
})
test("url without trailing slash works", async () => {
const dirs = await Discovery.pull(CLOUDFLARE_SKILLS_URL.replace(/\/$/, ""))
@@ -23,15 +61,16 @@ describe("Discovery.pull", () => {
const md = path.join(dir, "SKILL.md")
expect(await Filesystem.exists(md)).toBe(true)
}
}, 30_000)
})
test("returns empty array for invalid url", async () => {
const dirs = await Discovery.pull("https://example.invalid/.well-known/skills/")
const dirs = await Discovery.pull(`http://localhost:${server.port}/invalid-url/`)
expect(dirs).toEqual([])
})
test("returns empty array for non-json response", async () => {
const dirs = await Discovery.pull("https://example.com/")
// any url not explicitly handled in server returns 404 text "Not Found"
const dirs = await Discovery.pull(`http://localhost:${server.port}/some-other-path/`)
expect(dirs).toEqual([])
})
@@ -39,6 +78,7 @@ describe("Discovery.pull", () => {
const dirs = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
// find a skill dir that should have reference files (e.g. agents-sdk)
const agentsSdk = dirs.find((d) => d.endsWith("/agents-sdk"))
expect(agentsSdk).toBeDefined()
if (agentsSdk) {
const refs = path.join(agentsSdk, "references")
expect(await Filesystem.exists(path.join(agentsSdk, "SKILL.md"))).toBe(true)
@@ -46,16 +86,25 @@ describe("Discovery.pull", () => {
const refDir = await Array.fromAsync(new Bun.Glob("**/*.md").scan({ cwd: refs, onlyFiles: true }))
expect(refDir.length).toBeGreaterThan(0)
}
}, 30_000)
})
test("caches downloaded files on second pull", async () => {
// clear dir and downloadCount
await rm(Discovery.dir(), { recursive: true, force: true })
downloadCount = 0
// first pull to populate cache
const first = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
expect(first.length).toBeGreaterThan(0)
const firstCount = downloadCount
expect(firstCount).toBeGreaterThan(0)
// second pull should return same results from cache
const second = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
expect(second.length).toBe(first.length)
expect(second.sort()).toEqual(first.sort())
}, 60_000)
// second pull should NOT increment download count
expect(downloadCount).toBe(firstCount)
})
})