-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I7471b54981a3f3ffbe2f05909536532e5fa15d91 Signed-off-by: Maciej Sopylo <[email protected]>
- Loading branch information
Showing
3 changed files
with
272 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
const axios = require("axios"); | ||
jest.mock("axios"); | ||
axios.create.mockReturnValue(axios); | ||
const api = require("./api.js"); | ||
|
||
const MOCK_DATA = { | ||
releases: [ | ||
{ | ||
name: "edge", | ||
devices: [ | ||
{ | ||
name: "somedevice", | ||
interfaces: [ | ||
{ name: "phosh" }, | ||
{ | ||
name: "plasma-mobile", | ||
images: [ | ||
{ | ||
timestamp: 0, | ||
url: "someurl", | ||
sha256: "sha256-first" | ||
}, | ||
{ | ||
timestamp: 0, | ||
url: "someurl2", | ||
sha256: "sha256-other" | ||
} | ||
] | ||
}, | ||
{ name: "sxmo-de-sway" }, | ||
{ name: "other" } | ||
] | ||
} | ||
] | ||
} | ||
] | ||
}; | ||
|
||
describe("postmarketos api", () => { | ||
beforeEach(() => { | ||
axios.get.mockResolvedValueOnce({ | ||
data: MOCK_DATA | ||
}); | ||
}); | ||
|
||
describe("getInterfaces()", () => { | ||
it("should resolve interfaces", async () => { | ||
const result = await api.getInterfaces("somedevice"); | ||
expect(result).toContainEqual({ | ||
value: "phosh", | ||
label: "Phosh" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "plasma-mobile", | ||
label: "Plasma Mobile" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "sxmo-de-sway", | ||
label: "SXMO Sway" | ||
}); | ||
expect(result).toContainEqual({ | ||
value: "other", | ||
label: "other" | ||
}); | ||
}); | ||
|
||
it("should reject on errors", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getInterfaces("nonexistent"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
|
||
describe("getImages()", () => { | ||
it("should resolve images", async () => { | ||
const result = await api.getImages("edge", "plasma-mobile", "somedevice"); | ||
expect(result).toContainEqual({ | ||
url: "someurl", | ||
checksum: { | ||
sum: "sha256-first", | ||
algorithm: "sha256" | ||
} | ||
}); | ||
}); | ||
|
||
it("should throw on 404", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getImages("non", "existent", "stuff"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
|
||
describe("getReleases()", () => { | ||
it("should resolve releases", async () => { | ||
const result = await api.getReleases("somedevice"); | ||
expect(result).toEqual(["edge"]); | ||
}); | ||
|
||
it("should throw on 404", async () => { | ||
axios.get.mockReset(); | ||
axios.get.mockRejectedValueOnce({ | ||
response: { | ||
status: 404 | ||
} | ||
}); | ||
|
||
const test = () => api.getReleases("nonexistent"); | ||
await expect(test).rejects.toThrow("404"); | ||
|
||
axios.get.mockRejectedValueOnce(new Error("other")); | ||
await expect(test).rejects.toThrow("other"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
const mainEvent = { emit: jest.fn() }; | ||
beforeEach(() => mainEvent.emit.mockReset()); | ||
const fs = require("fs/promises"); | ||
jest.mock("fs/promises", () => ({ | ||
rename: jest.fn() | ||
})); | ||
const log = require("../../../lib/log.js"); | ||
jest.mock("../../../lib/log.js"); | ||
const api = require("./api.js"); | ||
jest.mock("./api.js"); | ||
const path = require("path"); | ||
|
||
const cachePath = "surprise.xz/inthename"; | ||
|
||
const pmosPlugin = new (require("./plugin.js"))( | ||
{ | ||
settings: { | ||
release: "somerelease", | ||
interface: "someinterface" | ||
}, | ||
config: { | ||
codename: "somecodename" | ||
} | ||
}, | ||
cachePath, | ||
mainEvent, | ||
log | ||
); | ||
|
||
describe("postmarketos plugin", () => { | ||
describe("action__download()", () => { | ||
it("should download images", async () => { | ||
const files = [{ url: "http://somewebsite.com/somefilename.zip" }]; | ||
api.getImages.mockResolvedValueOnce(files); | ||
|
||
const ret = await pmosPlugin.action__download(); | ||
expect(api.getImages).toHaveBeenCalledWith( | ||
"somerelease", | ||
"someinterface", | ||
"somecodename" | ||
); | ||
expect(ret[0]).toBeDefined(); | ||
expect(ret[0].actions).toContainEqual({ | ||
"core:download": { | ||
group: "postmarketOS", | ||
files | ||
} | ||
}); | ||
expect(ret[0].actions).toContainEqual({ | ||
"postmarketos:rename_unpacked_files": { | ||
group: "postmarketOS", | ||
files | ||
} | ||
}); | ||
expect(ret[0].actions[1]["core:unpack"].files).toContainEqual({ | ||
url: files[0].url, | ||
archive: "somefilename.zip" | ||
}); | ||
}); | ||
}); | ||
|
||
describe("action__rename_unpacked_files()", () => { | ||
it("should rename the files", async () => { | ||
jest.spyOn(pmosPlugin.event, "emit").mockReturnValue(); | ||
|
||
const group = "group"; | ||
const basepath = path.join(cachePath, "somecodename", group); | ||
const files = [ | ||
{ | ||
url: "https://asdf.io/somethingelse.img.xz" | ||
}, | ||
{ | ||
url: "https://asdf.io/somethingelse-boot.img.xz" | ||
} | ||
]; | ||
|
||
await pmosPlugin.action__rename_unpacked_files({ group, files }); | ||
expect(pmosPlugin.event.emit).toHaveBeenCalledTimes(3); | ||
expect(fs.rename).toHaveBeenCalledWith( | ||
path.join(basepath, "somethingelse.img"), | ||
path.join(basepath, "rootfs.img") | ||
); | ||
expect(fs.rename).toHaveBeenCalledWith( | ||
path.join(basepath, "somethingelse-boot.img"), | ||
path.join(basepath, "boot.img") | ||
); | ||
}); | ||
}); | ||
|
||
describe("remote_values__interfaces()", () => { | ||
it("should get interfaces", async () => { | ||
await pmosPlugin.remote_values__interfaces(); | ||
|
||
expect(api.getInterfaces).toHaveBeenCalledWith("somecodename"); | ||
}); | ||
}); | ||
|
||
describe("remote_values__releases()", () => { | ||
it("should get releases", async () => { | ||
api.getReleases.mockResolvedValueOnce(["a", "b"]); | ||
const result = await pmosPlugin.remote_values__releases(); | ||
expect(api.getReleases).toHaveBeenCalledWith("somecodename"); | ||
expect(result).toContainEqual({ | ||
label: "a", | ||
value: "a" | ||
}); | ||
expect(result).toContainEqual({ | ||
label: "b", | ||
value: "b" | ||
}); | ||
}); | ||
}); | ||
}); |