Skip to content

Commit

Permalink
feat: delete canister snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker committed Nov 21, 2024
1 parent 31fbfce commit 87acd1d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,60 @@ describe("ICManagementCanister", () => {
await expect(call).rejects.toThrow(error);
});
});

describe("deleteCanisterSnapshot", () => {
it("should call delete_canister_snapshot with Uint8Array snapshotId", async () => {
const service = mock<IcManagementService>();
service.delete_canister_snapshot.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);

const params = {
canisterId: mockCanisterId,
snapshotId: Uint8Array.from([1, 2, 3, 4]),
};

await icManagement.deleteCanisterSnapshot(params);

expect(service.delete_canister_snapshot).toHaveBeenCalledWith({
canister_id: params.canisterId,
snapshot_id: params.snapshotId,
});
});

it("should call delete_canister_snapshot with string snapshotId", async () => {
const service = mock<IcManagementService>();
service.delete_canister_snapshot.mockResolvedValue(undefined);

const icManagement = await createICManagement(service);

const params = {
canisterId: mockCanisterId,
snapshotId: "000000000000000201010000000000000001",
};

await icManagement.deleteCanisterSnapshot(params);

expect(service.delete_canister_snapshot).toHaveBeenCalledWith({
canister_id: params.canisterId,
snapshot_id: decodeSnapshotId(params.snapshotId),
});
});

it("should throw an error if delete_canister_snapshot fails", async () => {
const error = new Error("Test error");
const service = mock<IcManagementService>();
service.delete_canister_snapshot.mockRejectedValue(error);

const icManagement = await createICManagement(service);

const call = () =>
icManagement.deleteCanisterSnapshot({
canisterId: mockCanisterId,
snapshotId: Uint8Array.from([1, 2, 3, 4]),
});

await expect(call).rejects.toThrow(error);
});
});
});
31 changes: 31 additions & 0 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,35 @@ export class ICManagementCanister {
),
});
};

/**
* Deletes a specific snapshot of a canister.
*
* @link https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-delete_canister_snapshot
*
* @param {Object} params - Parameters for the deletion operation.
* @param {Principal} params.canisterId - The ID of the canister for which the snapshot will be deleted.
* @param {snapshot_id} params.snapshotId - The ID of the snapshot to delete.
*
* @returns {Promise<void>} A promise that resolves when the snapshot is successfully deleted.
*
* @throws {Error} If the deletion operation fails.
*/
deleteCanisterSnapshot = async ({
canisterId,
snapshotId,
}: {
canisterId: Principal;
snapshotId: SnapshotIdText | snapshot_id;
}): Promise<void> => {
const { delete_canister_snapshot } = this.service;

await delete_canister_snapshot({
canister_id: canisterId,
snapshot_id:
typeof snapshotId === "string"
? decodeSnapshotId(snapshotId)
: snapshotId,
});
};
}

0 comments on commit 87acd1d

Please sign in to comment.