network.test.ts 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. import { expect, test } from "bun:test"
  2. import { Effect, Exit } from "effect"
  3. import { TestClock } from "effect/testing"
  4. import { HttpClientRequest } from "effect/unstable/http"
  5. import { SimulationNetwork } from "../src/backend/network"
  6. test("keeps routes and request logs local to each network", async () => {
  7. await Effect.runPromise(
  8. Effect.scoped(
  9. Effect.gen(function* () {
  10. yield* TestClock.setTime(1_234)
  11. const first = yield* SimulationNetwork.make([
  12. SimulationNetwork.json("GET", "https://example.test/value", { source: "first" }),
  13. ])
  14. const second = yield* SimulationNetwork.make()
  15. const request = HttpClientRequest.get("https://example.test/value")
  16. const response = yield* first.client.execute(request)
  17. expect(yield* response.text).toBe('{"source":"first"}')
  18. expect(Exit.isFailure(yield* second.client.execute(request).pipe(Effect.exit))).toBe(true)
  19. expect(yield* first.log()).toEqual([
  20. { time: 1_234, method: "GET", url: "https://example.test/value", matched: true },
  21. ])
  22. expect(yield* second.log()).toEqual([
  23. { time: 1_234, method: "GET", url: "https://example.test/value", matched: false },
  24. ])
  25. }).pipe(Effect.provide(TestClock.layer())),
  26. ),
  27. )
  28. })