-
Notifications
You must be signed in to change notification settings - Fork 542
/
location.ts
131 lines (115 loc) · 3.5 KB
/
location.ts
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
import {
VirtualFile,
SnapManifest,
createSnapManifest,
} from '@metamask/snaps-utils';
import {
DEFAULT_SNAP_BUNDLE,
DEFAULT_SNAP_ICON,
getSnapManifest,
} from '@metamask/snaps-utils/test-utils';
import { assert } from '@metamask/utils';
import { SnapLocation } from 'src/snaps/location';
const MANIFEST_PATH = 'snap.manifest.json';
type LoopbackOptions = {
/**
* @default getSnapManifest()
*/
manifest?: SnapManifest | VirtualFile<SnapManifest>;
/**
* @default [new VirtualFile({ value: DEFAULT_SNAP_BUNDLE, path: manifest.source.location.npm.filePath }]
*/
files?: VirtualFile[];
/**
* @default false
*/
shouldAlwaysReload?: boolean;
};
// Mirror NPM and HTTP implementation
const coerceManifest = (manifest: VirtualFile<SnapManifest>) => {
manifest.result = createSnapManifest(manifest.result);
return manifest;
};
export class LoopbackLocation implements SnapLocation {
#manifest: VirtualFile<SnapManifest>;
#files: VirtualFile[];
#shouldAlwaysReload: boolean;
constructor(opts: LoopbackOptions = {}) {
const shouldAlwaysReload = opts.shouldAlwaysReload ?? false;
const manifest = coerceManifest(
opts.manifest instanceof VirtualFile
? opts.manifest
: new VirtualFile({
value: '',
result: opts.manifest ?? getSnapManifest(),
path: 'snap.manifest.json',
}),
);
let files;
if (opts.files === undefined) {
files = [
new VirtualFile({
value: DEFAULT_SNAP_BUNDLE,
path: manifest.result.source.location.npm.filePath,
}),
];
if (manifest.result.source.location.npm.iconPath !== undefined) {
files.push(
new VirtualFile({
value: DEFAULT_SNAP_ICON,
path: manifest.result.source.location.npm.iconPath,
}),
);
}
} else {
files = opts.files;
assert(
files.find(
(file) => file.path === manifest.result.source.location.npm.filePath,
) !== undefined,
'Source bundle not found in files.',
);
assert(
manifest.result.source.location.npm.iconPath === undefined ||
files.find(
(file) =>
file.path === manifest.result.source.location.npm.iconPath,
) !== undefined,
'Icon not found in files.',
);
}
assert(
!files.find((file) => file.path === MANIFEST_PATH),
'Manifest in fetch() files',
);
assert(manifest.path === MANIFEST_PATH, 'Manifest has wrong path.');
this.#shouldAlwaysReload = shouldAlwaysReload;
this.#manifest = manifest;
this.#files = files;
}
/* eslint-disable @typescript-eslint/require-await */
manifest = jest.fn(async () => this.#manifest);
fetch = jest.fn(async (path: string) => {
const file = this.#files.find((candidate) => candidate.path === path);
assert(
file !== undefined,
`Tried to access file "${path}" not found in loopback location mock.\nFile list:\n${this.#files
.map((candidate) => `\t-> ${candidate.path}`)
.join('\n')}`,
);
return file;
});
/* eslint-enable @typescript-eslint/require-await */
get shouldAlwaysReload() {
return this._shouldAlwaysReload();
}
_shouldAlwaysReload = jest.fn(() => this.#shouldAlwaysReload);
}
export const loopbackDetect = (
opts: LoopbackOptions | LoopbackLocation = {},
) => {
if (opts instanceof LoopbackLocation) {
return jest.fn(() => opts);
}
return jest.fn(() => new LoopbackLocation(opts));
};