generated from rustworthy/npm-javascript-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storedge.test.js
117 lines (104 loc) · 3.66 KB
/
storedge.test.js
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
'use strict';
const assert = require('node:assert');
const { describe, it, after, before } = require('node:test');
const { BSON, ObjectId } = require('bson');
const StorEdge = require('./storedge.js');
describe('StorEdge client connection', () => {
it('should fail if env file not found', async () => {
const envFile = 'aff54a.secret.vault';
const [error, client] = StorEdge.with({ envFile }).build();
assert.equal(client, null);
// encountered this issue in CI on windows platform,
// specifically with Node.js 20 on windows-latest:
// expected: "ENOENT: no such file or directory, open 'afasf232__&&csdsr123#4e12rqc'"
// actual: "ENOENT: no such file or directory, open 'D:\\a\\storedge\\storedge\\afasf232__&&csdsr123#4e12rqc'"
// so going with a more generic assertion:
const noSuchFile = 'ENOENT: no such file or directory';
assert(error.message.startsWith(noSuchFile));
assert(error.message.includes(envFile));
});
it('should fail when pinging invalid endpoint', async () => {
after(function cleanUp() {
delete process.env.UPSTASH_REDIS_REST_URL;
delete process.env.UPSTASH_REDIS_REST_TOKEN;
});
const storedge = StorEdge.with({ envFile: '.env.sample' });
const [error, client] = await storedge.buildWithPing();
assert.equal(client, null);
assert.equal(error.message, 'fetch failed');
});
it('succeeds if valid creds in dot-env file', async () => {
const [error, client] = StorEdge.with({ envFile: '.env' }).build();
assert.equal(error, null);
assert(client instanceof StorEdge);
});
it('succeeds if valid creds loaded to execution environment', async () => {
const [error, client] = StorEdge.build();
assert.equal(error, null);
assert(client instanceof StorEdge);
});
});
describe('StorEdge KV for primitive cases', () => {
let client1, client2;
before(async () => {
// Also valid instantiation:
client1 = new StorEdge({ envFile: '.env' }).build()[1];
client2 = new StorEdge().build()[1];
});
it('client2 can get a primitive after clien1 set it', async () => {
const key = 'testPrimitiveCase';
const value = 123;
const status = await client1.KV.set(key, value);
assert.equal(status, 'OK');
const resultKV = await client2.KV.get(key);
assert.equal(resultKV, value);
});
});
describe('StorEdge Durables for primitive cases', () => {
let client1, client2;
before(async () => {
client1 = new StorEdge({ envFile: '.env' }).build()[1];
client2 = new StorEdge().build()[1];
});
it('client2 can get a primitive after clien1 set it', async () => {
const key = 'testObjectCase_Durables';
const value = {
_id: new ObjectId(),
name: 'Paul Michelle',
patterns: [/^Miche/g, /^Pa/i, /^Mi/g],
age: 42,
born: new Date(1979, 3, 21),
likes: ['food', 'movies', 'sports'],
contact: {
email: 'email@address.io',
phone: '555-555-5555',
},
visited: true,
reviews: null,
secretKey: BSON.serialize({ key: new ObjectId() }).toString(),
backUpKey1: new Buffer.from([1, 2, 3, 4, 5, 6, 7, 8]).toString('base64'),
backUpKey2: '\x01\x02\x03\x04\x05\x06\x07\x08',
location: {
type: 'Polygon',
coordinates: [
[
[0, 0],
[3, 6],
[6, 1],
[0, 0],
],
[
[2, 2],
[3, 3],
[4, 2],
[2, 2],
],
],
},
};
const id = await client1.Durables.put(value, { key });
assert.equal(id, key);
const resultDurables = await client2.Durables.get(id);
assert.deepStrictEqual(resultDurables, value);
});
});