discovery.test.ts 2.4 KB

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