system.test.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { describe, expect, test } from "bun:test"
  2. import path from "path"
  3. import { Effect } from "effect"
  4. import { Agent } from "../../src/agent/agent"
  5. import { Instance } from "../../src/project/instance"
  6. import { SystemPrompt } from "../../src/session"
  7. import { provideInstance, tmpdir } from "../fixture/fixture"
  8. function load<A>(dir: string, fn: (svc: Agent.Interface) => Effect.Effect<A>) {
  9. return Effect.runPromise(provideInstance(dir)(Agent.Service.use(fn)).pipe(Effect.provide(Agent.defaultLayer)))
  10. }
  11. describe("session.system", () => {
  12. test("skills output is sorted by name and stable across calls", async () => {
  13. await using tmp = await tmpdir({
  14. git: true,
  15. init: async (dir) => {
  16. for (const [name, description] of [
  17. ["zeta-skill", "Zeta skill."],
  18. ["alpha-skill", "Alpha skill."],
  19. ["middle-skill", "Middle skill."],
  20. ]) {
  21. const skillDir = path.join(dir, ".opencode", "skill", name)
  22. await Bun.write(
  23. path.join(skillDir, "SKILL.md"),
  24. `---
  25. name: ${name}
  26. description: ${description}
  27. ---
  28. # ${name}
  29. `,
  30. )
  31. }
  32. },
  33. })
  34. const home = process.env.OPENCODE_TEST_HOME
  35. process.env.OPENCODE_TEST_HOME = tmp.path
  36. try {
  37. await Instance.provide({
  38. directory: tmp.path,
  39. fn: async () => {
  40. const build = await load(tmp.path, (svc) => svc.get("build"))
  41. const runSkills = Effect.gen(function* () {
  42. const svc = yield* SystemPrompt.Service
  43. return yield* svc.skills(build!)
  44. }).pipe(Effect.provide(SystemPrompt.defaultLayer))
  45. const first = await Effect.runPromise(runSkills)
  46. const second = await Effect.runPromise(runSkills)
  47. expect(first).toBe(second)
  48. const alpha = first!.indexOf("<name>alpha-skill</name>")
  49. const middle = first!.indexOf("<name>middle-skill</name>")
  50. const zeta = first!.indexOf("<name>zeta-skill</name>")
  51. expect(alpha).toBeGreaterThan(-1)
  52. expect(middle).toBeGreaterThan(alpha)
  53. expect(zeta).toBeGreaterThan(middle)
  54. },
  55. })
  56. } finally {
  57. process.env.OPENCODE_TEST_HOME = home
  58. }
  59. })
  60. })