-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplugins.test.ts
More file actions
162 lines (151 loc) · 5.76 KB
/
Copy pathplugins.test.ts
File metadata and controls
162 lines (151 loc) · 5.76 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { expect, it } from "vitest";
import { tryResolveAssetUrl, tryResolveLatestJson } from "./plugins.js";
import { getLatestReleaseInfo } from "./utils/github.js";
function resolveAsset(url: string) {
return tryResolveAssetUrl(new URL(url));
}
it("tryResolveAssetUrl", () => {
// allowed repo — should cache
expect(
resolveAsset(
"https://plugins.dprint.dev/dprint/dprint-plugin-prettier/0.67.0/asset/dprint-plugin-prettier-x86_64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/dprint/dprint-plugin-prettier/releases/download/0.67.0/dprint-plugin-prettier-x86_64-apple-darwin.zip",
shouldCache: true,
});
// latest tag — still resolves but not cached
expect(
resolveAsset(
"https://plugins.dprint.dev/dprint/dprint-plugin-prettier/latest/asset/dprint-plugin-prettier-x86_64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/dprint/dprint-plugin-prettier/releases/download/latest/dprint-plugin-prettier-x86_64-apple-darwin.zip",
shouldCache: true,
});
// different repo in dprint org — should cache
expect(
resolveAsset("https://plugins.dprint.dev/dprint/dprint-plugin-exec/0.5.0/asset/some-binary.zip"),
).toEqual({
githubUrl: "https://github.com/dprint/dprint-plugin-exec/releases/download/0.5.0/some-binary.zip",
shouldCache: true,
});
// approved community repo — should cache
expect(
resolveAsset(
"https://plugins.dprint.dev/drluckyspin/dprint-plugin-swift/v0.1.0/asset/dprint-plugin-swift-aarch64-apple-darwin.zip",
),
).toEqual({
githubUrl:
"https://github.com/drluckyspin/dprint-plugin-swift/releases/download/v0.1.0/dprint-plugin-swift-aarch64-apple-darwin.zip",
shouldCache: true,
});
// org not on allow list — resolves but should not cache (redirect)
expect(
resolveAsset("https://plugins.dprint.dev/someone/some-repo/0.1.0/asset/file.zip"),
).toEqual({
githubUrl: "https://github.com/someone/some-repo/releases/download/0.1.0/file.zip",
shouldCache: false,
});
// non-matching URL
expect(
resolveAsset("https://plugins.dprint.dev/dprint/dprint-plugin-prettier/0.67.0/file.zip"),
).toEqual(undefined);
});
it("tryResolveUserLatestJson", async () => {
// non-matching
expect(
await tryResolveLatestJson(
new URL("https://plugins.dprint.dev/dsherret/latest.json"),
),
).toEqual(undefined);
expect(
await tryResolveLatestJson(
new URL("https://plugins.dprint.dev/dsherret/non-existent/latest.json"),
),
).toEqual(404);
// dprint repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/dprint/typescript/latest.json");
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-typescript");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/typescript-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/typescript" },
});
}
// dprint repo full name
{
const result = await getValidResultForUrl(
"https://plugins.dprint.dev/dprint/dprint-plugin-typescript/latest.json",
);
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-typescript");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/typescript-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/typescript" },
});
}
// non-dprint repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/malobre/vue/latest.json");
const releaseInfo = await getLatestReleaseInfo("malobre", "dprint-plugin-vue");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/malobre/vue-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
// non-dprint repo full name
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/malobre/dprint-plugin-vue/latest.json");
const releaseInfo = await getLatestReleaseInfo("malobre", "dprint-plugin-vue");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/malobre/vue-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
});
}
// process plugin repo
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/dprint/prettier/latest.json");
const releaseInfo = await getLatestReleaseInfo("dprint", "dprint-plugin-prettier");
expect(releaseInfo?.checksum?.length).toEqual(64);
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/prettier-${releaseInfo!.tagName}.json`,
version: releaseInfo!.tagName,
checksum: releaseInfo!.checksum,
npm: { name: "@dprint/prettier" },
});
}
// community repo published to npm
{
const result = await getValidResultForUrl("https://plugins.dprint.dev/g-plane/malva/latest.json");
const releaseInfo = await getLatestReleaseInfo("g-plane", "malva");
expect(result).toEqual({
schemaVersion: 1,
url: `https://plugins.dprint.dev/g-plane/malva-${releaseInfo!.tagName}.wasm`,
version: releaseInfo!.tagName.replace(/^v/, ""),
checksum: releaseInfo!.checksum,
npm: { name: "dprint-plugin-malva" },
});
}
async function getValidResultForUrl(url: string) {
const result = await tryResolveLatestJson(new URL(url))!;
if (result == null || result === 404) {
throw new Error("Expected result.");
}
return result;
}
});