summaryrefslogtreecommitdiff
path: root/packages/db/test/unit/remote-info.test.js
blob: 2c58f28b7ba2a2fb5411dda9724282eda8600340 (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
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
import assert from 'node:assert';
import test, { after, beforeEach, describe } from 'node:test';
import { getManagedRemoteToken, getRemoteDatabaseInfo } from '../../dist/core/utils.js';
import { clearEnvironment } from '../test-utils.js';

describe('RemoteDatabaseInfo', () => {
	beforeEach(() => {
		clearEnvironment();
	});

	test('default remote info', () => {
		const dbInfo = getRemoteDatabaseInfo();

		assert.deepEqual(dbInfo, {
			type: 'studio',
			url: 'https://db.services.astro.build',
		});
	});

	test('configured Astro Studio remote', () => {
		process.env.ASTRO_STUDIO_REMOTE_DB_URL = 'https://studio.astro.build';
		const dbInfo = getRemoteDatabaseInfo();

		assert.deepEqual(dbInfo, {
			type: 'studio',
			url: 'https://studio.astro.build',
		});
	});

	test('configured libSQL remote', () => {
		process.env.ASTRO_DB_REMOTE_URL = 'libsql://libsql.self.hosted';
		const dbInfo = getRemoteDatabaseInfo();

		assert.deepEqual(dbInfo, {
			type: 'libsql',
			url: 'libsql://libsql.self.hosted',
		});
	});

	test('configured both libSQL and Studio remote', () => {
		process.env.ASTRO_DB_REMOTE_URL = 'libsql://libsql.self.hosted';
		process.env.ASTRO_STUDIO_REMOTE_DB_URL = 'https://studio.astro.build';
		const dbInfo = getRemoteDatabaseInfo();

		assert.deepEqual(dbInfo, {
			type: 'studio',
			url: 'https://studio.astro.build',
		});
	});
});

describe('RemoteManagedToken', () => {
	// Avoid conflicts with other tests
	beforeEach(() => {
		clearEnvironment();
		process.env.ASTRO_STUDIO_APP_TOKEN = 'studio token';
		process.env.ASTRO_DB_APP_TOKEN = 'db token';
	});
	after(() => {
		clearEnvironment();
	});

	test('given token for default remote', async () => {
		const { token } = await getManagedRemoteToken('given token');
		assert.equal(token, 'given token');
	});

	test('token for default remote', async () => {
		const { token } = await getManagedRemoteToken();

		assert.equal(token, 'studio token');
	});

	test('given token for configured Astro Studio remote', async () => {
		process.env.ASTRO_STUDIO_REMOTE_DB_URL = 'https://studio.astro.build';
		const { token } = await getManagedRemoteToken('given token');
		assert.equal(token, 'given token');
	});

	test('token for configured Astro Studio remote', async () => {
		process.env.ASTRO_STUDIO_REMOTE_DB_URL = 'https://studio.astro.build';
		const { token } = await getManagedRemoteToken();

		assert.equal(token, 'studio token');
	});

	test('given token for configured libSQL remote', async () => {
		process.env.ASTRO_DB_REMOTE_URL = 'libsql://libsql.self.hosted';
		const { token } = await getManagedRemoteToken('given token');
		assert.equal(token, 'given token');
	});

	test('token for configured libSQL remote', async () => {
		process.env.ASTRO_DB_REMOTE_URL = 'libsql://libsql.self.hosted';
		const { token } = await getManagedRemoteToken();

		assert.equal(token, 'db token');
	});

	test('token for given Astro Studio remote', async () => {
		process.env.ASTRO_DB_REMOTE_URL = 'libsql://libsql.self.hosted';
		const { token } = await getManagedRemoteToken(undefined, {
			type: 'studio',
			url: 'https://studio.astro.build',
		});

		assert.equal(token, 'studio token');
	});

	test('token for given libSQL remote', async () => {
		process.env.ASTRO_STUDIO_REMOTE_URL = 'libsql://libsql.self.hosted';
		const { token } = await getManagedRemoteToken(undefined, {
			type: 'libsql',
			url: 'libsql://libsql.self.hosted',
		});

		assert.equal(token, 'db token');
	});
});