summaryrefslogtreecommitdiff
path: root/packages/db/test/local-prod.test.js
blob: 9bd56dad05f087824bfe7319947b8b236d699e67 (plain) (blame)
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
import assert from 'node:assert/strict';
import { relative } from 'node:path';
import { after, before, describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import testAdapter from '../../astro/test/test-adapter.js';
import { loadFixture } from '../../astro/test/test-utils.js';

describe('astro:db local database', () => {
	let fixture;
	before(async () => {
		fixture = await loadFixture({
			root: new URL('./fixtures/local-prod/', import.meta.url),
			output: 'server',
			adapter: testAdapter(),
		});
	});

	describe('build (not remote) with DATABASE_FILE env (file URL)', () => {
		const prodDbPath = new URL('./fixtures/basics/dist/astro.db', import.meta.url).toString();
		before(async () => {
			process.env.ASTRO_DATABASE_FILE = prodDbPath;
			await fixture.build();
		});

		after(async () => {
			delete process.env.ASTRO_DATABASE_FILE;
		});

		it('Can render page', async () => {
			const app = await fixture.loadTestAdapterApp();
			const request = new Request('http://example.com/');
			const response = await app.render(request);
			assert.equal(response.status, 200);
		});
	});

	describe('build (not remote) with DATABASE_FILE env (relative file path)', () => {
		const absoluteFileUrl = new URL('./fixtures/basics/dist/astro.db', import.meta.url);
		const prodDbPath = relative(process.cwd(), fileURLToPath(absoluteFileUrl));

		before(async () => {
			process.env.ASTRO_DATABASE_FILE = prodDbPath;
			await fixture.build();
		});

		after(async () => {
			delete process.env.ASTRO_DATABASE_FILE;
		});

		it('Can render page', async () => {
			const app = await fixture.loadTestAdapterApp();
			const request = new Request('http://example.com/');
			const response = await app.render(request);
			assert.equal(response.status, 200);
		});
	});

	describe('build (not remote)', () => {
		it('should throw during the build for server output', async () => {
			delete process.env.ASTRO_DATABASE_FILE;
			let buildError = null;
			try {
				await fixture.build();
			} catch (err) {
				buildError = err;
			}

			assert.equal(buildError instanceof Error, true);
		});

		it('should throw during the build for hybrid output', async () => {
			let fixture2 = await loadFixture({
				root: new URL('./fixtures/local-prod/', import.meta.url),
				output: 'static',
				adapter: testAdapter(),
			});

			delete process.env.ASTRO_DATABASE_FILE;
			let buildError = null;
			try {
				await fixture2.build();
			} catch (err) {
				buildError = err;
			}

			assert.equal(buildError instanceof Error, true);
		});
	});
});