Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/installPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ async function resolveDepsManifest(deps) {
const manifests = await resolveDepsManifest(
remoteDependency.dependencies,
);
resolved.push(manifests);
resolved.push(...manifests);
}

resolved.push(remoteDependency);
Expand Down
147 changes: 147 additions & 0 deletions tests/unit/installPluginDependencies.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import JSZip from "jszip";
import { expect, it, vi } from "vitest";
import formatDownloadProgress from "../../src/lib/downloadProgress.js";
import { loadSourceModule } from "../helpers/loadSourceModule";

it("installs a dependency that has its own dependency", async () => {
const fixture = await setup();
await fixture.install("file:///parent.zip");
expect(fixture.confirm.mock.calls[0][1]).toMatch(
/<br \/><br \/>Nested, Direct$/,
);
expect(fixture.downloads).toEqual(["nested", "direct"]);
expect(fixture.loaded.mock.calls).toEqual([
["nested", true],
["direct", true],
["parent", true],
]);
});

it("installs a dependency whose own dependency is already up to date", async () => {
const fixture = await setup({ installedNestedVersion: "1.0.0" });
await fixture.install("file:///parent.zip");
expect(fixture.confirm.mock.calls[0][1]).toMatch(/<br \/><br \/>Direct$/);
expect(fixture.downloads).toEqual(["direct"]);
expect(fixture.loaded.mock.calls).toEqual([
["direct", true],
["parent", true],
]);
});

async function setup({ installedNestedVersion } = {}) {
const platform = loadSourceModule(
"src/lib/platform.js",
{},
{ Bridge: { platformId: "ios" } },
).default;
const manifests = {
direct: {
id: "direct",
name: "Direct",
main: "main.js",
version: "1.0.0",
price: 0,
dependencies: ["nested"],
},
nested: {
id: "nested",
name: "Nested",
main: "main.js",
version: "1.0.0",
price: 0,
},
};
const pluginZip = async (manifest) => {
const zip = new JSZip();
zip.file("plugin.json", JSON.stringify(manifest));
zip.file("main.js", "");
return zip.generateAsync({ type: "uint8array" });
};
const files = new Map([
["file:///plugins", null],
[
"file:///parent.zip",
await pluginZip({
id: "parent",
main: "main.js",
dependencies: ["direct"],
}),
],
]);
if (installedNestedVersion) {
files.set("file:///plugins/nested", null);
files.set(
"file:///plugins/nested/plugin.json",
JSON.stringify({ id: "nested", version: installedNestedVersion }),
);
}
const downloads = [];
const join = (...parts) =>
parts.reduce(
(parent, child) =>
`${parent.replace(/\/$/, "")}/${String(child).replace(/^\/+/, "")}`,
);
const fsOperation = (...parts) => {
const url = join(...parts);
return {
exists: async () => files.has(url),
readFile: async (encoding) => {
const api = "https://fixture.invalid/plugin/";
if (url.startsWith(`${api}download/`)) {
const id = url.slice(`${api}download/`.length).split("?")[0];
downloads.push(id);
if (!manifests[id]) throw new Error(`404 ${url}`);
return pluginZip(manifests[id]);
}
if (url.startsWith(api)) return manifests[url.slice(api.length)];
if (!files.has(url)) throw new Error(`Missing fixture: ${url}`);
const data = files.get(url);
return encoding === "json" ? JSON.parse(data) : data;
},
createDirectory: async (name) => files.set(join(url, name), null),
createFile: async (name) => files.set(join(url, name), ""),
writeFile: async (data) => files.set(url, data),
delete: async () => files.delete(url),
lsDir: async () => [],
};
};
const confirm = vi.fn(async () => true);
const loaded = vi.fn();
const install = loadSourceModule(
"src/lib/installPlugin.js",
{
fileSystem: fsOperation,
"dialogs/alert": vi.fn(),
"dialogs/confirm": confirm,
"dialogs/loader": {
create: () => ({ show() {}, setMessage() {}, destroy() {} }),
},
"handlers/purchase": vi.fn(),
jszip: JSZip,
"utils/helpers": {},
"utils/Url": { join },
"utils/version": { isVersionGreater: (a, b) => a > b },
"./config": { API_BASE: "https://fixture.invalid" },
"./downloadProgress": formatDownloadProgress,
"./installState": {
new: async () => ({
exists: () => false,
isUpdated: async () => true,
save: async () => {},
clear: async () => {},
}),
},
"./loadPlugins": { loadPluginWithTimeout: loaded },
"./platform": platform,
},
{
PLUGIN_DIR: "file:///plugins",
DATA_STORAGE: "file:///",
device: { uuid: "fixture", version: "18.6" },
BuildInfo: { packageName: "com.foxdebug.acode" },
window: { log: vi.fn() },
strings: new Proxy({}, { get: (target, key) => key }),
},
).default;
return { install, confirm, downloads, loaded };
}