-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathintegration.test.ts
More file actions
140 lines (117 loc) · 4.6 KB
/
Copy pathintegration.test.ts
File metadata and controls
140 lines (117 loc) · 4.6 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2019-Present Datadog, Inc.
import * as fsHelpers from '@dd/core/helpers/fs';
import { runBundlers } from '@dd/tests/_jest/helpers/runBundlers';
import fsp from 'fs/promises';
import path from 'path';
import { pathToFileURL } from 'url';
const BACKEND_OUT_DIR_PREFIX = 'dd-apps-backend-';
const BUNDLE_FILENAME_RE = /^[0-9a-f]{64}\.(.+)\.js$/;
type BackendMainModule = {
main: (context: unknown) => Promise<unknown>;
};
function isBackendMainModule(mod: unknown): mod is BackendMainModule {
if (typeof mod !== 'object' || mod === null) {
return false;
}
if (!('main' in mod)) {
return false;
}
return typeof mod.main === 'function';
}
describe('apps backend runtime — real @datadog/apps-backend integration', () => {
let backendOutDir: string | undefined;
let bundlesByFunctionName: Map<string, string>;
beforeAll(async () => {
const realRm = fsHelpers.rm;
const rmSpy = jest.spyOn(fsHelpers, 'rm').mockImplementation(async (dir: string) => {
if (dir.includes(BACKEND_OUT_DIR_PREFIX)) {
backendOutDir = dir;
return;
}
await realRm(dir);
});
const { errors } = await runBundlers(
{ apps: { identifier: 'app-id', name: 'test-app', dryRun: true } },
{ entry: { main: './apps_backend_project/main.ts' } },
['vite'],
);
rmSpy.mockRestore();
if (errors.length > 0) {
throw new Error(`Expected no build errors, got: ${errors.join(', ')}`);
}
if (!backendOutDir) {
throw new Error('Expected the apps plugin to build backend function bundles.');
}
const files = await fsp.readdir(backendOutDir);
bundlesByFunctionName = new Map();
for (const file of files) {
const match = BUNDLE_FILENAME_RE.exec(file);
if (match) {
bundlesByFunctionName.set(match[1], path.join(backendOutDir, file));
}
}
}, 30000);
afterAll(async () => {
if (backendOutDir) {
await fsp.rm(backendOutDir, { recursive: true, force: true });
}
});
const importBackendMain = async (functionName: string) => {
const bundlePath = bundlesByFunctionName.get(functionName);
if (!bundlePath) {
throw new Error(`No emitted bundle found for backend function "${functionName}".`);
}
const mod: unknown = await import(pathToFileURL(bundlePath).href);
if (!isBackendMainModule(mod)) {
throw new Error(`Expected ${bundlePath} to export a "main" function.`);
}
return mod.main;
};
const validSource = () => ({
initiator: { id: 'initiator-id', orgId: 'org-1' },
runAsUser: { id: 'run-as-id', orgId: 'org-1' },
});
test('resolves execution and initiating users from the real SDK', async () => {
const main = await importBackendMain('getRuntimeUsers');
const { initiator, runAsUser } = validSource();
const result = await main({
Source: { initiator, runAsUser },
backendFunctionArgs: ['integration-test'],
});
expect(result).toEqual({
label: 'integration-test',
executionUser: runAsUser,
initiatingUser: initiator,
});
});
test('forwards arguments to the backend function', async () => {
const main = await importBackendMain('plainEcho');
const result = await main({
Source: validSource(),
backendFunctionArgs: ['hello'],
});
expect(result).toEqual({ value: 'hello' });
});
test('a backend that does not use the runtime SDK still works', async () => {
const main = await importBackendMain('noSdkFunction');
const result = await main({
Source: validSource(),
backendFunctionArgs: [],
});
expect(result).toEqual({ ok: true });
});
test('rejects an invalid context', async () => {
const main = await importBackendMain('getRuntimeUsers');
await expect(
main({
Source: {
initiator: { orgId: 'org-1' },
runAsUser: { id: 'run-as-id', orgId: 'org-1' },
},
backendFunctionArgs: ['integration-test'],
}),
).rejects.toThrow(/is missing the required "id" property/);
});
});