nukadoko 0.6.0 → 0.7.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +541 -3
- data/README.ja.md +20 -5
- data/README.md +12 -6
- data/docs/ci.ja.md +542 -0
- data/docs/ci.md +635 -0
- data/docs/commands.ja.md +1524 -0
- data/docs/commands.md +1281 -0
- data/docs/engines.json +19 -0
- data/docs/execution-events.ja.md +336 -0
- data/docs/execution-events.md +423 -0
- data/docs/glossary.json +1348 -0
- data/docs/guide.ja.md +733 -0
- data/docs/guide.md +815 -0
- data/docs/intent.ja.md +133 -0
- data/docs/intent.md +156 -0
- data/docs/layout.ja.md +442 -0
- data/docs/layout.md +399 -0
- data/docs/releasing.ja.md +186 -0
- data/docs/releasing.md +237 -0
- data/docs/ruby/reference.ja.md +302 -0
- data/docs/ruby/reference.md +368 -0
- data/docs/spec.ja.md +8916 -0
- data/docs/spec.md +11250 -0
- data/docs/tutorial.ja.md +451 -0
- data/docs/tutorial.md +458 -0
- data/docs/typescript/adopting.ja.md +231 -0
- data/docs/typescript/adopting.md +251 -0
- data/docs/typescript/doors.ja.md +301 -0
- data/docs/typescript/doors.md +329 -0
- data/docs/typescript/migration-playwright-test.ja.md +189 -0
- data/docs/typescript/migration-playwright-test.md +212 -0
- data/docs/typescript/migration.ja.md +391 -0
- data/docs/typescript/migration.md +505 -0
- data/docs/typescript/reference.ja.md +176 -0
- data/docs/typescript/reference.md +197 -0
- data/docs/typescript/upgrading.ja.md +1774 -0
- data/docs/typescript/upgrading.md +2224 -0
- data/docs/web-app.ja.md +627 -0
- data/docs/web-app.md +659 -0
- data/docs/writing-scenarios.ja.md +544 -0
- data/docs/writing-scenarios.md +563 -0
- data/lib/nukadoko/base.rb +12 -5
- data/lib/nukadoko/changed_files.rb +95 -0
- data/lib/nukadoko/check.rb +459 -51
- data/lib/nukadoko/claim_drops.rb +271 -0
- data/lib/nukadoko/claim_line.rb +24 -4
- data/lib/nukadoko/claim_rules.rb +143 -1
- data/lib/nukadoko/claim_sides.rb +157 -9
- data/lib/nukadoko/cli.rb +395 -48
- data/lib/nukadoko/codes.json +246 -21
- data/lib/nukadoko/codes.rb +21 -4
- data/lib/nukadoko/command.rb +120 -13
- data/lib/nukadoko/command_help.rb +128 -0
- data/lib/nukadoko/compat.rb +469 -0
- data/lib/nukadoko/criteria.rb +1 -1
- data/lib/nukadoko/definition.rb +157 -7
- data/lib/nukadoko/error_frames.rb +6 -3
- data/lib/nukadoko/events/count.rb +146 -0
- data/lib/nukadoko/events/counted.rb +114 -0
- data/lib/nukadoko/events/format.rb +257 -0
- data/lib/nukadoko/events/lists.rb +154 -0
- data/lib/nukadoko/events/path.rb +285 -0
- data/lib/nukadoko/events/read.rb +284 -0
- data/lib/nukadoko/events/reader.rb +490 -0
- data/lib/nukadoko/events/selection.rb +252 -0
- data/lib/nukadoko/events/tally.rb +438 -0
- data/lib/nukadoko/features.rb +2 -2
- data/lib/nukadoko/fixtures.rb +160 -35
- data/lib/nukadoko/http_recording.rb +6 -2
- data/lib/nukadoko/intent.rb +364 -0
- data/lib/nukadoko/intent_file.rb +81 -0
- data/lib/nukadoko/intent_view.rb +190 -0
- data/lib/nukadoko/js_json.rb +2 -1
- data/lib/nukadoko/judge.rb +2 -2
- data/lib/nukadoko/judgment_file.rb +1 -1
- data/lib/nukadoko/judgments.rb +4 -4
- data/lib/nukadoko/measure_mcp.rb +202 -0
- data/lib/nukadoko/parts.rb +8 -2
- data/lib/nukadoko/path_view.rb +252 -0
- data/lib/nukadoko/place_rows.rb +161 -0
- data/lib/nukadoko/program.rb +1265 -51
- data/lib/nukadoko/program_log.rb +122 -0
- data/lib/nukadoko/program_shape.rb +385 -0
- data/lib/nukadoko/project_fixtures.rb +43 -10
- data/lib/nukadoko/project_program.rb +5 -4
- data/lib/nukadoko/repeated_prefix.rb +310 -0
- data/lib/nukadoko/report_run.rb +3 -3
- data/lib/nukadoko/run.rb +1148 -159
- data/lib/nukadoko/run_record.rb +70 -46
- data/lib/nukadoko/scaffold.rb +24 -6
- data/lib/nukadoko/schema.rb +15 -2
- data/lib/nukadoko/secrets.rb +55 -5
- data/lib/nukadoko/states.rb +478 -0
- data/lib/nukadoko/step.rb +71 -2
- data/lib/nukadoko/step_fixtures.rb +52 -3
- data/lib/nukadoko/summary.rb +799 -29
- data/lib/nukadoko/table.rb +4 -2
- data/lib/nukadoko/text_fit.rb +20 -7
- data/lib/nukadoko/trace.rb +152 -45
- data/lib/nukadoko/tree_passes.rb +95 -33
- data/lib/nukadoko/underneath.rb +311 -0
- data/lib/nukadoko/version.rb +1 -1
- data/lib/nukadoko/vocabulary.rb +166 -23
- data/lib/nukadoko/walk.rb +2 -2
- data/lib/nukadoko/walk_step.rb +1 -1
- data/lib/nukadoko.rb +23 -0
- data/llms.txt +41 -34
- data/sig/generated/nukadoko/base.rbs +4 -4
- data/sig/generated/nukadoko/changed_files.rbs +42 -0
- data/sig/generated/nukadoko/check.rbs +147 -10
- data/sig/generated/nukadoko/claim_drops.rbs +135 -0
- data/sig/generated/nukadoko/claim_line.rbs +11 -0
- data/sig/generated/nukadoko/claim_rules.rbs +49 -4
- data/sig/generated/nukadoko/claim_sides.rbs +63 -12
- data/sig/generated/nukadoko/cli.rbs +80 -7
- data/sig/generated/nukadoko/codes.rbs +19 -4
- data/sig/generated/nukadoko/command.rbs +68 -1
- data/sig/generated/nukadoko/command_help.rbs +18 -0
- data/sig/generated/nukadoko/compat.rbs +392 -0
- data/sig/generated/nukadoko/criteria.rbs +1 -1
- data/sig/generated/nukadoko/definition.rbs +50 -0
- data/sig/generated/nukadoko/error_frames.rbs +1 -1
- data/sig/generated/nukadoko/events/count.rbs +69 -0
- data/sig/generated/nukadoko/events/counted.rbs +70 -0
- data/sig/generated/nukadoko/events/format.rbs +163 -0
- data/sig/generated/nukadoko/events/lists.rbs +47 -0
- data/sig/generated/nukadoko/events/path.rbs +161 -0
- data/sig/generated/nukadoko/events/read.rbs +109 -0
- data/sig/generated/nukadoko/events/reader.rbs +226 -0
- data/sig/generated/nukadoko/events/selection.rbs +100 -0
- data/sig/generated/nukadoko/events/tally.rbs +227 -0
- data/sig/generated/nukadoko/features.rbs +6 -4
- data/sig/generated/nukadoko/fixtures.rbs +67 -11
- data/sig/generated/nukadoko/http_recording.rbs +1 -1
- data/sig/generated/nukadoko/intent.rbs +132 -0
- data/sig/generated/nukadoko/intent_file.rbs +46 -0
- data/sig/generated/nukadoko/intent_view.rbs +69 -0
- data/sig/generated/nukadoko/judgment_file.rbs +1 -1
- data/sig/generated/nukadoko/judgments.rbs +1 -1
- data/sig/generated/nukadoko/measure_mcp.rbs +104 -0
- data/sig/generated/nukadoko/parts.rbs +2 -2
- data/sig/generated/nukadoko/path_view.rbs +105 -0
- data/sig/generated/nukadoko/place_rows.rbs +43 -0
- data/sig/generated/nukadoko/program.rbs +627 -23
- data/sig/generated/nukadoko/program_log.rbs +67 -0
- data/sig/generated/nukadoko/program_shape.rbs +147 -0
- data/sig/generated/nukadoko/project_fixtures.rbs +16 -3
- data/sig/generated/nukadoko/project_program.rbs +2 -1
- data/sig/generated/nukadoko/repeated_prefix.rbs +172 -0
- data/sig/generated/nukadoko/report_run.rbs +1 -1
- data/sig/generated/nukadoko/run.rbs +286 -41
- data/sig/generated/nukadoko/run_record.rbs +20 -12
- data/sig/generated/nukadoko/scaffold.rbs +3 -0
- data/sig/generated/nukadoko/schema.rbs +5 -0
- data/sig/generated/nukadoko/secrets.rbs +26 -3
- data/sig/generated/nukadoko/states.rbs +214 -0
- data/sig/generated/nukadoko/step.rbs +58 -1
- data/sig/generated/nukadoko/step_fixtures.rbs +39 -0
- data/sig/generated/nukadoko/summary.rbs +256 -2
- data/sig/generated/nukadoko/table.rbs +3 -2
- data/sig/generated/nukadoko/trace.rbs +108 -33
- data/sig/generated/nukadoko/tree_passes.rbs +36 -8
- data/sig/generated/nukadoko/underneath.rbs +141 -0
- data/sig/generated/nukadoko/vocabulary.rbs +45 -6
- data/skills/nukadoko/SKILL.md +18 -18
- data/skills/nukadoko/references/checking-steps.md +16 -83
- data/skills/nukadoko/references/diagnosing.md +6 -6
- data/skills/nukadoko/references/evidence.md +19 -6
- data/skills/nukadoko/references/exploring.md +22 -7
- data/skills/nukadoko/references/fixtures.md +12 -32
- data/skills/nukadoko/references/judgments.md +12 -0
- data/skills/nukadoko/references/maintenance.md +1 -1
- data/skills/nukadoko/references/merge-risk.md +1 -1
- data/skills/nukadoko/references/parts.md +46 -23
- data/skills/nukadoko/references/{perturbed-worlds.md → perturbed-systems.md} +30 -12
- data/skills/nukadoko/references/program.md +168 -169
- data/skills/nukadoko/references/reviewing.md +20 -20
- data/skills/nukadoko/references/ruby/checking-steps.md +106 -0
- data/skills/nukadoko/references/ruby/examples/cli.md +212 -0
- data/skills/nukadoko/references/ruby/fixtures.md +34 -0
- data/skills/nukadoko/references/ruby/program.md +112 -0
- data/skills/nukadoko/references/ruby/step-files.md +115 -0
- data/skills/nukadoko/references/running.md +34 -34
- data/skills/nukadoko/references/scenario-rules.md +7 -6
- data/skills/nukadoko/references/states.md +7 -1
- data/skills/nukadoko/references/step-files.md +41 -120
- data/skills/nukadoko/references/typescript/checking-steps.md +129 -0
- data/skills/nukadoko/references/{examples → typescript/examples}/cli.md +1 -1
- data/skills/nukadoko/references/{examples → typescript/examples}/http-api.md +6 -6
- data/skills/nukadoko/references/{examples → typescript/examples}/workflow.md +1 -1
- data/skills/nukadoko/references/typescript/fixtures.md +56 -0
- data/skills/nukadoko/references/typescript/program.md +111 -0
- data/skills/nukadoko/references/typescript/step-files.md +104 -0
- data/skills/nukadoko/references/verdicts.md +10 -10
- data/skills/nukadoko/references/writing-steps.md +67 -67
- data/skills/nukadoko/references/writing-the-feature.md +1 -1
- data/skills/nukadoko-verification/SKILL.md +13 -8
- metadata +103 -7
- data/skills/nukadoko/references/ruby.md +0 -80
data/docs/web-app.md
ADDED
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
# Testing a web app
|
|
2
|
+
|
|
3
|
+
[The tutorial](tutorial.md) tests a command line program. This page is
|
|
4
|
+
for a program that is a server: an HTTP API, a page in a browser, or a
|
|
5
|
+
Rails app. It shows how nukadoko starts the server, how a step reaches
|
|
6
|
+
it, and what each step record keeps. Every step and config pair ran in
|
|
7
|
+
both engines. The Rails section uses the Ruby engine only.
|
|
8
|
+
|
|
9
|
+
## The program under test
|
|
10
|
+
|
|
11
|
+
The program under test on this page is a small todo API,
|
|
12
|
+
[typescript/examples/todo/app/server.ts](https://github.com/meganemura/nukadoko/blob/main/typescript/examples/todo/app/server.ts).
|
|
13
|
+
It keeps the todos in memory and listens on the port that `--port`
|
|
14
|
+
names. It answers these routes, among others: `GET /todos`,
|
|
15
|
+
`POST /todos`, `GET /todos/<id>`, and `PATCH /todos/<id>`. It also
|
|
16
|
+
writes one line for each request. The line starts with the
|
|
17
|
+
`traceparent` header, a trace id that nukadoko puts on each request a
|
|
18
|
+
step sends:
|
|
19
|
+
|
|
20
|
+
<!-- engine: neither -->
|
|
21
|
+
```ts
|
|
22
|
+
return createServer(async (req, res) => {
|
|
23
|
+
console.log(`[${req.headers.traceparent ?? "-"}] ${req.method} ${req.url}`);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The config's `program` is a function. It returns a table that tells
|
|
27
|
+
nukadoko how to start the program. Here the program is `app/server.ts`
|
|
28
|
+
in the project:
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
# nukadoko.config.rb
|
|
32
|
+
Nukadoko.config(
|
|
33
|
+
program: -> {
|
|
34
|
+
{ serve: { command: "node app/server.ts --port {port}", ready: "/todos" } }
|
|
35
|
+
},
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
// nukadoko.config.ts
|
|
41
|
+
import { defineConfig } from "nukadoko";
|
|
42
|
+
|
|
43
|
+
export default defineConfig({
|
|
44
|
+
program: () => ({
|
|
45
|
+
serve: { command: "node app/server.ts --port {port}", ready: "/todos" },
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
A system is one copy of the program that a run starts, with its own port
|
|
51
|
+
and its own directory. A run starts one system, or one for each
|
|
52
|
+
environment that the config's `environments` names
|
|
53
|
+
([Systems](spec.md#systems)). For each system, nukadoko does these steps:
|
|
54
|
+
|
|
55
|
+
1. It finds a free port and puts it where the command says `{port}`.
|
|
56
|
+
2. It starts the command. The command is split on whitespace and runs
|
|
57
|
+
with no shell. A command that needs a shell is written as
|
|
58
|
+
`["sh", "-c", "..."]`.
|
|
59
|
+
3. It sends `GET` to `ready` until the answer is 2xx. The default is `/`.
|
|
60
|
+
This program answers 404 on `/`, so the config names `/todos`.
|
|
61
|
+
4. It runs the scenarios, and every step reaches the same program.
|
|
62
|
+
5. It stops the program and the processes the program started.
|
|
63
|
+
|
|
64
|
+
A program that reads its port from the environment gets it through the
|
|
65
|
+
`env` field of `program`, for example `env: { PORT: "{port}" }`. The
|
|
66
|
+
field has the same name and shape in both engines. nukadoko sets no
|
|
67
|
+
variable that the program does not ask for. `nuka run` names the URL
|
|
68
|
+
and the program's log on stderr:
|
|
69
|
+
|
|
70
|
+
```text
|
|
71
|
+
system default: serve started: node app/server.ts --port 37139, waiting for http://localhost:37139/todos
|
|
72
|
+
system default: serve 0.1s, http://localhost:37139 (log .nukadoko/runs/run-20261203-235017-2z3j/systems/default/main/program.log)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`program.log` holds what the program printed. A program that exits
|
|
76
|
+
before `ready` answers fails the run with `program-start-failed` and the
|
|
77
|
+
end of that log. For a program that nukadoko does not start, such as a
|
|
78
|
+
staging deploy, `url` replaces `serve`. "Program" in
|
|
79
|
+
[the spec](spec.md#program) has every field, the limits, and the
|
|
80
|
+
placeholders.
|
|
81
|
+
|
|
82
|
+
## A step that sends a request
|
|
83
|
+
|
|
84
|
+
The `request` fixture sends a request to the program. A path joins the
|
|
85
|
+
program's URL, which the `baseURL` fixture holds (`base_url` in Ruby,
|
|
86
|
+
which also accepts `baseURL`). This step is from the `todo` conformance case:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
class AddTodo < Nukadoko::Step
|
|
90
|
+
pattern "a todo titled {title:string} is added"
|
|
91
|
+
description "Create a todo via POST /todos and return the created record"
|
|
92
|
+
args { string :title, "The title of the todo to create" }
|
|
93
|
+
returns do
|
|
94
|
+
string :id, "The id the server assigned"
|
|
95
|
+
string :title, "The title as the server stored it"
|
|
96
|
+
boolean :done, "Whether the todo is done; false for a new one"
|
|
97
|
+
end
|
|
98
|
+
mutates true
|
|
99
|
+
|
|
100
|
+
def run(request:, args:)
|
|
101
|
+
request.post("/todos", data: { title: args.title }).json
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { defineStep, z } from "nukadoko";
|
|
108
|
+
|
|
109
|
+
export default defineStep({
|
|
110
|
+
pattern: "a todo titled {title:string} is added",
|
|
111
|
+
description: "Create a todo via POST /todos and return the created record",
|
|
112
|
+
args: z.object({ title: z.string().describe("The title of the todo to create") }),
|
|
113
|
+
returns: z.object({
|
|
114
|
+
id: z.string().describe("The id the server assigned"),
|
|
115
|
+
title: z.string().describe("The title as the server stored it"),
|
|
116
|
+
done: z.boolean().describe("Whether the todo is done; false for a new one"),
|
|
117
|
+
}),
|
|
118
|
+
mutates: true,
|
|
119
|
+
async run({ request }, args) {
|
|
120
|
+
const res = await request.post("/todos", { data: { title: args.title } });
|
|
121
|
+
return res.json();
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
In TypeScript, `request` is Playwright's own `APIRequestContext`, so the
|
|
127
|
+
project needs the `playwright` package but no browser
|
|
128
|
+
([Install a browser](#install-a-browser)). In Ruby, `request` uses
|
|
129
|
+
`Net::HTTP` and needs nothing more.
|
|
130
|
+
|
|
131
|
+
The step record keeps `http.jsonl`, with one line for each request. The
|
|
132
|
+
line holds the request body and the response body:
|
|
133
|
+
|
|
134
|
+
```json
|
|
135
|
+
{"method":"POST","url":"http://localhost:37139/todos","declared_url":"/todos","status":201,"duration_ms":5,"via":"request","traceparent":"00-1d8d9d3a57209c5506c4e9241e3497db-e7cefde795943ba6-01","traceparent_sent_to":["http://localhost:37139"],"request_body":{"text":"{\"title\":\"Walk the dog\"}","bytes":24},"response_body":{"text":"{\"id\":\"d6590efc-47bf-43ae-87da-c24b8beb7a4e\",\"title\":\"Walk the dog\",\"done\":false}","bytes":81}}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The step record also keeps `program-log.main.txt`: the lines of
|
|
139
|
+
`program.log` that carry the trace id of this step, `main` being the name
|
|
140
|
+
of a program's one service. Here that is
|
|
141
|
+
`[00-1d8d9d3a57209c5506c4e9241e3497db-e7cefde795943ba6-01] POST /todos`.
|
|
142
|
+
|
|
143
|
+
[`nuka scaffold`](commands.md#nuka-scaffold) with
|
|
144
|
+
`--request "<METHOD> <path>"` writes, in the project's engine language,
|
|
145
|
+
a step that sends one request and returns `status` and `body`. A `{key}`
|
|
146
|
+
in the path is that args key's value, and `--body <key>` sends an args
|
|
147
|
+
key as the request body. Write the feature line first, so that scaffold
|
|
148
|
+
can see its doc string:
|
|
149
|
+
|
|
150
|
+
```gherkin
|
|
151
|
+
Scenario: a todo posted as JSON is created
|
|
152
|
+
When I post this todo:
|
|
153
|
+
"""
|
|
154
|
+
{"title": "Buy milk"}
|
|
155
|
+
"""
|
|
156
|
+
Then the response status is 201
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```sh
|
|
160
|
+
nuka scaffold post-todo --pattern "I post this todo:" --request "POST /todos" --body docstring
|
|
161
|
+
nuka scaffold status-is --pattern "the response status is {code:int}" --then --from post-todo.status
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
`--from post-todo.status` fills an args key of the Then step from the
|
|
165
|
+
`status` that `post-todo` returns. The scenario passes with no edit. The
|
|
166
|
+
scaffolded step declares that it mutates. The method is not proof of a
|
|
167
|
+
write, so set `mutates` to false when the request changes nothing.
|
|
168
|
+
|
|
169
|
+
A step can also call the program with the language's own HTTP client:
|
|
170
|
+
`Net::HTTP` in Ruby, `fetch` in TypeScript. nukadoko records these calls
|
|
171
|
+
in `http.jsonl` too, with `via` set to `net/http` or `fetch`. This step
|
|
172
|
+
is from the `http-api` conformance case, whose program lists items at
|
|
173
|
+
`/items`:
|
|
174
|
+
|
|
175
|
+
```ruby
|
|
176
|
+
class ListItems < Nukadoko::Step
|
|
177
|
+
description "GET /items"
|
|
178
|
+
pattern "I request the item list"
|
|
179
|
+
returns do
|
|
180
|
+
number :status, "HTTP status"
|
|
181
|
+
unknown :body, "The parsed JSON response"
|
|
182
|
+
end
|
|
183
|
+
mutates false
|
|
184
|
+
|
|
185
|
+
def run(baseURL:, args:)
|
|
186
|
+
res = Net::HTTP.get_response(URI("#{baseURL}/items"))
|
|
187
|
+
{ status: res.code.to_i, body: JSON.parse(res.body) }
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
import { defineStep, z } from "nukadoko";
|
|
194
|
+
|
|
195
|
+
export default defineStep({
|
|
196
|
+
description: "GET /items",
|
|
197
|
+
pattern: "I request the item list",
|
|
198
|
+
args: z.object({}),
|
|
199
|
+
returns: z.object({
|
|
200
|
+
status: z.number().describe("HTTP status"),
|
|
201
|
+
body: z.unknown().describe("The parsed JSON response"),
|
|
202
|
+
}),
|
|
203
|
+
mutates: false,
|
|
204
|
+
async run({ baseURL }) {
|
|
205
|
+
const res = await fetch(`${baseURL}/items`);
|
|
206
|
+
return { status: res.status, body: await res.json() };
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
The guide walks the same loop from an empty directory, with a small
|
|
212
|
+
server, the scaffold commands, and a step that calls `fetch`: see
|
|
213
|
+
[An HTTP API](guide.md#an-http-api) and
|
|
214
|
+
[Calling the program with fetch](guide.md#calling-the-program-with-fetch).
|
|
215
|
+
|
|
216
|
+
## Install a browser
|
|
217
|
+
|
|
218
|
+
A step that uses `page` needs the Node `playwright` package and a
|
|
219
|
+
browser. A TypeScript step that uses `request` needs the package, but no
|
|
220
|
+
browser. The two engines install them in different ways.
|
|
221
|
+
|
|
222
|
+
In a TypeScript project, add the `playwright` package at the version
|
|
223
|
+
that nukadoko is tested against, and install Chromium:
|
|
224
|
+
|
|
225
|
+
<!-- engine: typescript -->
|
|
226
|
+
```sh
|
|
227
|
+
npm install -D -E playwright@1.63.0
|
|
228
|
+
npx playwright install chromium
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Without the package, `nuka check` reports `playwright-missing` for each
|
|
232
|
+
line that needs it, and prints the install command.
|
|
233
|
+
|
|
234
|
+
A Ruby project also needs Node (20 or later, for this Playwright) and
|
|
235
|
+
the same Node package. The gem drives the browser through its
|
|
236
|
+
dependency `playwright-ruby-client`. That client starts the driver of
|
|
237
|
+
the Node package, by default with `npx playwright`. In a project with no
|
|
238
|
+
`package.json`, `npm install` writes one:
|
|
239
|
+
|
|
240
|
+
<!-- engine: ruby -->
|
|
241
|
+
```sh
|
|
242
|
+
npm install -D -E playwright@1.63.0
|
|
243
|
+
npx playwright install chromium
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
In Ruby, `nuka check` does not find a missing package. Each page step
|
|
247
|
+
fails at run time with an error such as `Executable doesn't exist at
|
|
248
|
+
.../chromium_headless_shell-1255/...`, because `npx` then runs another
|
|
249
|
+
Playwright version. `PLAYWRIGHT_CLI` names another driver command, such
|
|
250
|
+
as the `node_modules/.bin/playwright` of another checkout. The browser's
|
|
251
|
+
launch options go in the config as `browser:`
|
|
252
|
+
([Ruby reference](ruby/reference.md#configuration)).
|
|
253
|
+
|
|
254
|
+
## A step that drives a page
|
|
255
|
+
|
|
256
|
+
The `page` fixture is a Playwright page whose base URL is the program's
|
|
257
|
+
URL. These steps are from the `shopping-list` conformance case, a page
|
|
258
|
+
with a form that adds an item to a list:
|
|
259
|
+
|
|
260
|
+
```ruby
|
|
261
|
+
class AddOnPage < Nukadoko::Step
|
|
262
|
+
pattern "{name:string} is added on the page"
|
|
263
|
+
args { string :name }
|
|
264
|
+
mutates true
|
|
265
|
+
|
|
266
|
+
def run(page:, args:)
|
|
267
|
+
page.get_by_label("Name").fill(args.name)
|
|
268
|
+
page.get_by_role("button", name: "Add").click
|
|
269
|
+
page.locator("#items li", hasText: args.name).wait_for
|
|
270
|
+
{}
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
```ts
|
|
276
|
+
import { defineStep, z } from "nukadoko";
|
|
277
|
+
|
|
278
|
+
export default defineStep({
|
|
279
|
+
pattern: "{name:string} is added on the page",
|
|
280
|
+
args: z.object({ name: z.string() }),
|
|
281
|
+
mutates: true,
|
|
282
|
+
async run({ page }, args) {
|
|
283
|
+
await page.getByLabel("Name").fill(args.name);
|
|
284
|
+
await page.getByRole("button", { name: "Add" }).click();
|
|
285
|
+
await page.locator("#items li", { hasText: args.name }).waitFor();
|
|
286
|
+
return {};
|
|
287
|
+
},
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
Each scenario gets a new browser context, and all its steps share one
|
|
292
|
+
page. A page step keeps more in its step record:
|
|
293
|
+
|
|
294
|
+
- `trace.zip`: a Playwright trace of the step. `npx playwright
|
|
295
|
+
show-trace` opens it.
|
|
296
|
+
- `http.jsonl`: the requests the page sent to the program, with
|
|
297
|
+
`via` set to `page`.
|
|
298
|
+
- `judged/page.png` and `judged/page.aria.yml`, for a Then step: the page
|
|
299
|
+
as the step read it for its [claims](writing-scenarios.md#claims).
|
|
300
|
+
|
|
301
|
+
The scenario also keeps `final.png`, a screenshot of the page when the
|
|
302
|
+
scenario ends. "Records" in [the spec](spec.md#records) has each field.
|
|
303
|
+
|
|
304
|
+
## Secrets and env files
|
|
305
|
+
|
|
306
|
+
`envFiles` in the config names the env files to load, for example
|
|
307
|
+
`envFiles: [".env"]`. The key is `envFiles` in both engines. Here `.env`
|
|
308
|
+
holds one line, and git does not track the file:
|
|
309
|
+
|
|
310
|
+
```text
|
|
311
|
+
API_KEY=sekrit123
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
A step reads a value with `requireEnv` (`require_env` in Ruby), which
|
|
315
|
+
fails the step when the key is missing or empty. This step only shows
|
|
316
|
+
the redaction: the todo program ignores the query.
|
|
317
|
+
|
|
318
|
+
```ruby
|
|
319
|
+
class ReadWithKey < Nukadoko::Step
|
|
320
|
+
description "GET /todos with the API key from the env files"
|
|
321
|
+
pattern "I read the todo list with the API key"
|
|
322
|
+
returns { number :status }
|
|
323
|
+
mutates false
|
|
324
|
+
|
|
325
|
+
def run(request:, require_env:)
|
|
326
|
+
response = request.get("/todos?api_key=#{require_env.("API_KEY")}")
|
|
327
|
+
{ status: response.status }
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
import { defineStep, z } from "nukadoko";
|
|
334
|
+
|
|
335
|
+
export default defineStep({
|
|
336
|
+
description: "GET /todos with the API key from the env files",
|
|
337
|
+
pattern: "I read the todo list with the API key",
|
|
338
|
+
args: z.object({}),
|
|
339
|
+
returns: z.object({ status: z.number() }),
|
|
340
|
+
mutates: false,
|
|
341
|
+
async run({ request, requireEnv }) {
|
|
342
|
+
const response = await request.get(`/todos?api_key=${requireEnv("API_KEY")}`);
|
|
343
|
+
return { status: response.status() };
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
nukadoko uses the git state of each env file to find which values are
|
|
349
|
+
secrets. It writes a secret as `{{secret.NAME}}` in the step record's
|
|
350
|
+
`record.json` and `http.jsonl`, and `record.json` lists the key under
|
|
351
|
+
`required_env`:
|
|
352
|
+
|
|
353
|
+
```json
|
|
354
|
+
{"method":"GET","url":"http://localhost:45645/todos?api_key={{secret.API_KEY}}","declared_url":"/todos?api_key={{secret.API_KEY}}","status":200,"duration_ms":23,"via":"request","traceparent":"00-7609ab4138ca8597787f0c2f06592ef2-a63b8810c7992935-01","traceparent_sent_to":["http://localhost:45645"],"response_body":{"text":"[]","bytes":2}}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
The program does not get the env files, because they hold the steps'
|
|
358
|
+
secrets ([env](spec.md#env)). To give the program a value, put it in
|
|
359
|
+
the `env` field of `program`. "Environments and secrets" in
|
|
360
|
+
[the spec](spec.md#environments-and-secrets) has the rule for each file
|
|
361
|
+
and each form of redaction.
|
|
362
|
+
|
|
363
|
+
## Fresh data for each run
|
|
364
|
+
|
|
365
|
+
Each run starts the program again, in a new system with a directory of
|
|
366
|
+
its own, `{dir}`. The data is fresh for each run, not for each scenario.
|
|
367
|
+
Three more fields of `program` prepare the data:
|
|
368
|
+
|
|
369
|
+
- `build`: commands that run once for each run, before any system starts.
|
|
370
|
+
- `prepare`: commands that run once for each system, with its env, such
|
|
371
|
+
as a database migration into `{dir}`.
|
|
372
|
+
- `setup`: a function that gets the system (`url`, `port`, `dir`, `env`,
|
|
373
|
+
and more) after `ready` answers and before the first scenario.
|
|
374
|
+
|
|
375
|
+
This `setup` adds a todo, so the first scenario finds one:
|
|
376
|
+
|
|
377
|
+
```ruby
|
|
378
|
+
Nukadoko.config(
|
|
379
|
+
program: -> {
|
|
380
|
+
{
|
|
381
|
+
serve: { command: "node app/server.ts --port {port}", ready: "/todos" },
|
|
382
|
+
setup: ->(system) { Net::HTTP.post(URI("#{system.url}/todos"), JSON.generate(title: "Water the plants")) },
|
|
383
|
+
}
|
|
384
|
+
},
|
|
385
|
+
)
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
```ts
|
|
389
|
+
import { defineConfig } from "nukadoko";
|
|
390
|
+
|
|
391
|
+
export default defineConfig({
|
|
392
|
+
program: () => ({
|
|
393
|
+
serve: { command: "node app/server.ts --port {port}", ready: "/todos" },
|
|
394
|
+
setup: async (system) => {
|
|
395
|
+
await fetch(`${system.url}/todos`, { method: "POST", body: JSON.stringify({ title: "Water the plants" }) });
|
|
396
|
+
},
|
|
397
|
+
}),
|
|
398
|
+
});
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
All scenarios of a system reach the same program, so the state that one
|
|
402
|
+
scenario writes stays for the next. A scenario that reads state makes it
|
|
403
|
+
first, and claims only on what it made. "Program" in
|
|
404
|
+
[the spec](spec.md#program) has each phase and its limit.
|
|
405
|
+
|
|
406
|
+
## In a Rails app
|
|
407
|
+
|
|
408
|
+
This section uses the Ruby engine. The `program` fields are the same in
|
|
409
|
+
a TypeScript config. Add the gem to the test group of the Gemfile, and
|
|
410
|
+
run `nuka init`. When the `Rakefile` has no `scenarios` task, `init`
|
|
411
|
+
adds one:
|
|
412
|
+
|
|
413
|
+
<!-- engine: ruby -->
|
|
414
|
+
```ruby
|
|
415
|
+
gem "nukadoko", group: :test
|
|
416
|
+
```
|
|
417
|
+
|
|
418
|
+
```sh
|
|
419
|
+
bundle install
|
|
420
|
+
bundle exec nuka init
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
The program starts the Rails server in the test environment. Each system
|
|
424
|
+
gets a new SQLite database in its own directory, and `prepare` makes the
|
|
425
|
+
schema before the server starts. `/up` is the health check of a new
|
|
426
|
+
Rails app:
|
|
427
|
+
|
|
428
|
+
<!-- engine: ruby -->
|
|
429
|
+
```ruby
|
|
430
|
+
# nukadoko.config.rb
|
|
431
|
+
Nukadoko.config(
|
|
432
|
+
program: -> {
|
|
433
|
+
{
|
|
434
|
+
prepare: ["bin/rails db:prepare"],
|
|
435
|
+
serve: { command: "bin/rails server -p {port}", ready: "/up" },
|
|
436
|
+
env: { RAILS_ENV: "test", DATABASE_URL: "sqlite3:{dir}/test.sqlite3" },
|
|
437
|
+
}
|
|
438
|
+
},
|
|
439
|
+
)
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
A step sends what the controller expects, for example
|
|
443
|
+
`request.post("/todos", data: { todo: { title: args.title } })`.
|
|
444
|
+
`bundle exec rake scenarios` prints the command it runs, then the run:
|
|
445
|
+
|
|
446
|
+
```text
|
|
447
|
+
bundle exec nuka run
|
|
448
|
+
system default: prepare started: bin/rails db:prepare
|
|
449
|
+
system default: serve started: bin/rails server -p 38195, waiting for http://localhost:38195/up
|
|
450
|
+
system default: prepare 1.3s, serve 1.6s, http://localhost:38195 (log .nukadoko/runs/run-20261204-001418-pufs/systems/default/main/program.log)
|
|
451
|
+
scenario 1/1 features/todos.feature:3 an added todo is in the list
|
|
452
|
+
step 1/2 passed 0.1s a todo titled "Buy milk" is added
|
|
453
|
+
step 2/2 passed 0.1s the todo list includes "Buy milk"
|
|
454
|
+
```
|
|
455
|
+
|
|
456
|
+
In the test environment, Rails writes its request log to
|
|
457
|
+
`log/test.log`, not to `program.log`. Do not give `bin/rails server` the
|
|
458
|
+
`-d` flag. The command then exits before `ready` answers, the system
|
|
459
|
+
fails with `program-start-failed`, and the server stays in the
|
|
460
|
+
background after the run.
|
|
461
|
+
|
|
462
|
+
## Several services
|
|
463
|
+
|
|
464
|
+
A program can be more than one server, such as a shop that calls a
|
|
465
|
+
billing service. `services` names each one,
|
|
466
|
+
with its own `serve`, `env`, and the other fields, and `root`, the
|
|
467
|
+
directory its commands run in. `primary` names the service the steps
|
|
468
|
+
reach through `page`, `request`, and `baseURL`. Every port is chosen
|
|
469
|
+
before any service starts, so `{service.<name>.url}` hands one service
|
|
470
|
+
the other's address:
|
|
471
|
+
|
|
472
|
+
```ruby
|
|
473
|
+
# nukadoko.config.rb
|
|
474
|
+
Nukadoko.config(program: lambda {
|
|
475
|
+
{
|
|
476
|
+
primary: "shop",
|
|
477
|
+
services: {
|
|
478
|
+
shop: {
|
|
479
|
+
serve: { command: "node relay.mjs --port {port}", ready: "/whoami" },
|
|
480
|
+
env: { NAME: "shop", PEER_URL: "{service.billing.url}" }
|
|
481
|
+
},
|
|
482
|
+
billing: {
|
|
483
|
+
root: "billing",
|
|
484
|
+
serve: { command: "node ../relay.mjs --port {port}", ready: "/whoami" },
|
|
485
|
+
env: { NAME: "billing", PEER_URL: "{service.shop.url}" }
|
|
486
|
+
}
|
|
487
|
+
},
|
|
488
|
+
environments: {
|
|
489
|
+
local: {},
|
|
490
|
+
staging: { services: { shop: { url: "https://shop.staging.acme.example" }, billing: { url: "https://billing.staging.acme.example" } } }
|
|
491
|
+
},
|
|
492
|
+
defaultEnvironments: ["local"]
|
|
493
|
+
}
|
|
494
|
+
})
|
|
495
|
+
```
|
|
496
|
+
|
|
497
|
+
```ts
|
|
498
|
+
// nukadoko.config.ts
|
|
499
|
+
import { defineConfig } from "nukadoko";
|
|
500
|
+
|
|
501
|
+
export default defineConfig({
|
|
502
|
+
program: () => ({
|
|
503
|
+
primary: "shop",
|
|
504
|
+
services: {
|
|
505
|
+
shop: {
|
|
506
|
+
serve: { command: "node relay.mjs --port {port}", ready: "/whoami" },
|
|
507
|
+
env: { NAME: "shop", PEER_URL: "{service.billing.url}" },
|
|
508
|
+
},
|
|
509
|
+
billing: {
|
|
510
|
+
root: "billing",
|
|
511
|
+
serve: { command: "node ../relay.mjs --port {port}", ready: "/whoami" },
|
|
512
|
+
env: { NAME: "billing", PEER_URL: "{service.shop.url}" },
|
|
513
|
+
},
|
|
514
|
+
},
|
|
515
|
+
environments: {
|
|
516
|
+
local: {},
|
|
517
|
+
staging: { services: { shop: { url: "https://shop.staging.acme.example" }, billing: { url: "https://billing.staging.acme.example" } } },
|
|
518
|
+
},
|
|
519
|
+
defaultEnvironments: ["local"],
|
|
520
|
+
}),
|
|
521
|
+
});
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
A step reaches a service other than the primary one through the
|
|
525
|
+
`services` fixture, by name, and never writes a URL of its own:
|
|
526
|
+
|
|
527
|
+
```ruby
|
|
528
|
+
def run(request:, services:)
|
|
529
|
+
{ name: request.get("#{services[:billing].url}/whoami").json["name"] }
|
|
530
|
+
end
|
|
531
|
+
```
|
|
532
|
+
|
|
533
|
+
```ts
|
|
534
|
+
async run({ request, services }) {
|
|
535
|
+
const response = await request.get(`${services.billing!.url}/whoami`);
|
|
536
|
+
return { name: ((await response.json()) as { name: string }).name };
|
|
537
|
+
},
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
So `nuka run` starts both services and `nuka run --env staging` runs the
|
|
541
|
+
same scenarios and steps against the deployed ones, starting nothing.
|
|
542
|
+
Each service has its own `program.log`, under
|
|
543
|
+
`systems/<environment>/<service>/`, and each step record keeps the lines
|
|
544
|
+
of each one that carry its trace id, in `program-log.<service>.txt`: a
|
|
545
|
+
request shop passes on to billing with its `traceparent` is the step's in
|
|
546
|
+
billing's log too. A `root` outside the repository, such as `../billing`
|
|
547
|
+
for a clone beside it, is a checkout: `nuka run --base` builds it once
|
|
548
|
+
for head's system and the base's, and the run summary names the commit it
|
|
549
|
+
had. "Services" in [the spec](spec.md#services) has every rule.
|
|
550
|
+
|
|
551
|
+
## A test API
|
|
552
|
+
|
|
553
|
+
A system nukadoko starts has the program's command line at hand, so a
|
|
554
|
+
step can seed data with `bin/rails runner`. A deployed environment does
|
|
555
|
+
not: nothing of the program is on the machine the scenarios run on. One
|
|
556
|
+
suite has to work in both, so declare `stepsReach: "url"` and let the
|
|
557
|
+
steps reach the program only at its URL:
|
|
558
|
+
|
|
559
|
+
```ruby
|
|
560
|
+
# nukadoko.config.rb
|
|
561
|
+
Nukadoko.config(program: lambda {
|
|
562
|
+
{
|
|
563
|
+
stepsReach: "url",
|
|
564
|
+
serve: { command: "bin/rails server -p {port}", ready: "/up" },
|
|
565
|
+
env: { RAILS_ENV: "test" },
|
|
566
|
+
setup: ->(system) {
|
|
567
|
+
answer = Net::HTTP.post(URI("#{system.url}/test/tenants"),
|
|
568
|
+
JSON.generate({ flags: system.conditions }),
|
|
569
|
+
"content-type" => "application/json")
|
|
570
|
+
{ tenant: JSON.parse(answer.body)["tenant"] }
|
|
571
|
+
},
|
|
572
|
+
environments: {
|
|
573
|
+
local: {},
|
|
574
|
+
staging: { url: "https://staging.acme.example" }
|
|
575
|
+
},
|
|
576
|
+
defaultEnvironments: ["local"]
|
|
577
|
+
}
|
|
578
|
+
})
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
```ts
|
|
582
|
+
// nukadoko.config.ts
|
|
583
|
+
import { defineConfig } from "nukadoko";
|
|
584
|
+
|
|
585
|
+
export default defineConfig({
|
|
586
|
+
program: () => ({
|
|
587
|
+
stepsReach: "url",
|
|
588
|
+
serve: { command: "bin/rails server -p {port}", ready: "/up" },
|
|
589
|
+
env: { RAILS_ENV: "test" },
|
|
590
|
+
setup: async ({ url, conditions }) => {
|
|
591
|
+
const answer = await fetch(`${url}/test/tenants`, {
|
|
592
|
+
method: "POST",
|
|
593
|
+
headers: { "content-type": "application/json" },
|
|
594
|
+
body: JSON.stringify({ flags: conditions }),
|
|
595
|
+
});
|
|
596
|
+
return { tenant: ((await answer.json()) as { tenant: string }).tenant };
|
|
597
|
+
},
|
|
598
|
+
environments: {
|
|
599
|
+
local: {},
|
|
600
|
+
staging: { url: "https://staging.acme.example" },
|
|
601
|
+
},
|
|
602
|
+
defaultEnvironments: ["local"],
|
|
603
|
+
}),
|
|
604
|
+
});
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
`setup` runs once for each system, after `ready` answers and before the
|
|
608
|
+
first scenario, and what it returns reaches every step as the
|
|
609
|
+
`setupResult` fixture. One system is then one test tenant, so two runs
|
|
610
|
+
against the same staging environment do not read each other's data.
|
|
611
|
+
|
|
612
|
+
The program provides what the command line used to: a small API that
|
|
613
|
+
creates a tenant, seeds its data, and switches its flags, mounted in the
|
|
614
|
+
test and staging builds and nowhere else.
|
|
615
|
+
|
|
616
|
+
<!-- engine: neither -->
|
|
617
|
+
```ruby
|
|
618
|
+
# config/routes.rb
|
|
619
|
+
post "/test/tenants" => "test_tenants#create" if Rails.configuration.x.test_api
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
A step reads the tenant from `setupResult` and sends it with each
|
|
623
|
+
request, so the program answers for that tenant alone:
|
|
624
|
+
|
|
625
|
+
```ruby
|
|
626
|
+
def run(request:, setup_result:)
|
|
627
|
+
answer = request.get("/todos", headers: { "x-tenant" => setup_result["tenant"] })
|
|
628
|
+
{ titles: answer.json.map { _1["title"] } }
|
|
629
|
+
end
|
|
630
|
+
```
|
|
631
|
+
|
|
632
|
+
```ts
|
|
633
|
+
async run({ request, setupResult }) {
|
|
634
|
+
const answer = await request.get("/todos", { headers: { "x-tenant": setupResult.tenant } });
|
|
635
|
+
return { titles: ((await answer.json()) as { title: string }[]).map((todo) => todo.title) };
|
|
636
|
+
}
|
|
637
|
+
```
|
|
638
|
+
|
|
639
|
+
In a system that declares `"url"`, `runCommand`
|
|
640
|
+
(`Nukadoko.run_command` in the gem) starts nothing and throws instead,
|
|
641
|
+
wherever it is called, and `nuka check` reports a step whose body calls
|
|
642
|
+
it as a `steps-reach-command` note before anything runs:
|
|
643
|
+
`nuka check --fail-on steps-reach-command` makes that note fail a job.
|
|
644
|
+
An environment of its own may open the door again with
|
|
645
|
+
`stepsReach: "any"`, for the system a team starts on its own machine.
|
|
646
|
+
"Steps reach" in [the spec](spec.md#steps-reach) has every rule.
|
|
647
|
+
|
|
648
|
+
## Where to go next
|
|
649
|
+
|
|
650
|
+
- [CI](ci.md): how to run the scenarios on every pull request, a browser
|
|
651
|
+
project included.
|
|
652
|
+
- [Commands](commands.md): every `nuka` command and its flags.
|
|
653
|
+
- [Writing scenarios](writing-scenarios.md): how to word feature lines
|
|
654
|
+
and claims.
|
|
655
|
+
- [Ruby reference](ruby/reference.md) and
|
|
656
|
+
[TypeScript reference](typescript/reference.md): the fixtures and the
|
|
657
|
+
program in each engine.
|
|
658
|
+
|
|
659
|
+
[Japanese](web-app.ja.md)
|