ptywright 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 (46) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +188 -0
  3. package/bin/ptywright +4 -0
  4. package/package.json +76 -0
  5. package/schemas/ptywright-script.schema.json +297 -0
  6. package/skills/ptywright-testing/SKILL.md +102 -0
  7. package/src/cli.ts +414 -0
  8. package/src/generator/doc_parser.ts +341 -0
  9. package/src/generator/generate.ts +161 -0
  10. package/src/generator/index.ts +10 -0
  11. package/src/generator/script_generator.ts +209 -0
  12. package/src/generator/step_extractor.ts +397 -0
  13. package/src/index.ts +16 -0
  14. package/src/mcp/http_server.ts +174 -0
  15. package/src/mcp/script_recording.ts +238 -0
  16. package/src/mcp/server.ts +1348 -0
  17. package/src/pty/bun_pty_adapter.ts +34 -0
  18. package/src/pty/bun_terminal_adapter.ts +149 -0
  19. package/src/pty/pty_adapter.ts +31 -0
  20. package/src/script/dsl.ts +188 -0
  21. package/src/script/module.ts +43 -0
  22. package/src/script/path.ts +151 -0
  23. package/src/script/run.ts +108 -0
  24. package/src/script/run_all.ts +229 -0
  25. package/src/script/runner.ts +983 -0
  26. package/src/script/schema.ts +237 -0
  27. package/src/script/steps/assert_snapshot_equals.ts +21 -0
  28. package/src/script/steps/index.ts +2 -0
  29. package/src/script/suite_report.ts +626 -0
  30. package/src/session/session_manager.ts +145 -0
  31. package/src/session/terminal_session.ts +473 -0
  32. package/src/terminal/ansi.ts +142 -0
  33. package/src/terminal/keys.ts +180 -0
  34. package/src/terminal/mask.ts +70 -0
  35. package/src/terminal/mouse.ts +75 -0
  36. package/src/terminal/snapshot.ts +196 -0
  37. package/src/terminal/style.ts +121 -0
  38. package/src/terminal/view.ts +49 -0
  39. package/src/trace/asciicast.ts +20 -0
  40. package/src/trace/asciinema_player_assets.ts +44 -0
  41. package/src/trace/cast_to_txt.ts +116 -0
  42. package/src/trace/recorder.ts +110 -0
  43. package/src/trace/report.ts +2092 -0
  44. package/src/types.ts +86 -0
  45. package/src/util/hash.ts +8 -0
  46. package/src/util/sleep.ts +5 -0
