discovery.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { describe, test, expect } from "bun:test"
  2. import { Discovery } from "../../src/skill/discovery"
  3. import path from "path"
  4. const CLOUDFLARE_SKILLS_URL = "https://developers.cloudflare.com/.well-known/skills/"
  5. describe("Discovery.pull", () => {
  6. test("downloads skills from cloudflare url", async () => {
  7. const dirs = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
  8. expect(dirs.length).toBeGreaterThan(0)
  9. for (const dir of dirs) {
  10. expect(dir).toStartWith(Discovery.dir())
  11. const md = path.join(dir, "SKILL.md")
  12. expect(await Bun.file(md).exists()).toBe(true)
  13. }
  14. }, 30_000)
  15. test("url without trailing slash works", async () => {
  16. const dirs = await Discovery.pull(CLOUDFLARE_SKILLS_URL.replace(/\/$/, ""))
  17. expect(dirs.length).toBeGreaterThan(0)
  18. for (const dir of dirs) {
  19. const md = path.join(dir, "SKILL.md")
  20. expect(await Bun.file(md).exists()).toBe(true)
  21. }
  22. }, 30_000)
  23. test("returns empty array for invalid url", async () => {
  24. const dirs = await Discovery.pull("https://example.invalid/.well-known/skills/")
  25. expect(dirs).toEqual([])
  26. })
  27. test("returns empty array for non-json response", async () => {
  28. const dirs = await Discovery.pull("https://example.com/")
  29. expect(dirs).toEqual([])
  30. })
  31. test("downloads reference files alongside SKILL.md", async () => {
  32. const dirs = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
  33. // find a skill dir that should have reference files (e.g. agents-sdk)
  34. const agentsSdk = dirs.find((d) => d.endsWith("/agents-sdk"))
  35. if (agentsSdk) {
  36. const refs = path.join(agentsSdk, "references")
  37. expect(await Bun.file(path.join(agentsSdk, "SKILL.md")).exists()).toBe(true)
  38. // agents-sdk has reference files per the index
  39. const refDir = await Array.fromAsync(new Bun.Glob("**/*.md").scan({ cwd: refs, onlyFiles: true }))
  40. expect(refDir.length).toBeGreaterThan(0)
  41. }
  42. }, 30_000)
  43. test("caches downloaded files on second pull", async () => {
  44. // first pull to populate cache
  45. const first = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
  46. expect(first.length).toBeGreaterThan(0)
  47. // second pull should return same results from cache
  48. const second = await Discovery.pull(CLOUDFLARE_SKILLS_URL)
  49. expect(second.length).toBe(first.length)
  50. expect(second.sort()).toEqual(first.sort())
  51. }, 60_000)
  52. })