-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodos.wp
More file actions
429 lines (385 loc) · 11.1 KB
/
Copy pathtodos.wp
File metadata and controls
429 lines (385 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# ================================================================
# Simple GraphQL-backed Todo App (HTML + Forms + POST/Redirect/GET)
# ================================================================
# ------------------------------------------------
# Config
# ------------------------------------------------
config pg {
host: $WP_PG_HOST || "localhost"
port: $WP_PG_PORT || "5432"
database: $WP_PG_DATABASE || "express-test"
user: $WP_PG_USER || "postgres"
password: $WP_PG_PASSWORD || "postgres"
ssl: false
initialPoolSize: 10
maxPoolSize: 20
}
config auth {
sessionTtl: 604800
cookieName: "wp_session"
cookieSecure: false
cookieHttpOnly: true
cookieSameSite: "Lax"
cookiePath: "/"
}
config log {
enabled: true
format: "json"
level: "debug"
includeBody: false
includeHeaders: true
maxBodySize: 1024
timestamp: true
}
config graphql {
endpoint: "/graphql"
}
# ------------------------------------------------
# GraphQL Schema for Todos (internal only)
# ------------------------------------------------
graphqlSchema = `
type Todo {
id: ID!
title: String!
completed: Boolean!
userId: Int!
createdAt: String!
updatedAt: String!
}
type Query {
todos(userId: Int!): [Todo!]!
}
type Mutation {
createTodo(userId: Int!, title: String!): Todo!
toggleTodo(userId: Int!, id: ID!): Todo!
deleteTodo(userId: Int!, id: ID!): Boolean!
}
`
# ------------------------------------------------
# GraphQL Resolvers (DB-backed)
# ------------------------------------------------
# Query: fetch all todos for a user
query todos =
|> pg([.userId]): `
SELECT
id,
title,
completed,
user_id AS "userId",
created_at AS "createdAt",
updated_at AS "updatedAt"
FROM todos
WHERE user_id = $1
ORDER BY created_at DESC
`
|> jq: `.data.rows`
# Mutation: create a todo
mutation createTodo =
|> pg([.title, .userId]): `
INSERT INTO todos (title, completed, user_id)
VALUES ($1, false, $2)
RETURNING
id,
title,
completed,
user_id AS "userId",
created_at AS "createdAt",
updated_at AS "updatedAt"
`
|> jq: `.data.rows[0]`
# Mutation: toggle completion flag
mutation toggleTodo =
|> pg([.id, .userId]): `
UPDATE todos
SET completed = NOT completed,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $2
RETURNING
id,
title,
completed,
user_id AS "userId",
created_at AS "createdAt",
updated_at AS "updatedAt"
`
|> jq: `.data.rows[0]`
# Mutation: delete a todo
mutation deleteTodo =
|> pg([.id, .userId]): `
DELETE FROM todos WHERE id = $1 AND user_id = $2
RETURNING true
`
|> jq: `.data.rows[0]`
# ------------------------------------------------
# Helper Pipelines
# ------------------------------------------------
# Derive a userId from header for local dev
# If x-user-id is not present, default to user 1
pipeline withUserIdFromHeader =
|> jq: `. + {
userId: ((.headers["x-user-id"] // "1") | tonumber)
}`
# HTML template for the Todo page
pipeline todoPageTemplate =
|> handlebars: `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Simple Todo App</title>
<style>
body { font-family: system-ui, sans-serif; margin: 2rem; }
h1 { margin-bottom: 1rem; }
form { margin-bottom: 1rem; }
.todo-list { list-style: none; padding: 0; }
.todo-item { margin: 0.5rem 0; }
.todo-item.completed .title { text-decoration: line-through; color: #666; }
.todo-item form { display: inline; margin-left: 0.5rem; }
</style>
</head>
<body>
<h1>Todos</h1>
<form method="POST" action="/todos">
<input
type="text"
name="title"
placeholder="New todo"
required
minlength="1"
/>
<button type="submit">Add</button>
</form>
<ul class="todo-list">
{{#each todos}}
{{> todoItemPartial}}
{{/each}}
</ul>
</body>
</html>
`
# ------------------------------------------------
# Handlebars Partials
# ------------------------------------------------
handlebars todoItemPartial = `
<li class="todo-item {{#if completed}}completed{{/if}}">
<span class="title">{{title}}</span>
<form method="POST" action="/todos/{{id}}/toggle">
<button type="submit">
{{#if completed}}Mark incomplete{{else}}Mark complete{{/if}}
</button>
</form>
<form method="POST" action="/todos/{{id}}/delete">
<button type="submit">Delete</button>
</form>
</li>
`
# ================================================================
# Public HTTP Endpoints (HTML + Forms, backed by GraphQL)
# ================================================================
# GET /todos - render HTML page with current user's todos
GET /todos
# |> log: `level: debug`
|> pipeline: withUserIdFromHeader
|> graphql({ userId: .userId }): `
query($userId: Int!) {
todos(userId: $userId) {
id
title
completed
userId
createdAt
updatedAt
}
}
`
|> jq: `{ todos: .data.todos }`
|> pipeline: todoPageTemplate
# POST /todos - create a new todo from form, then re-render page
# (This one still returns HTML; URL stays /todos, which is fine.)
POST /todos
|> log: `level: debug`
|> pipeline: withUserIdFromHeader
|> validate: `
title: string(1..100)
`
|> graphql({ userId: .userId, title: .body.title }): `
mutation($userId: Int!, $title: String!) {
createTodo(userId: $userId, title: $title) {
id
}
}
`
|> result
ok(303):
|> jq: `{ setHeaders: { "Location": "/todos" } }`
# POST /todos/:id/toggle - toggle completion, then redirect to /todos
POST /todos/:id/toggle
|> log: `level: debug`
|> pipeline: withUserIdFromHeader
|> graphql({ userId: .userId, id: (.params.id | tonumber) }): `
mutation($userId: Int!, $id: ID!) {
toggleTodo(userId: $userId, id: $id) {
id
}
}
`
|> result
ok(303):
|> jq: `{ setHeaders: { "Location": "/todos" } }`
# POST /todos/:id/delete - delete todo, then redirect to /todos
POST /todos/:id/delete
|> log: `level: debug`
|> pipeline: withUserIdFromHeader
|> graphql({ userId: .userId, id: (.params.id | tonumber) }): `
mutation($userId: Int!, $id: ID!) {
deleteTodo(userId: $userId, id: $id)
}
`
|> result
ok(303):
|> jq: `{ setHeaders: { "Location": "/todos" } }`
# Optional: tiny home route that links to /todos
GET /
|> handlebars: `
<!DOCTYPE html>
<html>
<head><title>Home</title></head>
<body>
<h1>Home</h1>
<p><a href="/todos">Go to Todo App</a></p>
</body>
</html>
`
# ================================================================
# Tests
# ================================================================
# ------------------------------------------------
# Template / Pipeline unit test (like teamTemplate)
# ------------------------------------------------
describe "todoPageTemplate pipeline"
it "renders todos into HTML"
let todoTitle = "Pipeline todo"
when executing pipeline todoPageTemplate
with input `{
todos: [
{ id: 1, title: $todoTitle, completed: false }
]
}`
then selector `.todo-list` exists
and selector `.todo-item` exists
and selector `.todo-item .title` text equals "{{todoTitle}}"
and selector `form[action="/todos/1/toggle"]` exists
and selector `form[action="/todos/1/delete"]` exists
# ------------------------------------------------
# HTML Page Rendering (GraphQL mocked)
# ------------------------------------------------
describe "Simple Todo App - HTML list"
let userId = 1
with mock query todos returning `[
{
id: 1,
title: "First todo",
completed: false,
userId: $userId,
createdAt: "2024-01-01T00:00:00Z",
updatedAt: "2024-01-01T00:00:00Z"
},
{
id: 2,
title: "Second todo",
completed: true,
userId: $userId,
createdAt: "2024-01-02T00:00:00Z",
updatedAt: "2024-01-02T00:00:00Z"
}
]`
it "renders todos with forms using GraphQL behind the scenes"
when calling GET /todos
with headers `{
"x-user-id": ($userId | tostring)
}`
then status is 200
and selector `form[action="/todos"]` exists
and selector `input[name="title"]` exists
and selector `.todo-list > .todo-item` count equals 2
and selector `.todo-list > .todo-item:nth-child(1) .title` text equals "First todo"
and selector `.todo-list > .todo-item:nth-child(1) form[action="/todos/1/toggle"]` exists
and selector `.todo-list > .todo-item:nth-child(1) form[action="/todos/1/delete"]` exists
and selector `.todo-list > .todo-item:nth-child(2)` exists
and selector `.todo-list > .todo-item:nth-child(2) .title` text equals "Second todo"
and call query todos with `{
userId: $userId
}`
# ------------------------------------------------
# HTML Mutations (POST forms) using GraphQL mocks
# ------------------------------------------------
describe "Simple Todo App - HTML mutations via forms"
let userId = 2
let newTitle = "New todo from form"
let todoId = 5
with mock mutation createTodo returning `{
id: 10,
title: $newTitle,
completed: false,
userId: $userId,
createdAt: "2024-01-02T00:00:00Z",
updatedAt: "2024-01-02T00:00:00Z"
}`
with mock mutation toggleTodo returning `{
id: 10,
title: "Example toggled",
completed: true,
userId: $userId,
createdAt: "2024-01-01T00:00:00Z",
updatedAt: "2024-01-03T00:00:00Z"
}`
with mock mutation deleteTodo returning `true`
# When createTodo completes, the app re-fetches todos via query todos
with mock query todos returning `[
{
id: $todoId,
title: "Example toggled",
completed: true,
userId: $userId,
createdAt: "2024-01-01T00:00:00Z",
updatedAt: "2024-01-03T00:00:00Z"
}
]`
it "creates a todo using POST /todos and re-renders page"
when calling POST /todos
with headers `{
"x-user-id": ($userId | tostring),
"content-type": "application/json"
}`
and with body `{
title: $newTitle
}`
then status is 303
and call mutation createTodo with `{
userId: $userId,
title: $newTitle
}`
it "toggles a todo using POST /todos/:id/toggle and redirects to /todos"
when calling POST /todos/{{todoId}}/toggle
with headers `{
"x-user-id": ($userId | tostring),
"content-type": "application/json"
}`
then status is 303
and header "Location" equals "/todos"
and call mutation toggleTodo with `{
userId: $userId,
id: $todoId
}`
it "deletes a todo using POST /todos/:id/delete and redirects to /todos"
when calling POST /todos/{{todoId}}/delete
with headers `{
"x-user-id": ($userId | tostring),
"content-type": "application/json"
}`
then status is 303
and header "Location" equals "/todos"
and call mutation deleteTodo with `{
userId: $userId,
id: $todoId
}`