@@ -0,0 +1,237 @@
1
+ import { z } from "zod";
2
+
3
+ export const textMaskRuleSchema = z.object({
4
+ regex: z.string().min(1),
5
+ flags: z.string().optional(),
6
+ replacement: z.string().optional(),
7
+ preserveLength: z.boolean().optional(),
8
+ });
9
+
10
+ export const launchConfigSchema = z.object({
11
+ command: z.string().min(1),
12
+ args: z.array(z.string()).optional(),
13
+ cwd: z.string().optional(),
14
+ env: z.record(z.string()).optional(),
15
+ cols: z.number().int().positive().optional(),
16
+ rows: z.number().int().positive().optional(),
17
+ name: z.string().optional(),
18
+ });
19
+
20
+ export const scriptTraceSchema = z.object({
21
+ saveCast: z.boolean().optional(),
22
+ saveReport: z.boolean().optional(),
23
+ castPath: z.string().optional(),
24
+ reportPath: z.string().optional(),
25
+ reportScope: z.enum(["visible", "buffer"]).optional(),
26
+ reportMaxFrames: z.number().int().positive().optional(),
27
+ });
28
+
29
+ const sendTextStepSchema = z.object({
30
+ type: z.literal("sendText"),
31
+ text: z.string(),
32
+ enter: z.boolean().optional(),
33
+ });
34
+
35
+ const pressKeyStepSchema = z.object({
36
+ type: z.literal("pressKey"),
37
+ key: z.string().min(1),
38
+ });
39
+
40
+ const sendMouseStepSchema = z.object({
41
+ type: z.literal("sendMouse"),
42
+ action: z.enum(["down", "up", "move", "click", "scroll_up", "scroll_down"]),
43
+ x: z.number().int(),
44
+ y: z.number().int(),
45
+ button: z.enum(["left", "middle", "right"]).optional(),
46
+ shift: z.boolean().optional(),
47
+ alt: z.boolean().optional(),
48
+ ctrl: z.boolean().optional(),
49
+ });
50
+
51
+ const resizeStepSchema = z.object({
52
+ type: z.literal("resize"),
53
+ cols: z.number().int().positive(),
54
+ rows: z.number().int().positive(),
55
+ });
56
+
57
+ const markStepSchema = z.object({
58
+ type: z.literal("mark"),
59
+ label: z.string().optional(),
60
+ });
61
+
62
+ const sleepStepSchema = z.object({
63
+ type: z.literal("sleep"),
64
+ ms: z.number().int().nonnegative(),
65
+ });
66
+
67
+ const waitForTextStepSchema = z
68
+ .object({
69
+ type: z.literal("waitForText"),
70
+ scope: z.enum(["visible", "buffer"]).optional(),
71
+ text: z.string().optional(),
72
+ regex: z.string().optional(),
73
+ timeoutMs: z.number().int().positive().optional(),
74
+ intervalMs: z.number().int().positive().optional(),
75
+ })
76
+ .superRefine((value, ctx) => {
77
+ if (!value.text && !value.regex) {
78
+ ctx.addIssue({
79
+ code: z.ZodIssueCode.custom,
80
+ message: "waitForText requires text or regex",
81
+ });
82
+ }
83
+ });
84
+
85
+ const waitForStableScreenStepSchema = z.object({
86
+ type: z.literal("waitForStableScreen"),
87
+ timeoutMs: z.number().int().positive().optional(),
88
+ quietMs: z.number().int().positive().optional(),
89
+ intervalMs: z.number().int().positive().optional(),
90
+ });
91
+
92
+ const waitForExitStepSchema = z.object({
93
+ type: z.literal("waitForExit"),
94
+ timeoutMs: z.number().int().positive().optional(),
95
+ intervalMs: z.number().int().positive().optional(),
96
+ exitCode: z.number().int().optional(),
97
+ signal: z.union([z.number().int(), z.string()]).optional(),
98
+ });
99
+
100
+ const expectMetaStepSchema = z
101
+ .object({
102
+ type: z.literal("expectMeta"),
103
+ bufferType: z.enum(["normal", "alternate"]).optional(),
104
+ cols: z.number().int().positive().optional(),
105
+ rows: z.number().int().positive().optional(),
106
+ cursor: z
107
+ .object({
108
+ x: z.number().int().positive(),
109
+ y: z.number().int().positive(),
110
+ })
111
+ .optional(),
112
+ })
113
+ .superRefine((value, ctx) => {
114
+ if (
115
+ value.bufferType === undefined &&
116
+ value.cols === undefined &&
117
+ value.rows === undefined &&
118
+ value.cursor === undefined
119
+ ) {
120
+ ctx.addIssue({
121
+ code: z.ZodIssueCode.custom,
122
+ message: "expectMeta requires at least one assertion (bufferType/cols/rows/cursor)",
123
+ });
124
+ }
125
+ });
126
+
127
+ const snapshotStepSchema = z
128
+ .object({
129
+ type: z.literal("snapshot"),
130
+ kind: z.enum(["text", "view", "ansi", "view_ansi", "grid"]),
131
+ scope: z.enum(["visible", "buffer"]).optional(),
132
+ trimRight: z.boolean().optional(),
133
+ trimBottom: z.boolean().optional(),
134
+ maxLines: z.number().int().positive().optional(),
135
+ tailLines: z.number().int().positive().optional(),
136
+ lineNumbers: z.boolean().optional(),
137
+ includeStyles: z.boolean().optional(),
138
+ mask: z.array(textMaskRuleSchema).optional(),
139
+ saveAs: z.string().optional(),
140
+ saveTo: z.string().optional(),
141
+ })
142
+ .superRefine((value, ctx) => {
143
+ if (value.maxLines !== undefined && value.tailLines !== undefined) {
144
+ ctx.addIssue({
145
+ code: z.ZodIssueCode.custom,
146
+ message: "snapshot: maxLines and tailLines are mutually exclusive",
147
+ });
148
+ }
149
+ });
150
+
151
+ const expectStepSchema = z
152
+ .object({
153
+ type: z.literal("expect"),
154
+ from: z.string().optional(),
155
+ equals: z.string().optional(),
156
+ contains: z.array(z.string()).optional(),
157
+ notContains: z.array(z.string()).optional(),
158
+ regex: z.string().optional(),
159
+ })
160
+ .superRefine((value, ctx) => {
161
+ if (
162
+ value.equals === undefined &&
163
+ !value.contains?.length &&
164
+ !value.notContains?.length &&
165
+ !value.regex
166
+ ) {
167
+ ctx.addIssue({
168
+ code: z.ZodIssueCode.custom,
169
+ message: "expect requires at least one matcher (equals/contains/notContains/regex)",
170
+ });
171
+ }
172
+ });
173
+
174
+ const expectGoldenStepSchema = z.object({
175
+ type: z.literal("expectGolden"),
176
+ from: z.string().optional(),
177
+ path: z.string().min(1),
178
+ });
179
+
180
+ const customStepSchema = z.object({
181
+ type: z.literal("custom"),
182
+ name: z.string().min(1),
183
+ payload: z.unknown().optional(),
184
+ });
185
+
186
+ const assertStepSchema = z
187
+ .object({
188
+ type: z.literal("assert"),
189
+ scope: z.enum(["visible", "buffer"]).optional(),
190
+ text: z.string().optional(),
191
+ regex: z.string().optional(),
192
+ description: z.string().optional(),
193
+ })
194
+ .superRefine((value, ctx) => {
195
+ if (!value.text && !value.regex) {
196
+ ctx.addIssue({
197
+ code: z.ZodIssueCode.custom,
198
+ message: "assert requires text or regex",
199
+ });
200
+ }
201
+ });
202
+
203
+ const assertSemanticStepSchema = z.object({
204
+ type: z.literal("assertSemantic"),
205
+ prompt: z.string().min(1),
206
+ description: z.string().optional(),
207
+ });
208
+
209
+ export const scriptStepSchema = z.union([
210
+ sendTextStepSchema,
211
+ pressKeyStepSchema,
212
+ sendMouseStepSchema,
213
+ resizeStepSchema,
214
+ markStepSchema,
215
+ sleepStepSchema,
216
+ waitForTextStepSchema,
217
+ waitForStableScreenStepSchema,
218
+ waitForExitStepSchema,
219
+ expectMetaStepSchema,
220
+ snapshotStepSchema,
221
+ expectStepSchema,
222
+ expectGoldenStepSchema,
223
+ customStepSchema,
224
+ assertStepSchema,
225
+ assertSemanticStepSchema,
226
+ ]);
227
+
228
+ export const scriptSchema = z.object({
229
+ name: z.string().optional(),
230
+ artifactsDir: z.string().optional(),
231
+ launch: launchConfigSchema,
232
+ trace: scriptTraceSchema.optional(),
233
+ steps: z.array(scriptStepSchema).min(1),
234
+ });
235
+
236
+ export type Script = z.infer<typeof scriptSchema>;
237
+ export type ScriptStep = z.infer<typeof scriptStepSchema>;
@@ -0,0 +1,21 @@
1
+ import type { CustomStepHandler } from "../runner";
2
+
3
+ export type AssertSnapshotEqualsPayload = {
4
+ expected: string;
5
+ };
6
+
7
+ export function createAssertSnapshotEqualsStep(
8
+ from: string,
9
+ ): CustomStepHandler<AssertSnapshotEqualsPayload> {
10
+ return (ctx, step) => {
11
+ const expected = step.payload?.expected;
12
+ if (typeof expected !== "string") {
13
+ throw new Error("assertSnapshotEquals: missing payload.expected");
14
+ }
15
+
16
+ const record = ctx.getSnapshot(from);
17
+ if (record.text.trimEnd() !== expected) {
18
+ throw new Error("assertSnapshotEquals failed");
19
+ }
20
+ };
21
+ }
@@ -0,0 +1,2 @@
1
+ export { createAssertSnapshotEqualsStep } from "./assert_snapshot_equals";
2
+ export type { AssertSnapshotEqualsPayload } from "./assert_snapshot_equals";