Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: utils to mapSnapshotId #764

Merged
merged 3 commits into from
Nov 21, 2024
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
8 changes: 2 additions & 6 deletions packages/ic-management/src/ic-management.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import type {
CanisterStatusResponse,
FetchCanisterLogsResponse,
} from "./types/ic-management.responses";
import { decodeSnapshotId } from "./utils/ic-management.utils";
import { mapSnapshotId } from "./utils/ic-management.utils";

export class ICManagementCanister {
private constructor(private readonly service: IcManagementService) {
Expand Down Expand Up @@ -384,11 +384,7 @@ export class ICManagementCanister {
return take_canister_snapshot({
canister_id: canisterId,
replace_snapshot: toNullable(
nonNullish(snapshotId)
? typeof snapshotId === "string"
? decodeSnapshotId(snapshotId)
: snapshotId
: undefined,
nonNullish(snapshotId) ? mapSnapshotId(snapshotId) : undefined,
),
});
};
Expand Down
19 changes: 18 additions & 1 deletion packages/ic-management/src/utils/ic-management.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { mockCanisterId } from "../ic-management.mock";
import { decodeSnapshotId, encodeSnapshotId } from "./ic-management.utils";
import {
decodeSnapshotId,
encodeSnapshotId,
mapSnapshotId,
} from "./ic-management.utils";

describe("ic-management.utils", () => {
const mockLocalSubnetId = [0, 0, 0, 0, 0, 0, 0, 1];
Expand Down Expand Up @@ -29,4 +33,17 @@ describe("ic-management.utils", () => {

expect(decoded).toEqual(mockSnapshotId);
});

describe("mapSnapshotId", () => {
it("should map a Uint8Array snapshot ID without changes", () => {
const result = mapSnapshotId(mockSnapshotId);

expect(result).toEqual(mockSnapshotId);
});

it("should map a string snapshot ID by decoding it", () => {
const result = mapSnapshotId(snapshotIdHex);
expect(result).toEqual(mockSnapshotId);
});
});
});
14 changes: 14 additions & 0 deletions packages/ic-management/src/utils/ic-management.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@ export const encodeSnapshotId = (snapshotId: snapshot_id): SnapshotIdText =>
*/
export const decodeSnapshotId = (snapshotId: SnapshotIdText): snapshot_id =>
hexStringToUint8Array(snapshotId);

/**
* Maps a snapshot ID to the appropriate format for the IC interface.
*
* @param {SnapshotIdText | snapshot_id} snapshotId - The snapshot ID to map.
* It can either be a `string` (SnapshotIdText) or a `Uint8Array | number[]` (snapshot_id).
* If a `string` is provided, it is decoded into a `Uint8Array` using `decodeSnapshotId`.
*
* @returns {Uint8Array | number[]} The mapped snapshot ID.
*/
export const mapSnapshotId = (
snapshotId: SnapshotIdText | snapshot_id,
): snapshot_id =>
typeof snapshotId === "string" ? decodeSnapshotId(snapshotId) : snapshotId;
Loading