Skip to content

Commit

Permalink
feat: utils to mapSnapshotId (#764)
Browse files Browse the repository at this point in the history
# Motivation

As suggested by @AntonioVentilii in
#759 (comment) we can
create a function to map the snapshot id because we will reuse similar
pattern in #763.

# Changes

- Extract mapper in utils
- Use mapper in existing implementation

---------

Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker authored Nov 21, 2024
1 parent a22db87 commit 80badcf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
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;

0 comments on commit 80badcf

Please sign in to comment.