session-system-context.test.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Layer } from "effect"
  3. import * as TestClock from "effect/testing/TestClock"
  4. import { Location } from "@opencode-ai/core/location"
  5. import { AbsolutePath } from "@opencode-ai/core/schema"
  6. import { SessionSystemContext } from "@opencode-ai/core/session-system-context"
  7. import { SystemContext } from "@opencode-ai/core/system-context"
  8. import { location } from "./fixture/location"
  9. import { testEffect } from "./lib/effect"
  10. const directory = AbsolutePath.make("/repo/packages/core")
  11. const projectDirectory = AbsolutePath.make("/repo")
  12. const timestamp = Date.parse("2026-06-03T12:00:00.000Z")
  13. const localDate = (time: number) => new Date(time).toDateString()
  14. const it = testEffect(
  15. SessionSystemContext.locationLayer.pipe(
  16. Layer.provide(
  17. Layer.succeed(
  18. Location.Service,
  19. Location.Service.of(
  20. location({ directory }, { projectDirectory, vcs: { type: "git", store: AbsolutePath.make("/repo/.git") } }),
  21. ),
  22. ),
  23. ),
  24. ),
  25. )
  26. describe("SessionSystemContext", () => {
  27. it.effect("loads location-scoped environment and host-local date context", () =>
  28. Effect.gen(function* () {
  29. yield* TestClock.setTime(timestamp)
  30. const context = yield* SessionSystemContext.Service
  31. const initialized = SystemContext.initialize(yield* context.load())
  32. expect(initialized.baseline).toEqual([
  33. {
  34. key: SystemContext.Key.make("core/environment"),
  35. text: [
  36. "Here is some useful information about the environment you are running in:",
  37. "<env>",
  38. ` Working directory: ${directory}`,
  39. ` Workspace root folder: ${projectDirectory}`,
  40. " Is directory a git repo: yes",
  41. ` Platform: ${process.platform}`,
  42. "</env>",
  43. ].join("\n"),
  44. },
  45. { key: SystemContext.Key.make("core/date"), text: `Today's date: ${localDate(timestamp)}` },
  46. ])
  47. }),
  48. )
  49. it.effect("refreshes the date without repeating unchanged environment context", () =>
  50. Effect.gen(function* () {
  51. yield* TestClock.setTime(timestamp)
  52. const context = yield* SessionSystemContext.Service
  53. const initialized = SystemContext.initialize(yield* context.load())
  54. yield* TestClock.setTime(timestamp + 24 * 60 * 60 * 1000)
  55. const refreshed = SystemContext.refresh(yield* context.load(), initialized.checkpoint)
  56. expect(refreshed.changes).toEqual([
  57. {
  58. key: SystemContext.Key.make("core/date"),
  59. text: `Today's date is now: ${localDate(timestamp + 24 * 60 * 60 * 1000)}`,
  60. },
  61. ])
  62. }),
  63. )
  64. })