Skip to content

Commit

Permalink
test: datastore serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 26, 2025
1 parent e3520c1 commit 74cca96
Showing 1 changed file with 131 additions and 2 deletions.
133 changes: 131 additions & 2 deletions src/tests/specs/satellite.datastore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ import type {
} from '$declarations/satellite/satellite.did';
import { idlFactory as idlFactorSatellite } from '$declarations/satellite/satellite.factory.did';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import { fromNullable, toNullable } from '@dfinity/utils';
import { Principal } from '@dfinity/principal';
import {
arrayOfNumberToUint8Array,
assertNonNullish,
fromNullable,
toNullable,
uint8ArrayToArrayOfNumber
} from '@dfinity/utils';
import { type Actor, PocketIc } from '@hadronous/pic';
import {
JUNO_ERROR_NO_VERSION_PROVIDED,
JUNO_ERROR_VERSION_OUTDATED_OR_FUTURE
} from '@junobuild/errors';
import { fromArray, toArray } from '@junobuild/utils';
import { nanoid } from 'nanoid';
import { afterAll, beforeAll, describe, expect, inject } from 'vitest';
import { mockData } from './mocks/doc.mocks';
Expand All @@ -23,6 +31,7 @@ describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])(
({ memory }) => {
let pic: PocketIc;
let actor: Actor<SatelliteActor>;
let canisterId: Principal;

const controller = Ed25519KeyIdentity.generate();

Expand All @@ -31,14 +40,15 @@ describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])(
beforeAll(async () => {
pic = await PocketIc.create(inject('PIC_URL'));

const { actor: c } = await pic.setupCanister<SatelliteActor>({
const { actor: c, canisterId: cId } = await pic.setupCanister<SatelliteActor>({
idlFactory: idlFactorSatellite,
wasm: SATELLITE_WASM_PATH,
arg: controllersInitArgs(controller),
sender: controller.getPrincipal()
});

actor = c;
canisterId = cId;

actor.setIdentity(controller);

Expand Down Expand Up @@ -804,5 +814,124 @@ describe.each([{ memory: { Heap: null } }, { memory: { Stable: null } }])(
});
});
});

describe('serializer/deserializer', () => {
const user = Ed25519KeyIdentity.generate();

const setRule: SetRule = {
memory: toNullable(memory),
max_size: toNullable(),
read: { Managed: null },
mutable_permissions: toNullable(),
write: { Managed: null },
version: toNullable(),
max_capacity: toNullable(),
rate_config: toNullable(),
max_changes_per_user: toNullable()
};

const collection = `test_data_${'Heap' in memory ? 'heap' : 'stable'}`;

const data = {
// date: nowInBigIntNanoSeconds(),
myBigInt: 666777888n,
principal: user.getPrincipal(),
array: arrayOfNumberToUint8Array([4, 5, 6, 7, 8, 9]),
myString: 'hello',
myNumber: 123
};

beforeAll(async () => {
actor.setIdentity(controller);

const { set_rule } = actor;

await set_rule({ Db: null }, collection, setRule);
});

it('should serialize data for types not supported by the JS APIs', async () => {
// TODO
// const mockDateNow = 1698416400000;
// vi.spyOn(Date, 'now').mockReturnValue(mockDateNow);

const serializedData = await toArray(data);

const key = nanoid();

const { set_doc, get_doc } = actor;

await set_doc(collection, key, {
data: serializedData,
description: toNullable(),
version: toNullable()
});

const result = await get_doc(collection, key);

const doc = fromNullable(result);

assertNonNullish(doc);

// We explicitly want to assert that the data are saved / serialized in a certain way so we read the raw data
const blob = new Blob(
[doc.data instanceof Uint8Array ? doc.data : new Uint8Array(doc.data)],
{
type: 'application/json; charset=utf-8'
}
);

const savedData = JSON.parse(await blob.text());

expect(savedData).toEqual({
myBigInt: { __bigint__: `${data.myBigInt}` },
principal: {
__principal__: data.principal.toText()
},
array: {
__uint8array__: uint8ArrayToArrayOfNumber(data.array)
},
myString: data.myString,
myNumber: data.myNumber
});
});

it('should deserialize data for types not supported by the JS APIs', async () => {
// TODO
// const mockDateNow = 1698416400000;
// vi.spyOn(Date, 'now').mockReturnValue(mockDateNow);

const serializedData = await toArray(data);

const key = nanoid();

const { set_doc, get_doc } = actor;

await set_doc(collection, key, {
data: serializedData,
description: toNullable(),
version: toNullable()
});

const result = await get_doc(collection, key);

const doc = fromNullable(result);

assertNonNullish(doc);

const savedData: typeof data = await fromArray(doc.data);

expect(typeof savedData.myBigInt).toEqual('bigint');
expect(savedData.myBigInt).toEqual(data.myBigInt);

expect(savedData.principal).toBeInstanceOf(Principal);
expect(savedData.principal.toText()).toEqual(data.principal.toText());

expect(savedData.array).toBeInstanceOf(Uint8Array);
expect(savedData.array).toEqual(data.array);

expect(savedData.myString).toEqual(data.myString);
expect(savedData.myNumber).toEqual(data.myNumber);
});
});
}
);

0 comments on commit 74cca96

Please sign in to comment.