create-acrylblends 0.2.4 → 0.2.6

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.
@@ -1,10 +1,19 @@
1
+ import type { Db } from '__PACKAGE__/db'
1
2
  import { testClient } from '__PACKAGE__/testing'
2
3
  import assert from 'node:assert/strict'
3
- import type { DatabaseSync } from 'node:sqlite'
4
- import { test } from 'node:test'
5
- import { openDb } from '../src/db.ts'
4
+ import { after, test } from 'node:test'
5
+ import { openNotesDb } from '../src/db.ts'
6
6
  import { createApi, createApp } from '../src/notes-app.ts'
7
7
 
8
+ /** A fresh database in memory with the real migrations; every one is closed when the tests end. */
9
+ const closers: (() => Promise<void>)[] = []
10
+ after(() => Promise.all(closers.map((close) => close())))
11
+ async function memoryDb(): Promise<Db> {
12
+ const { db, close } = await openNotesDb(':memory:')
13
+ closers.push(close)
14
+ return db
15
+ }
16
+
8
17
  const form = (data: Record<string, string>, cookie = ''): RequestInit => ({
9
18
  method: 'POST',
10
19
  body: new URLSearchParams(data),
@@ -18,7 +27,7 @@ async function signedUp(app: ReturnType<typeof createApp>, email = 'a@example.co
18
27
  }
19
28
 
20
29
  test('the home page sends a visitor to the notes, which send them to log in', async () => {
21
- const app = createApp(openDb(':memory:'))
30
+ const app = createApp(await memoryDb())
22
31
  assert.equal((await app.request('/')).headers.get('location'), '/notes')
23
32
  const res = await app.request('/notes')
24
33
  assert.equal(res.status, 302)
@@ -26,7 +35,7 @@ test('the home page sends a visitor to the notes, which send them to log in', as
26
35
  })
27
36
 
28
37
  test('sign up validates, refuses a duplicate, and starts a session', async () => {
29
- const app = createApp(openDb(':memory:'))
38
+ const app = createApp(await memoryDb())
30
39
  assert.match(await (await app.request('/signup')).text(), /Create account/)
31
40
  const bad = await app.request('/signup', form({ email: 'nope', password: 'short' }))
32
41
  assert.equal(bad.status, 400)
@@ -44,7 +53,7 @@ test('sign up validates, refuses a duplicate, and starts a session', async () =>
44
53
  })
45
54
 
46
55
  test('log in checks the password; log out ends the session', async () => {
47
- const app = createApp(openDb(':memory:'))
56
+ const app = createApp(await memoryDb())
48
57
  const first = await signedUp(app)
49
58
  assert.match(await (await app.request('/login')).text(), /Log in/)
50
59
  const wrong = await app.request(
@@ -69,7 +78,7 @@ test('log in checks the password; log out ends the session', async () => {
69
78
  })
70
79
 
71
80
  test('create, search, edit and delete a note through the forms', async () => {
72
- const app = createApp(openDb(':memory:'))
81
+ const app = createApp(await memoryDb())
73
82
  const cookie = await signedUp(app)
74
83
  assert.match(
75
84
  await (await app.request('/notes/new', { headers: { cookie } })).text(),
@@ -123,7 +132,7 @@ test('create, search, edit and delete a note through the forms', async () => {
123
132
  })
124
133
 
125
134
  test("one user cannot read or change another user's notes", async () => {
126
- const app = createApp(openDb(':memory:'))
135
+ const app = createApp(await memoryDb())
127
136
  const alice = await signedUp(app, 'alice@example.com')
128
137
  const bob = await signedUp(app, 'bob@example.com')
129
138
  await app.request('/notes', form({ title: 'Secret', body: 'alice only' }, alice))
@@ -133,7 +142,7 @@ test("one user cannot read or change another user's notes", async () => {
133
142
  })
134
143
 
135
144
  test('the JSON API needs a session and validates input', async () => {
136
- const db = openDb(':memory:')
145
+ const db = await memoryDb()
137
146
  const app = createApp(db)
138
147
  const cookie = await signedUp(app)
139
148
  const res = await app.request('/api/notes')
@@ -149,7 +158,7 @@ test('the JSON API needs a session and validates input', async () => {
149
158
  })
150
159
 
151
160
  test('the typed client covers every API route', async () => {
152
- const db = openDb(':memory:')
161
+ const db = await memoryDb()
153
162
  const app = createApp(db)
154
163
  const cookie = await signedUp(app)
155
164
  const client = testClient(createApi(db))
@@ -180,7 +189,7 @@ test('the typed client covers every API route', async () => {
180
189
  })
181
190
 
182
191
  test('csrf refuses a form POST from another site', async () => {
183
- const app = createApp(openDb(':memory:'))
192
+ const app = createApp(await memoryDb())
184
193
  const res = await app.request('/signup', {
185
194
  method: 'POST',
186
195
  body: new URLSearchParams({ email: 'a@example.com', password: 'correct horse' }),
@@ -192,7 +201,7 @@ test('csrf refuses a form POST from another site', async () => {
192
201
  test('the session cookie is Secure over HTTPS, and behind a proxy only when the proxy is trusted', async () => {
193
202
  const body = new URLSearchParams({ email: 'a@example.com', password: 'correct horse' })
194
203
  const flags = async (url: string, headers: Record<string, string>, trustProxy: boolean) => {
195
- const app = createApp(openDb(':memory:'), { trustProxy })
204
+ const app = createApp(await memoryDb(), { trustProxy })
196
205
  const origin = new URL(url).origin
197
206
  const res = await app.request(url, { method: 'POST', body, headers: { origin, ...headers } })
198
207
  assert.equal(res.status, 303)
@@ -206,7 +215,7 @@ test('the session cookie is Secure over HTTPS, and behind a proxy only when the
206
215
  })
207
216
 
208
217
  test('behind a proxy csrf accepts the public origin and still refuses others', async () => {
209
- const app = createApp(openDb(':memory:'), { publicOrigin: 'https://notes.example.com' })
218
+ const app = createApp(await memoryDb(), { publicOrigin: 'https://notes.example.com' })
210
219
  const post = (origin: string) =>
211
220
  app.request('/signup', {
212
221
  method: 'POST',
@@ -218,12 +227,10 @@ test('behind a proxy csrf accepts the public origin and still refuses others', a
218
227
  })
219
228
 
220
229
  test('an error answers a plain 500 with no stack and no detail', async () => {
221
- const broken = {
222
- prepare() {
223
- throw new Error('secret detail at /srv/app/db.ts:1')
224
- },
225
- exec() {},
226
- } as unknown as DatabaseSync
230
+ const fail = async (): Promise<never> => {
231
+ throw new Error('secret detail at /srv/app/db.ts:1')
232
+ }
233
+ const broken: Db = { exec: fail, run: fail, get: fail, all: fail, transaction: fail }
227
234
  const app = createApp(broken)
228
235
  const res = await app.request('/notes', { headers: { cookie: 'sid=anything' } })
229
236
  assert.equal(res.status, 500)
@@ -231,7 +238,7 @@ test('an error answers a plain 500 with no stack and no detail', async () => {
231
238
  })
232
239
 
233
240
  test('the note form loads the character counter, a client component compiled for the browser', async () => {
234
- const app = createApp(openDb(':memory:'))
241
+ const app = createApp(await memoryDb())
235
242
  const cookie = await signedUp(app)
236
243
  assert.match(
237
244
  await (await app.request('/notes/new', { headers: { cookie } })).text(),
@@ -15,7 +15,7 @@ async function start(env: Record<string, string> = {}) {
15
15
  env: {
16
16
  ...process.env,
17
17
  PORT: '0',
18
- NOTES_DB: join(mkdtempSync(join(tmpdir(), 'notes-')), 'notes.db'),
18
+ NOTES_DB: join(mkdtempSync(join(tmpdir(), 'notes-')), 'notes.sqlite'),
19
19
  ...env,
20
20
  },
21
21
  stdio: ['ignore', 'pipe', 'inherit'],