test(skill): migrate discovery tests to Effect runner (#27127)

This commit is contained in:
Kit Langton
2026-05-12 19:39:03 +00:00
committed by GitHub
parent 45de4975de
commit ec960da42a
+73 -58
View File
@@ -1,10 +1,11 @@
import { describe, test, expect, beforeAll, afterAll } from "bun:test" import { describe, expect, beforeAll, afterAll } from "bun:test"
import { Effect } from "effect" import { Effect } from "effect"
import { Discovery } from "../../src/skill/discovery" import { Discovery } from "../../src/skill/discovery"
import { Global } from "@opencode-ai/core/global" import { Global } from "@opencode-ai/core/global"
import { Filesystem } from "@/util/filesystem" import { Filesystem } from "@/util/filesystem"
import { rm } from "fs/promises" import { rm } from "fs/promises"
import path from "path" import path from "path"
import { testEffect } from "../lib/effect"
let CLOUDFLARE_SKILLS_URL: string let CLOUDFLARE_SKILLS_URL: string
let server: ReturnType<typeof Bun.serve> let server: ReturnType<typeof Bun.serve>
@@ -12,6 +13,7 @@ let downloadCount = 0
const fixturePath = path.join(import.meta.dir, "../fixture/skills") const fixturePath = path.join(import.meta.dir, "../fixture/skills")
const cacheDir = path.join(Global.Path.cache, "skills") const cacheDir = path.join(Global.Path.cache, "skills")
const it = testEffect(Discovery.defaultLayer)
beforeAll(async () => { beforeAll(async () => {
await rm(cacheDir, { recursive: true, force: true }) await rm(cacheDir, { recursive: true, force: true })
@@ -47,70 +49,83 @@ afterAll(async () => {
}) })
describe("Discovery.pull", () => { describe("Discovery.pull", () => {
const pull = (url: string) => const pull = Effect.fn("DiscoveryTest.pull")(function* (url: string) {
Effect.runPromise(Discovery.Service.use((s) => s.pull(url)).pipe(Effect.provide(Discovery.defaultLayer))) return yield* Discovery.Service.use((s) => s.pull(url))
test("downloads skills from cloudflare url", async () => {
const dirs = await pull(CLOUDFLARE_SKILLS_URL)
expect(dirs.length).toBeGreaterThan(0)
for (const dir of dirs) {
expect(dir).toStartWith(cacheDir)
const md = path.join(dir, "SKILL.md")
expect(await Filesystem.exists(md)).toBe(true)
}
}) })
test("url without trailing slash works", async () => { it.live("downloads skills from cloudflare url", () =>
const dirs = await pull(CLOUDFLARE_SKILLS_URL.replace(/\/$/, "")) Effect.gen(function* () {
expect(dirs.length).toBeGreaterThan(0) const dirs = yield* pull(CLOUDFLARE_SKILLS_URL)
for (const dir of dirs) { expect(dirs.length).toBeGreaterThan(0)
const md = path.join(dir, "SKILL.md") for (const dir of dirs) {
expect(await Filesystem.exists(md)).toBe(true) expect(dir).toStartWith(cacheDir)
} const md = path.join(dir, "SKILL.md")
}) expect(yield* Effect.promise(() => Filesystem.exists(md))).toBe(true)
}
}),
)
test("returns empty array for invalid url", async () => { it.live("url without trailing slash works", () =>
const dirs = await pull(`http://localhost:${server.port}/invalid-url/`) Effect.gen(function* () {
expect(dirs).toEqual([]) const dirs = yield* pull(CLOUDFLARE_SKILLS_URL.replace(/\/$/, ""))
}) expect(dirs.length).toBeGreaterThan(0)
for (const dir of dirs) {
const md = path.join(dir, "SKILL.md")
expect(yield* Effect.promise(() => Filesystem.exists(md))).toBe(true)
}
}),
)
test("returns empty array for non-json response", async () => { it.live("returns empty array for invalid url", () =>
// any url not explicitly handled in server returns 404 text "Not Found" Effect.gen(function* () {
const dirs = await pull(`http://localhost:${server.port}/some-other-path/`) const dirs = yield* pull(`http://localhost:${server.port}/invalid-url/`)
expect(dirs).toEqual([]) expect(dirs).toEqual([])
}) }),
)
test("downloads reference files alongside SKILL.md", async () => { it.live("returns empty array for non-json response", () =>
const dirs = await pull(CLOUDFLARE_SKILLS_URL) Effect.gen(function* () {
// find a skill dir that should have reference files (e.g. agents-sdk) // any url not explicitly handled in server returns 404 text "Not Found"
const agentsSdk = dirs.find((d) => d.endsWith(path.sep + "agents-sdk")) const dirs = yield* pull(`http://localhost:${server.port}/some-other-path/`)
expect(agentsSdk).toBeDefined() expect(dirs).toEqual([])
if (agentsSdk) { }),
const refs = path.join(agentsSdk, "references") )
expect(await Filesystem.exists(path.join(agentsSdk, "SKILL.md"))).toBe(true)
// agents-sdk has reference files per the index
const refDir = await Array.fromAsync(new Bun.Glob("**/*.md").scan({ cwd: refs, onlyFiles: true }))
expect(refDir.length).toBeGreaterThan(0)
}
})
test("caches downloaded files on second pull", async () => { it.live("downloads reference files alongside SKILL.md", () =>
// clear dir and downloadCount Effect.gen(function* () {
await rm(cacheDir, { recursive: true, force: true }) const dirs = yield* pull(CLOUDFLARE_SKILLS_URL)
downloadCount = 0 // find a skill dir that should have reference files (e.g. agents-sdk)
const agentsSdk = dirs.find((d) => d.endsWith(path.sep + "agents-sdk"))
expect(agentsSdk).toBeDefined()
if (agentsSdk) {
const refs = path.join(agentsSdk, "references")
expect(yield* Effect.promise(() => Filesystem.exists(path.join(agentsSdk, "SKILL.md")))).toBe(true)
// agents-sdk has reference files per the index
const refDir = yield* Effect.promise(() => Array.fromAsync(new Bun.Glob("**/*.md").scan({ cwd: refs, onlyFiles: true })))
expect(refDir.length).toBeGreaterThan(0)
}
}),
)
// first pull to populate cache it.live("caches downloaded files on second pull", () =>
const first = await pull(CLOUDFLARE_SKILLS_URL) Effect.gen(function* () {
expect(first.length).toBeGreaterThan(0) // clear dir and downloadCount
const firstCount = downloadCount yield* Effect.promise(() => rm(cacheDir, { recursive: true, force: true }))
expect(firstCount).toBeGreaterThan(0) downloadCount = 0
// second pull should return same results from cache // first pull to populate cache
const second = await pull(CLOUDFLARE_SKILLS_URL) const first = yield* pull(CLOUDFLARE_SKILLS_URL)
expect(second.length).toBe(first.length) expect(first.length).toBeGreaterThan(0)
expect(second.sort()).toEqual(first.sort()) const firstCount = downloadCount
expect(firstCount).toBeGreaterThan(0)
// second pull should NOT increment download count // second pull should return same results from cache
expect(downloadCount).toBe(firstCount) const second = yield* pull(CLOUDFLARE_SKILLS_URL)
}) expect(second.length).toBe(first.length)
expect(second.sort()).toEqual(first.sort())
// second pull should NOT increment download count
expect(downloadCount).toBe(firstCount)
}),
)
}) })