|
| 1 | +import { Atom } from '@systemfsoftware/effect-atom' |
| 2 | +import { AtomReact } from '@systemfsoftware/effect-atom-react' |
| 3 | +import { Gherkin, Given, it, makeFeature, Then, When } from '@systemfsoftware/effect-gherkin-spec' |
| 4 | +import { vi } from '@systemfsoftware/vitest' |
| 5 | +import { render } from '@testing-library/react' |
| 6 | +import '@vitest/browser/matchers' |
| 7 | +import * as Cause from 'effect/Cause' |
| 8 | +import * as Context from 'effect/Context' |
| 9 | +import * as Effect from 'effect/Effect' |
| 10 | +import * as Layer from 'effect/Layer' |
| 11 | +import { createElement } from 'react' |
| 12 | +import { ownRegistry, renderCleanupLayer } from './__fixtures__/render-cleanup.js' |
| 13 | + |
| 14 | +const Feature = makeFeature({ it }) |
| 15 | + |
| 16 | +interface Gateway { |
| 17 | + readonly build: number |
| 18 | +} |
| 19 | + |
| 20 | +class GatewayService extends Context.Service<GatewayService, Gateway>()( |
| 21 | + '@systemfsoftware/effect-atom-react/tests/multiple-roots-sharing-a-runtime.integration.test/GatewayService', |
| 22 | +) {} |
| 23 | + |
| 24 | +const makeGatewayLayer = () => { |
| 25 | + const counts = { built: 0, released: 0 } |
| 26 | + const layer = Layer.effect( |
| 27 | + GatewayService, |
| 28 | + Effect.acquireRelease( |
| 29 | + Effect.sync((): Gateway => ({ build: ++counts.built })), |
| 30 | + () => Effect.sync(() => void counts.released++), |
| 31 | + ), |
| 32 | + ) |
| 33 | + return { counts, layer } |
| 34 | +} |
| 35 | + |
| 36 | +const mountRoot = ( |
| 37 | + registry: Atom.Registry.Registry, |
| 38 | + gateway: Atom.Atom<Atom.AsyncResult.Result<Gateway | Context.Context<GatewayService>, Cause.NoSuchElementError>>, |
| 39 | +) => { |
| 40 | + const seen: Gateway[] = [] |
| 41 | + function Island() { |
| 42 | + const result = AtomReact.useAtomValue(gateway) |
| 43 | + if (!Atom.AsyncResult.isSuccess(result)) { |
| 44 | + return createElement('div', { 'data-testid': 'gateway-loading' }) |
| 45 | + } |
| 46 | + if (!Context.isContext(result.value)) { |
| 47 | + seen.push(result.value) |
| 48 | + } |
| 49 | + return createElement('div', { 'data-testid': 'gateway-ready' }) |
| 50 | + } |
| 51 | + const view = render( |
| 52 | + createElement(AtomReact.RegistryProvider, { registry }, createElement(Island)), |
| 53 | + { container: document.body.appendChild(document.createElement('div')) }, |
| 54 | + ) |
| 55 | + return { seen, view } |
| 56 | +} |
| 57 | + |
| 58 | +const registryWorkSettled = Effect.callback<void>((resume) => { |
| 59 | + const cancel = AtomReact.scheduleTask(() => resume(Effect.void)) |
| 60 | + return Effect.sync(cancel) |
| 61 | +}) |
| 62 | + |
| 63 | +Feature('Several roots on one page sharing one registry and one built layer') |
| 64 | + .live('renders real React roots in Chromium') |
| 65 | + .withLayer(Layer.empty) |
| 66 | + .withScenarioLayer(renderCleanupLayer) |
| 67 | + .body(({ scenario }) => { |
| 68 | + scenario( |
| 69 | + 'A second root mounted after the first one unmounted reuses the layer the first root built', |
| 70 | + Gherkin.Do.pipe( |
| 71 | + Given('a page registry shared by every root and a runtime kept alive')('ctx', () => |
| 72 | + Effect.sync(() => { |
| 73 | + const { counts, layer } = makeGatewayLayer() |
| 74 | + const runtime = Atom.keepAlive(Atom.context()(layer)) |
| 75 | + const gateway = runtime.atom((_get) => GatewayService.use((service) => Effect.succeed(service))) |
| 76 | + return { counts, gateway, registry: ownRegistry() } |
| 77 | + })), |
| 78 | + When('root A reads the service and unmounts, and then root B mounts on the same registry')( |
| 79 | + 'roots', |
| 80 | + (s) => |
| 81 | + Effect.gen(function*() { |
| 82 | + const a = mountRoot(s.ctx.registry, s.ctx.gateway) |
| 83 | + yield* Effect.promise(() => vi.waitFor(() => expectReady(a.seen))) |
| 84 | + a.view.unmount() |
| 85 | + yield* registryWorkSettled |
| 86 | + const b = mountRoot(s.ctx.registry, s.ctx.gateway) |
| 87 | + yield* Effect.promise(() => vi.waitFor(() => expectReady(b.seen))) |
| 88 | + return { a: a.seen, b: b.seen } |
| 89 | + }), |
| 90 | + ), |
| 91 | + Then('both roots hold the identical service and the layer was built exactly once')((s, expect) => |
| 92 | + expect({ |
| 93 | + built: s.ctx.counts.built, |
| 94 | + released: s.ctx.counts.released, |
| 95 | + sameInstance: s.roots.a.at(-1) === s.roots.b.at(-1), |
| 96 | + instance: s.roots.b.at(-1)?.build, |
| 97 | + }).toEqual({ built: 1, released: 0, sameInstance: true, instance: 1 }) |
| 98 | + ), |
| 99 | + ), |
| 100 | + ) |
| 101 | + |
| 102 | + scenario( |
| 103 | + 'A fresh registry per test case builds and releases its own layer', |
| 104 | + Gherkin.Do.pipe( |
| 105 | + Given('the same kept-alive runtime and a layer that counts its builds and releases')( |
| 106 | + 'ctx', |
| 107 | + () => |
| 108 | + Effect.sync(() => { |
| 109 | + const { counts, layer } = makeGatewayLayer() |
| 110 | + const runtime = Atom.keepAlive(Atom.context()(layer)) |
| 111 | + const gateway = runtime.atom((_get) => GatewayService.use((service) => Effect.succeed(service))) |
| 112 | + return { counts, gateway } |
| 113 | + }), |
| 114 | + ), |
| 115 | + When('two cases each mint a registry, read the service and dispose the registry')( |
| 116 | + 'cases', |
| 117 | + (s) => |
| 118 | + Effect.gen(function*() { |
| 119 | + const instances: Gateway[] = [] |
| 120 | + for (const _case of [1, 2]) { |
| 121 | + const registry = Atom.Registry.make({ scheduleTask: AtomReact.scheduleTask }) |
| 122 | + const mounted = mountRoot(registry, s.ctx.gateway) |
| 123 | + yield* Effect.promise(() => vi.waitFor(() => expectReady(mounted.seen))) |
| 124 | + const latest = mounted.seen.at(-1) |
| 125 | + if (latest !== undefined) { |
| 126 | + instances.push(latest) |
| 127 | + } |
| 128 | + mounted.view.unmount() |
| 129 | + Atom.Registry.dispose(registry) |
| 130 | + } |
| 131 | + yield* Effect.promise(() => vi.waitFor(() => expectReleased(s.ctx.counts))) |
| 132 | + return { instances } |
| 133 | + }), |
| 134 | + ), |
| 135 | + Then('each case built its own layer and released it on dispose')((s, expect) => |
| 136 | + expect({ |
| 137 | + counts: s.ctx.counts, |
| 138 | + builds: s.cases.instances.map((gateway) => gateway.build), |
| 139 | + }).toEqual({ counts: { built: 2, released: 2 }, builds: [1, 2] }) |
| 140 | + ), |
| 141 | + ), |
| 142 | + ) |
| 143 | + }) |
| 144 | + |
| 145 | +const expectReady = (seen: ReadonlyArray<Gateway>) => { |
| 146 | + if (seen.length === 0) { |
| 147 | + throw new Error('the root has not received the service yet') |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +const expectReleased = (counts: { built: number; released: number }) => { |
| 152 | + if (counts.released < counts.built) { |
| 153 | + throw new Error('a layer is still held') |
| 154 | + } |
| 155 | +} |
0 commit comments