From 6bfb0a47d3e9eaf062a8ddc28d11d91472cc013b Mon Sep 17 00:00:00 2001 From: Federico Jaramillo Martinez Date: Sun, 5 Jul 2026 00:01:39 +0200 Subject: [PATCH] test(machines): cover remote mutation contract --- src/server/machines/machineService.test.ts | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/server/machines/machineService.test.ts b/src/server/machines/machineService.test.ts index 0dfe4c5..9907a8c 100644 --- a/src/server/machines/machineService.test.ts +++ b/src/server/machines/machineService.test.ts @@ -42,6 +42,56 @@ describe("MachineService", () => { await expectOwnerOnlyMachineStore(storePath); }); + it("gets, updates, and removes remote machines without exposing stored secrets", async () => { + const machine = await service.add({ + name: "Remote", + baseUrl: "https://remote.example.test", + token: "initial-secret", + headers: { "X-Pi-Web-Test": "initial" }, + }); + + expect(await service.get(machine.id)).toEqual(machine); + + const updated = await service.update(machine.id, { + name: " Updated Remote ", + baseUrl: "https://updated.example.test/", + token: "updated-secret", + headers: { "X-Pi-Web-Test": "updated" }, + }); + if (updated === undefined) throw new Error("Expected remote machine update to succeed"); + + expect(updated).toMatchObject({ + id: machine.id, + name: "Updated Remote", + kind: "remote", + baseUrl: "https://updated.example.test", + createdAt: machine.createdAt, + }); + expect(updated).not.toHaveProperty("token"); + expect(updated).not.toHaveProperty("headers"); + expect(await service.get(machine.id)).toEqual(updated); + expect(await service.list()).toEqual([expect.objectContaining({ id: "local", kind: "local" }), updated]); + + const persistedAfterUpdate: unknown = JSON.parse(await readFile(storePath, "utf8")); + expect(persistedAfterUpdate).toMatchObject({ + machines: [expect.objectContaining({ + id: machine.id, + name: "Updated Remote", + baseUrl: "https://updated.example.test", + token: "updated-secret", + headers: { "X-Pi-Web-Test": "updated" }, + })], + }); + + await expect(service.remove(machine.id)).resolves.toBe(true); + await expect(service.get(machine.id)).resolves.toBeUndefined(); + await expect(service.remove(machine.id)).resolves.toBe(false); + expect(await service.list()).toEqual([expect.objectContaining({ id: "local", kind: "local" })]); + + const persistedAfterRemove: unknown = JSON.parse(await readFile(storePath, "utf8")); + expect(persistedAfterRemove).toEqual({ machines: [] }); + }); + it.skipIf(process.platform === "win32")("tightens permissions after reading an existing machine store", async () => { await writeFile(storePath, `${JSON.stringify({ machines: [{