-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.test.js
More file actions
109 lines (99 loc) · 3.37 KB
/
Copy pathplatform.test.js
File metadata and controls
109 lines (99 loc) · 3.37 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
#!/usr/bin/env node
/* ==============================================
* TEST — platform enum (<=50 col house)
* Proves the declarative tree (platform.js):
* 1. the enum is closed and exhaustive
* 2. every planet detects from data alone
* 3. precedence: specific beats general
* 4. backends are DERIVED, never hand-listed
* 5. a malformed leaf is a build error
* Detection is pure over injected globals, so
* we can stand on ten fake planets in one run.
* No framework. `node test-platform.js`.
* ============================================ */
'use strict';
const P = require('./platform');
let pass = 0, fail = 0;
function ok(name, cond) {
console.log(
(cond ? ' ok ' : ' FAIL ') + name);
cond ? pass++ : fail++;
}
// -- 1. closed + exhaustive -------------------
// (platform.js already validate()d itself at
// load — requiring it without a throw IS the
// build check. Assert the census anyway.)
ok('enum is closed: 11 leaves, 4 kinds',
P.leaves().length === 11 &&
Object.keys(P.TREE).length === 4);
// -- 2. ten fake planets ----------------------
const planets = [
[{ WebSocketPair: 1 },
'serverless.cloudflare-worker'],
[{ WebSocketPair: 1, DurableObject: 1 },
'serverless.durable-object'],
[{ process: { env: { FIREBASE_CONFIG: 'x' },
versions: { node: '20' } } },
'serverless.firebase-fn'],
[{ process: { env: { K_SERVICE: 'fn' },
versions: { node: '20' } } },
'serverless.gcp-cloud-fn'],
[{ navigator: { product: 'ReactNative' } },
'native.react-native'],
[{ window: 1, document: 1 },
'web.plain-html'],
[{ window: 1, document: 1, React: 1 },
'web.react'],
[{ Bun: 1 }, 'runtime.bun'],
[{ process: { versions: { node: '20' } } },
'runtime.node'],
[{}, 'unknown'],
];
for (const [g, want] of planets)
ok('detects ' + want, P.detect(g) === want);
// -- 3. precedence is declaration order -------
ok('bun outranks node (bun ships both)',
P.detect({ process: { versions:
{ node: '20', bun: '1.1' } } })
=== 'runtime.bun');
ok('firebase-fn outranks plain node',
P.detect({ process: {
env: { FIREBASE_CONFIG: 'x' },
versions: { node: '20' } } })
!== 'runtime.node');
// -- 4. backends derive from caps -------------
const node = P.diagnose(
{ process: { versions: { node: '20' } } });
ok('node: admin + pear derived',
node.backends.includes('instant-admin') &&
node.backends.includes('pear'));
const web = P.diagnose(
{ window: 1, document: 1 });
ok('web: client derived, admin FORBIDDEN',
web.backends.includes('instant-client') &&
!web.backends.includes('instant-admin'));
ok('unknown planet: memory only',
P.diagnose({}).backends
.join() === 'memory');
ok('recommend() = default + full stack',
P.recommend({}).use === 'memory' &&
P.recommend({ Bun: 1 }).stack
.includes('pear'));
// -- 5. malformed leaf = build error ----------
let msg = '';
try {
P.validate({ web: { oops: {
caps: { admin: false }, // caps missing
sniff: () => true, // client/pear/..
} } });
} catch (e) { msg = e.message; }
ok('half-declared leaf refuses to build',
/missing cap/.test(msg));
// -- and the ground we stand on ---------------
ok('this very test runs on runtime.node',
P.detect() === 'runtime.node');
ok('report() renders the box',
P.report().includes('runtime.node'));
console.log('\n ' + pass + ' passed, ' +
fail + ' failed');
process.exit(fail ? 1 : 0);