mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-07 09:00:47 +00:00
test: enforce async extension API contract (#1452)
This commit is contained in:
13 files changed
+121
-77
No files matched your search
@@ -838,7 +838,9 @@ func TestDownloader_Extension_BlobPauseContinueKeepsSource(t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
downloader := NewDownloader(nil)
|
||||
downloader := NewDownloader(&DownloaderConfig{
|
||||
StorageDir: t.TempDir(),
|
||||
})
|
||||
downloader.cfg.RefreshInterval = 50
|
||||
if err := downloader.Setup(); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -1479,7 +1481,7 @@ func TestDownloader_ExtensionRuntimeWebViewInjected(t *testing.T) {
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
value, err := runtime.Eval(`({
|
||||
value, err := runtime.Eval(`(async () => ({
|
||||
hasRuntime: !!gopeed.runtime,
|
||||
hasBlob: !!(gopeed.runtime && gopeed.runtime.blob),
|
||||
hasCreateObjectURL: typeof gopeed.runtime.blob.createObjectURL,
|
||||
@@ -1487,8 +1489,8 @@ func TestDownloader_ExtensionRuntimeWebViewInjected(t *testing.T) {
|
||||
hasWebView: !!(gopeed.runtime && gopeed.runtime.webview),
|
||||
hasOpen: typeof gopeed.runtime.webview.open,
|
||||
hasWebViewIsAvailable: typeof gopeed.runtime.webview.isAvailable,
|
||||
webViewAvailable: gopeed.runtime.webview.isAvailable()
|
||||
})`)
|
||||
webViewAvailable: await gopeed.runtime.webview.isAvailable()
|
||||
}))()`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -1530,9 +1532,9 @@ func TestDownloader_ExtensionRuntimeBlobRejectsReadableStreamSource(t *testing.T
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
value, err := runtime.Eval(`(() => {
|
||||
value, err := runtime.Eval(`(async () => {
|
||||
try {
|
||||
gopeed.runtime.blob.createObjectURL(new ReadableStream({
|
||||
await gopeed.runtime.blob.createObjectURL(new ReadableStream({
|
||||
start(controller) {
|
||||
controller.close();
|
||||
},
|
||||
@@ -1573,7 +1575,7 @@ func TestDownloader_ExtensionRuntimeWebViewAvailabilityFromProvider(t *testing.T
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
value, err := runtime.Eval(`gopeed.runtime.webview.isAvailable()`)
|
||||
value, err := runtime.Eval(`(async () => await gopeed.runtime.webview.isAvailable())()`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
+3
-3
@@ -1,7 +1,7 @@
|
||||
gopeed.events.onResolve(async function (ctx) {
|
||||
ctx.req.setLabels({ replaced: "true", removed: "true" });
|
||||
ctx.req.putLabel("modified", "true");
|
||||
ctx.req.delLabel("removed");
|
||||
await ctx.req.setLabels({ replaced: "true", removed: "true" });
|
||||
await ctx.req.putLabel("modified", "true");
|
||||
await ctx.req.delLabel("removed");
|
||||
ctx.res = {
|
||||
name: "test",
|
||||
files: Array(2).fill(true).map((_, i) => ({
|
||||
|
||||
+4
-4
@@ -3,7 +3,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
|
||||
if (ctx.req.url.endsWith("/blob")) {
|
||||
const blob = new Blob(["hello world"], {type: "text/plain"});
|
||||
const url = gopeed.runtime.blob.createObjectURL(blob);
|
||||
const url = await gopeed.runtime.blob.createObjectURL(blob);
|
||||
ctx.res = {
|
||||
name: "blob-blob",
|
||||
files: [
|
||||
@@ -42,7 +42,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
}
|
||||
});
|
||||
|
||||
const url = gopeed.runtime.blob.createObjectURL(open, { size: payload.byteLength, range: true });
|
||||
const url = await gopeed.runtime.blob.createObjectURL(open, { size: payload.byteLength, range: true });
|
||||
|
||||
ctx.res = {
|
||||
name: "blob-opener-range",
|
||||
@@ -61,7 +61,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
}
|
||||
|
||||
if (ctx.req.url.endsWith("/opener-unknown")) {
|
||||
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||
const url = await gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||
async start(controller) {
|
||||
if (offset > 0) {
|
||||
controller.close();
|
||||
@@ -88,7 +88,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => new ReadableStream({
|
||||
const url = await gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => new ReadableStream({
|
||||
start(controller) {
|
||||
const payload = encoder.encode("line 1\nline 2\n");
|
||||
controller.enqueue(payload.slice(offset, end >= 0 ? end + 1 : undefined));
|
||||
|
||||
+15
-15
@@ -39,7 +39,7 @@ function fastPayloadStream(offset = 0, end = -1) {
|
||||
});
|
||||
}
|
||||
|
||||
function createBlobSourceURL(delayed) {
|
||||
async function createBlobSourceURL(delayed) {
|
||||
const blob = new Blob([payloadText], { type: 'application/octet-stream' });
|
||||
const originalSlice = blob.slice.bind(blob);
|
||||
blob.slice = function (offset = 0, end) {
|
||||
@@ -53,20 +53,20 @@ function createBlobSourceURL(delayed) {
|
||||
},
|
||||
};
|
||||
};
|
||||
return gopeed.runtime.blob.createObjectURL(blob);
|
||||
return await gopeed.runtime.blob.createObjectURL(blob);
|
||||
}
|
||||
|
||||
function createOpenerSourceURL(delayed) {
|
||||
async function createOpenerSourceURL(delayed) {
|
||||
const open = async () => delayed ? delayedPayloadStream(0, -1) : fastPayloadStream(0, -1);
|
||||
return gopeed.runtime.blob.createObjectURL(open, {
|
||||
return await gopeed.runtime.blob.createObjectURL(open, {
|
||||
size: payloadSize,
|
||||
contentType: 'application/octet-stream',
|
||||
});
|
||||
}
|
||||
|
||||
function createRangeSourceURL(delayed) {
|
||||
async function createRangeSourceURL(delayed) {
|
||||
const open = async ({ offset = 0, end = -1 }) => delayed ? delayedPayloadStream(offset, end) : fastPayloadStream(offset, end);
|
||||
return gopeed.runtime.blob.createObjectURL(open, {
|
||||
return await gopeed.runtime.blob.createObjectURL(open, {
|
||||
size: payloadSize,
|
||||
range: true,
|
||||
contentType: 'application/octet-stream',
|
||||
@@ -86,14 +86,14 @@ function modeFromURL(url) {
|
||||
return '';
|
||||
}
|
||||
|
||||
function createPayloadURL(mode, delayed) {
|
||||
async function createPayloadURL(mode, delayed) {
|
||||
switch (mode) {
|
||||
case 'blob':
|
||||
return createBlobSourceURL(delayed);
|
||||
return await createBlobSourceURL(delayed);
|
||||
case 'opener':
|
||||
return createOpenerSourceURL(delayed);
|
||||
return await createOpenerSourceURL(delayed);
|
||||
case 'range':
|
||||
return createRangeSourceURL(delayed);
|
||||
return await createRangeSourceURL(delayed);
|
||||
default:
|
||||
throw new Error('unsupported pause rebuild mode: ' + mode);
|
||||
}
|
||||
@@ -113,7 +113,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
name: 'pause-rebuild-' + mode + '.bin',
|
||||
size: payloadSize,
|
||||
req: {
|
||||
url: createPayloadURL(mode, true),
|
||||
url: await createPayloadURL(mode, true),
|
||||
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
||||
labels: {
|
||||
mode: 'pause-rebuild',
|
||||
@@ -132,8 +132,8 @@ gopeed.events.onError(async function (ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.task.setUrl(createPayloadURL(source, false));
|
||||
req.putLabel('started', 'true');
|
||||
req.putLabel('rebuilt', 'true');
|
||||
ctx.task.continue();
|
||||
await ctx.task.setUrl(await createPayloadURL(source, false));
|
||||
await req.putLabel('started', 'true');
|
||||
await req.putLabel('rebuilt', 'true');
|
||||
await ctx.task.continue();
|
||||
});
|
||||
+4
-4
@@ -4,7 +4,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||
const url = await gopeed.runtime.blob.createObjectURL(async ({ offset = 0 }) => new ReadableStream({
|
||||
async start(controller) {
|
||||
if (offset > 0) {
|
||||
controller.close();
|
||||
@@ -40,7 +40,7 @@ gopeed.events.onError(async function (ctx) {
|
||||
return;
|
||||
}
|
||||
|
||||
req.putLabel("recovered", "true");
|
||||
ctx.task.setUrl(gopeed.runtime.blob.createObjectURL(new Blob(["ok\n"], { type: "text/plain" })));
|
||||
ctx.task.continue();
|
||||
await req.putLabel("recovered", "true");
|
||||
await ctx.task.setUrl(await gopeed.runtime.blob.createObjectURL(new Blob(["ok\n"], { type: "text/plain" })));
|
||||
await ctx.task.continue();
|
||||
});
|
||||
+8
-8
@@ -1,5 +1,5 @@
|
||||
function createRangeBlobUrl(target) {
|
||||
return gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => {
|
||||
async function createRangeBlobUrl(target) {
|
||||
return await gopeed.runtime.blob.createObjectURL(async ({ offset = 0, end = -1 }) => {
|
||||
const headers = {};
|
||||
if (offset > 0 || end >= 0) {
|
||||
headers.Range = `bytes=${offset}-${end >= 0 ? end : ''}`;
|
||||
@@ -25,7 +25,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
name: 'restart.bin',
|
||||
size: 262144,
|
||||
req: {
|
||||
url: createRangeBlobUrl(ctx.req.rawUrl || ctx.req.url),
|
||||
url: await createRangeBlobUrl(ctx.req.rawUrl || ctx.req.url),
|
||||
rawUrl: ctx.req.rawUrl || ctx.req.url,
|
||||
labels: {
|
||||
mode: 'restart',
|
||||
@@ -46,12 +46,12 @@ gopeed.events.onError(async function (ctx) {
|
||||
}
|
||||
|
||||
try {
|
||||
ctx.task.setUrl(createRangeBlobUrl(req.rawUrl));
|
||||
req.putLabel('started', 'true');
|
||||
req.putLabel('rebuilt', 'true');
|
||||
ctx.task.continue();
|
||||
await ctx.task.setUrl(await createRangeBlobUrl(req.rawUrl));
|
||||
await req.putLabel('started', 'true');
|
||||
await req.putLabel('rebuilt', 'true');
|
||||
await ctx.task.continue();
|
||||
} catch (error) {
|
||||
req.putLabel('rebuildError', String(error));
|
||||
await req.putLabel('rebuildError', String(error));
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
@@ -124,7 +124,7 @@ function createMultiplexStreams(url) {
|
||||
gopeed.events.onResolve(async function (ctx) {
|
||||
if (ctx.req.url.includes("/single-fetch-stream")) {
|
||||
const payloadSize = Number(new URL(ctx.req.url).searchParams.get("size")) || 0;
|
||||
const url = gopeed.runtime.blob.createObjectURL(async () => createFetchDrainedStream(sourceURL(ctx.req.url)), {
|
||||
const url = await gopeed.runtime.blob.createObjectURL(async () => createFetchDrainedStream(sourceURL(ctx.req.url)), {
|
||||
size: payloadSize,
|
||||
});
|
||||
ctx.res = {
|
||||
@@ -142,7 +142,7 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
|
||||
if (ctx.req.url.includes("/range-fetch-stream")) {
|
||||
const payloadSize = Number(new URL(ctx.req.url).searchParams.get("size")) || 0;
|
||||
const url = gopeed.runtime.blob.createObjectURL(
|
||||
const url = await gopeed.runtime.blob.createObjectURL(
|
||||
async ({ offset = 0, end = -1 }) => createFetchDrainedRangeStream(sourceURL(ctx.req.url), offset, end),
|
||||
{
|
||||
size: payloadSize,
|
||||
@@ -173,10 +173,10 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
}
|
||||
return streams;
|
||||
};
|
||||
const videoUrl = gopeed.runtime.blob.createObjectURL(async () => openStreams().video, {
|
||||
const videoUrl = await gopeed.runtime.blob.createObjectURL(async () => openStreams().video, {
|
||||
size: videoChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||
});
|
||||
const audioUrl = gopeed.runtime.blob.createObjectURL(async () => openStreams().audio, {
|
||||
const audioUrl = await gopeed.runtime.blob.createObjectURL(async () => openStreams().audio, {
|
||||
size: audioChunks.reduce((sum, chunk) => sum + byteLength(chunk), 0),
|
||||
});
|
||||
ctx.res = {
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
gopeed.events.onError(async function (ctx) {
|
||||
gopeed.logger.info("url", ctx.task.meta.req.url);
|
||||
gopeed.logger.info("error", ctx.error);
|
||||
ctx.task.setUrl("https://github.com");
|
||||
ctx.task.continue();
|
||||
await ctx.task.setUrl("https://github.com");
|
||||
await ctx.task.continue();
|
||||
});
|
||||
+2
-2
@@ -1,5 +1,5 @@
|
||||
gopeed.events.onStart(async function (ctx) {
|
||||
gopeed.logger.info("url", ctx.task.meta.req.url);
|
||||
ctx.task.setUrl("https://github.com");
|
||||
ctx.task.meta.req.putLabel('modified', 'true');
|
||||
await ctx.task.setUrl("https://github.com");
|
||||
await ctx.task.meta.req.putLabel('modified', 'true');
|
||||
});
|
||||
+13
-13
@@ -2,36 +2,36 @@ gopeed.events.onResolve(async function (ctx) {
|
||||
const key = "key"
|
||||
const value1 = "value1", value2 = JSON.stringify({a: 1, b: "2"})
|
||||
|
||||
if (gopeed.storage.get(key) !== null) {
|
||||
if ((await gopeed.storage.get(key)) !== null) {
|
||||
throw new Error("storage get null error")
|
||||
}
|
||||
gopeed.storage.remove(key)
|
||||
if(gopeed.storage.keys().length !== 0) {
|
||||
await gopeed.storage.remove(key)
|
||||
if((await gopeed.storage.keys()).length !== 0) {
|
||||
throw new Error("storage keys null error")
|
||||
}
|
||||
|
||||
gopeed.storage.set(key, value1)
|
||||
if (gopeed.storage.get(key) !== value1) {
|
||||
await gopeed.storage.set(key, value1)
|
||||
if ((await gopeed.storage.get(key)) !== value1) {
|
||||
throw new Error("storage put1 error")
|
||||
}
|
||||
|
||||
gopeed.storage.set(key, value2)
|
||||
if (gopeed.storage.get(key) !== value2) {
|
||||
await gopeed.storage.set(key, value2)
|
||||
if ((await gopeed.storage.get(key)) !== value2) {
|
||||
throw new Error("storage put2 error")
|
||||
}
|
||||
|
||||
if(gopeed.storage.keys().length !== 1) {
|
||||
if((await gopeed.storage.keys()).length !== 1) {
|
||||
throw new Error("storage keys error")
|
||||
}
|
||||
|
||||
gopeed.storage.remove(key)
|
||||
if (gopeed.storage.get(key) !== null) {
|
||||
await gopeed.storage.remove(key)
|
||||
if ((await gopeed.storage.get(key)) !== null) {
|
||||
throw new Error("storage delete error")
|
||||
}
|
||||
|
||||
gopeed.storage.set(key, value1)
|
||||
gopeed.storage.clear()
|
||||
if (gopeed.storage.get(key) !== null) {
|
||||
await gopeed.storage.set(key, value1)
|
||||
await gopeed.storage.clear()
|
||||
if ((await gopeed.storage.get(key)) !== null) {
|
||||
throw new Error("storage clear error")
|
||||
}
|
||||
|
||||
|
||||
@@ -924,7 +924,7 @@ func TestBuildServerPropagatesWebViewProvider(t *testing.T) {
|
||||
}
|
||||
defer runtime.Close()
|
||||
|
||||
value, err := runtime.Eval("gopeed.runtime.webview.isAvailable()")
|
||||
value, err := runtime.Eval("(async () => await gopeed.runtime.webview.isAvailable())()")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user