@systemfsoftware/alchemy-stack-layer 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +106 -47
  3. package/package.json +4 -4
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @systemfsoftware/alchemy-stack-layer
2
+
3
+ # @systemfsoftware/alchemy-stack-layer@0.1.0
4
+
5
+ Rewrites the README: install with every peer dependency (adds `@effect/platform-node`), a quick start, a table of where to pass the layer and how often it deploys, how dev and cloud resolve, how stages are named, and the errors each failure raises.
6
+
7
+ New package. `alchemyStack(name, config)` returns a service tag, distinct for each stack name, whose service is the stack's outputs, with a `layer` that deploys the stack on `@alchemy.run/test`'s engine when acquired and destroys it when released. `config.deploy` takes a stack definition carrying its own `providers` and `state`, or a function that receives `deploy` and returns the outputs, with an optional `setup` layer for what that function needs; stacks are destroyed in reverse deploy order, and each is registered for destroy before its deploy starts. Dev mode and sidecar resolve as `@alchemy.run/test` resolves them; `dev`, `stage`, `sidecar` and `profile` are forwarded unchanged. Without a configured `stage`, the stage starts from a non-empty `ALCHEMY_TEST_STAGE`, otherwise from `test_$USER_<worktree>` when the working directory is inside a linked git worktree, as `@alchemy.run/test`'s runner names it, so two worktrees of one checkout never share a stage, and otherwise from `test_$USER`; it then gets `_` and 8 hex digits of the SHA-256 of the running test file's Vitest project name, root directory name and path from the root; `fileStage({ baseStage, testFile })` is exported to compute that stage outside a test. Before deploying, the layer destroys any deployment left at the stage; if that fails, acquiring fails with `StackDestroyFailed` and nothing new is deployed. A cloud deploy over local or in-memory state fails with `DurableStateRequired` before it deploys; a failed deploy fails with `StackDeployFailed` and a failed destroy with `StackDestroyFailed`, each naming the stack and the stage; a stack with no configured stage built outside a running test file, or whose `ALCHEMY_TEST_STAGE` cannot be read, fails with `StageUnresolved`. `alchemy`, `effect` and `@effect/platform-node` are peer dependencies.
package/README.md CHANGED
@@ -1,45 +1,70 @@
1
1
  # @systemfsoftware/alchemy-stack-layer
2
2
 
3
- Deploy an [Alchemy](https://alchemy.run) stack as an [Effect](https://effect.website) layer, so a Gherkin Feature reads a deployed stack's outputs as a service.
3
+ Integration tests against a deployed [Alchemy](https://alchemy.run) stack, written as plain [Effect](https://effect.website) layers. You declare the stack; the layer deploys it before your scenarios run and destroys it when they finish. The same test file runs against the local Workers runtime or a real Cloudflare account, chosen by Alchemy's own `ALCHEMY_TEST_DEV` toggle.
4
4
 
5
- `alchemyStack(name, config)` returns a service tag, distinct for each stack name, whose service is the stack's outputs. Its `layer` deploys the stack when acquired and destroys it when released, on `@alchemy.run/test`'s engine. You declare no service class and no output mapping:
5
+ It is built for Gherkin Features written with [`@systemfsoftware/effect-gherkin-spec`](https://www.npmjs.com/package/@systemfsoftware/effect-gherkin-spec), but the layer is an ordinary `Layer`, so anything that accepts one can use it.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add -D @systemfsoftware/alchemy-stack-layer alchemy effect @effect/platform-node
11
+ ```
12
+
13
+ > [!NOTE]
14
+ > `alchemy`, `effect` and `@effect/platform-node` are peer dependencies, so the versions are yours. `@alchemy.run/test` comes with the package.
15
+
16
+ ## Quick start
17
+
18
+ Name the stack, hand its layer to a Feature, and read its outputs in a step through the tag:
6
19
 
7
20
  ```ts
8
21
  import { alchemyStack } from '@systemfsoftware/alchemy-stack-layer'
9
- import { Gherkin, Given, it, makeFeature, Then } from '@systemfsoftware/effect-gherkin-spec'
22
+ import { Gherkin, Given, it, makeFeature, sharedLayer, Then } from '@systemfsoftware/effect-gherkin-spec'
10
23
  import SignInStack from '../alchemy.run.ts'
11
24
 
12
25
  const Feature = makeFeature({ it })
13
26
 
14
27
  const SignIn = alchemyStack('SignIn', { deploy: SignInStack })
15
28
 
16
- Feature('signing in')
17
- .live('the sign-in worker answers over the network')
18
- .withLayer(SignIn.layer)
19
- .body(({ scenario }) => {
20
- scenario(
21
- 'the deployed worker has a URL',
22
- Gherkin.Do.pipe(
23
- Given('the sign-in stack')('stack', () => SignIn),
24
- Then('it serves from a URL')((s, expect) => expect(s.stack.url.startsWith('https://')).toBe(true)),
25
- ),
26
- )
27
- })
29
+ const Shared = sharedLayer(SignIn.layer)
30
+
31
+ Feature('signing in').withLayer(Shared).body(({ scenario }) => {
32
+ scenario(
33
+ 'the deployed worker has a URL',
34
+ Gherkin.Do.pipe(
35
+ Given('the sign-in stack')('stack', () => SignIn),
36
+ Then('it serves over HTTPS')((s, expect) => expect(s.stack.url.startsWith('https://')).toBe(true)),
37
+ ),
38
+ )
39
+ })
28
40
  ```
29
41
 
30
- Passed to `.withLayer`, the stack deploys once for the Feature. Passed through `sharedLayer` from `@systemfsoftware/effect-gherkin-spec`, it deploys once for the whole file, and every Feature in the file reads the same deployment:
42
+ There is no service class to declare, no output mapping, no `beforeAll` and no timeout. The tag's service is the stack's outputs, typed from the stack definition, and the tag is distinct for each stack name, so two stacks never collide in one layer graph.
31
43
 
32
- ```ts
33
- const Shared = sharedLayer(SignIn.layer)
44
+ ## Sharing one deployment
34
45
 
35
- Feature('signing in').withLayer(Shared).body() /* ... */
36
- ```
46
+ Where you pass the layer decides how often the stack deploys:
47
+
48
+ | Passed as | Deploys |
49
+ | -------------------------------------- | ------------------------------------------------------ |
50
+ | `.withLayer(sharedLayer(Stack.layer))` | once per test file, shared by every Feature in it |
51
+ | `.withLayer(Stack.layer)` | once per Feature |
52
+ | `.withScenarioLayer(Stack.layer)` | once per scenario, which is almost never what you want |
37
53
 
38
- A stack's deploy runs in the shared layer's setup, which has no time limit, so a slow deploy needs no `.live` or timeout of its own. Give stacks to `.withLayer`, not `.withScenarioLayer`: a scenario layer is built for every scenario, and every scenario would deploy. Scenarios that share one deployment must not depend on each other's writes to it.
54
+ A shared layer is built in the file's setup, before the first scenario starts, with no time limit. A deploy that takes minutes against Cloudflare therefore needs no `.live` and no raised timeout. Scenarios that share a deployment should not depend on each other's writes to it.
55
+
56
+ `.withLayer` chains, and a later layer may use what an earlier one provides. A client built over the stack's URL is one more layer:
57
+
58
+ ```ts
59
+ Feature('signing in')
60
+ .withLayer(Shared)
61
+ .withLayer(SignInClient.layer) // requires SignIn; a missing requirement is a compile error
62
+ .body() /* ... */
63
+ ```
39
64
 
40
- ## Deploying more than one stack
65
+ ## Deploying several stacks
41
66
 
42
- `config.deploy` takes a stack definition with its own `providers` and `state`, as `Alchemy.Stack(name, { providers, state }, effect)` returns. It also takes a function that receives `deploy` and returns the outputs, for a setup that deploys several stacks or provisions a dependency first. A `setup` layer provides what that function needs:
67
+ `deploy` also takes a function. It receives a `deploy` you call once per stack, and returns whatever outputs your steps should see. Give it a `setup` layer when it needs something first, such as a database the worker connects to:
43
68
 
44
69
  ```ts
45
70
  const Billing = alchemyStack('Billing', {
@@ -54,40 +79,74 @@ const Billing = alchemyStack('Billing', {
54
79
  })
55
80
  ```
56
81
 
57
- Stacks are destroyed in reverse deploy order. A stack is registered for destroy before its deploy starts, so a deploy that fails part-way still has what it created destroyed.
82
+ `Billing.layer` then requires nothing further. Stacks are destroyed in reverse deploy order. Each stack is registered for destroy before its deploy starts, so a deploy that fails halfway still has what it created torn down.
58
83
 
59
- ## Dev or cloud
84
+ ## Local or cloud
60
85
 
61
- Dev mode, the sidecar and the stage resolve exactly as `@alchemy.run/test` resolves them, from one code path:
86
+ The package adds no switch of its own. Dev mode resolves exactly as `@alchemy.run/test` resolves it, from one code path:
62
87
 
63
- - **Dev:** `ALCHEMY_TEST_DEV`, then the config's `dev`, then `ALCHEMY_DEV`. A local run starts the dev sidecar and deploys to the local Workers runtime; `ALCHEMY_TEST_DEV=0` deploys the same file to the cloud.
64
- - **Stage:** the config's `stage` is used as given. Without one, the stage is a non-empty `ALCHEMY_TEST_STAGE`; else, when the working directory is inside a linked git worktree, `test_$USER_<worktree>`, as `@alchemy.run/test`'s runner names it, so two worktrees running the same file at once never share a deployment; else the per-user test stage. It is followed by `_` and 8 hex digits of the SHA-256 of the test file's Vitest project name, root directory name and path from the root (see `fileStage` below). Two files that deploy the same stack never share a deployment either.
65
- - **State:** where a deployment records its state is the stack definition's own `state`. A cloud run over local or in-memory state fails before it deploys, with `DurableStateRequired`: a cloud deployment recorded only on one machine cannot be found or destroyed from another.
88
+ 1. `ALCHEMY_TEST_DEV`, if set;
89
+ 2. else the config's `dev`;
90
+ 3. else `ALCHEMY_DEV`.
66
91
 
67
- The config's `dev`, `stage`, `sidecar` and `profile` are forwarded to the engine unchanged.
92
+ In dev mode the layer starts Alchemy's sidecar and deploys to the local Workers runtime. With `ALCHEMY_TEST_DEV=0` the same file deploys to Cloudflare:
68
93
 
69
- `fileStage({ baseStage, testFile })` is exported for scripts that need the stage a test file deploys under, such as a CI job that checks a stage is empty. `testFile` is effect-spec-runtime's `TestFileInfo`: the file's path from the Vitest root, the root directory's name, and the Vitest project name. The suffix is 8 hex digits of the SHA-256 of those three joined by NUL bytes, so a file keeps its stage across checkouts and machines, and two packages with the same test path get different stages. `fileStage` needs a `Digest` service, such as `Digest.layerWebCrypto` from `effect/crypto/Digest`.
94
+ ```bash
95
+ ALCHEMY_TEST_DEV=0 ALCHEMY_TEST_STAGE=ci_main pnpm vitest run
96
+ ```
70
97
 
71
- ## Teardown
98
+ `ALCHEMY_TEST_DEV` is read once per process, so set it in the environment, not inside a test.
72
99
 
73
- - Before deploying, the layer destroys any deployment left at the resolved stage, so a run killed before its teardown is cleaned up by the next run at that stage. If that destroy fails, acquiring fails with `StackDestroyFailed` and nothing new is deployed. Give each CI job slot a stage that repeats, and run one job at a time per stage.
74
- - A failed destroy fails the run with `StackDestroyFailed`, naming the stack and the stage. It is never swallowed and never retried.
75
- - A failed deploy fails with `StackDeployFailed`, naming the stack and the stage, after what it created has been destroyed.
76
- - A stack built with no configured stage outside a running test file fails with `StageUnresolved`.
100
+ A cloud run needs durable state. A stack whose definition records state locally or in memory fails before it deploys, with `DurableStateRequired`, because a deployment recorded on one machine cannot be found or destroyed from another.
77
101
 
78
- ## Checking against real Cloudflare
102
+ The config's `dev`, `stage`, `sidecar` and `profile` are passed to Alchemy unchanged.
79
103
 
80
- `test:cloud` runs a one-worker stack against a real Cloudflare account: it deploys, fetches the worker over HTTP, destroys it, and checks that the stage holds no state rows and the worker is gone. It needs `CLOUDFLARE_API_TOKEN`, `CLOUDFLARE_ACCOUNT_ID`, `ALCHEMY_TEST_DEV=0` and an `ALCHEMY_TEST_STAGE`:
104
+ ## Stages
81
105
 
82
- ```bash
83
- ALCHEMY_TEST_DEV=0 ALCHEMY_TEST_STAGE=<stage> pnpm --filter @systemfsoftware/alchemy-stack-layer test:cloud
84
- ```
106
+ Every deployment lives under a stage. A `stage` in the config is used exactly as given. Without one, the stage is built from two parts.
85
107
 
86
- ## Install
108
+ The base is the first of:
87
109
 
88
- ```bash
89
- pnpm add -D @systemfsoftware/alchemy-stack-layer alchemy effect
90
- ```
110
+ - a non-empty `ALCHEMY_TEST_STAGE`;
111
+ - `test_<user>_<worktree>` inside a linked git worktree, so two worktrees running the same file at once never share a deployment;
112
+ - `test_<user>`.
91
113
 
92
- > [!NOTE]
93
- > `alchemy` and `effect` are peer dependencies — you bring your own.
114
+ The suffix is `_` plus 8 hex digits of a SHA-256 over the test file's Vitest project name, root directory name and path from the root. Two files that deploy the same stack get different stages. One file keeps the same stage across runs, machines and checkouts, which is what lets the next run find what a killed run left behind.
115
+
116
+ `fileStage({ baseStage, testFile })` computes that stage outside a test, for a CI job that checks a stage is empty after a run. It needs a `Digest` service such as `Digest.layerWebCrypto` from `effect/crypto/Digest`.
117
+
118
+ ## Teardown and errors
119
+
120
+ Teardown runs when the layer is released, on success, failure and interruption. It has no opt-out.
121
+
122
+ Before deploying, the layer destroys any deployment already recorded at its stage. A run killed before its teardown is cleaned up by the next run at that stage. In CI, give each branch or job slot a stage that repeats, and run one job at a time per stage.
123
+
124
+ Every failure is a tagged error that names the stack and the stage:
125
+
126
+ | Error | When |
127
+ | ---------------------- | --------------------------------------------------------------------------------------------------- |
128
+ | `StackDeployFailed` | a deploy failed; whatever it created has been destroyed |
129
+ | `StackDestroyFailed` | a destroy failed, including the cleanup of a leftover deployment; the run fails, nothing is retried |
130
+ | `DurableStateRequired` | a cloud run was given local or in-memory state; nothing was deployed or destroyed |
131
+ | `StageUnresolved` | a stack with no configured stage was built outside a running test file |
132
+
133
+ ## API
134
+
135
+ | Export | What it is |
136
+ | ------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------- |
137
+ | `alchemyStack(name, config)` | returns a tag for the stack's outputs, with `.layer` to deploy and destroy it |
138
+ | `config.deploy` | a stack definition, or a function `(deploy) => Effect` returning the outputs |
139
+ | `config.setup` | an optional layer providing what a `deploy` function needs |
140
+ | `config.dev`, `stage`, `sidecar`, `profile` | passed to Alchemy's test engine unchanged |
141
+ | `fileStage({ baseStage, testFile })` | the stage a test file deploys under |
142
+ | `StackDeployFailed`, `StackDestroyFailed`, `DurableStateRequired`, `StageUnresolved` | the error classes above |
143
+
144
+ `sharedLayer` comes from `@systemfsoftware/effect-gherkin-spec` (and `@systemfsoftware/effect-spec-runtime`).
145
+
146
+ ## Support
147
+
148
+ Report bugs and ask questions in the [issue tracker](https://github.com/systemfsoftware/systemfsoftware/issues).
149
+
150
+ ## License
151
+
152
+ [Apache-2.0](https://github.com/systemfsoftware/systemfsoftware/blob/main/packages/alchemy-stack-layer/LICENSE)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@systemfsoftware/alchemy-stack-layer",
3
3
  "license": "Apache-2.0",
4
- "version": "0.0.0",
4
+ "version": "0.1.0",
5
5
  "author": "Ryan Lee <drdgvhbh@gmail.com>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -34,17 +34,17 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@alchemy.run/test": "2.0.0-beta.87",
37
- "@systemfsoftware/effect-spec-runtime": "^1.3.3"
37
+ "@systemfsoftware/effect-spec-runtime": "^1.4.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "@alchemy.run/cloudflare": "2.0.0-beta.87",
41
41
  "@effect/platform-node": "^4.1.4",
42
42
  "@microsoft/api-extractor": "^7.60.1",
43
43
  "@systemfsoftware/arethetypeswrong-cli": "^4.2.0",
44
- "@systemfsoftware/effect-gherkin-spec": "^8.3.4",
44
+ "@systemfsoftware/effect-gherkin-spec": "^8.4.0",
45
45
  "@systemfsoftware/oxlint-config-recommended": "^9.0.2",
46
46
  "@systemfsoftware/tsconfig": "^2.1.0",
47
- "@systemfsoftware/tsdown-config": "^0.1.3",
47
+ "@systemfsoftware/tsdown-config": "^0.2.0",
48
48
  "@systemfsoftware/vitest": "^4.2.1",
49
49
  "@systemfsoftware/vitest-config": "^0.1.4",
50
50
  "@types/node": "^26.6.6",