Skip to content

Commit

Permalink
feat: in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpeterparker committed Feb 9, 2025
1 parent 7174aac commit 91f7962
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/frontend/src/lib/services/user.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const loadUserUsages = async ({
collectionType: CollectionType;
maxChangesPerUser: number | undefined;
}): Promise<UserUsageCollection> => {
let key = `${user.owner.toText()}#${collection}`;
const key = `${user.owner.toText()}#${collection}`;

let userUsageCollection =
const userUsageCollection =
'Storage' in collectionType ? '#user_usage_storage' : '#user_usage_db';

const result = await getDoc({
Expand Down
147 changes: 87 additions & 60 deletions src/tests/specs/satellite.user-usage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type {
CollectionType,
DelDoc,
Doc,
ListParams,
_SERVICE as SatelliteActor,
SetDoc,
SetRule,
UserUsage
SetRule
} from '$declarations/satellite/satellite.did';
import { idlFactory as idlFactorSatellite } from '$declarations/satellite/satellite.factory.did';
import { Ed25519KeyIdentity } from '@dfinity/identity';
import type { Principal } from '@dfinity/principal';
import { assertNonNullish, fromNullable, toNullable } from '@dfinity/utils';
import { assertNonNullish, fromNullable, isNullish, toNullable } from '@dfinity/utils';
import { type Actor, PocketIc } from '@hadronous/pic';
import { toArray } from '@junobuild/utils';
import { fromArray, toArray } from '@junobuild/utils';
import { nanoid } from 'nanoid';
import { beforeAll, describe, expect, inject } from 'vitest';
import { SATELLITE_ADMIN_ERROR_MSG } from './constants/satellite-tests.constants';
Expand Down Expand Up @@ -47,6 +48,34 @@ describe('Satellite User Usage', () => {
paginate: toNullable()
};

interface UserUsage {
changes_count: number;
}

const get_user_usage = async (

Check warning on line 55 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Functions with more than one parameter should accept an object and use destructuring
collection: string,
collectionType: CollectionType,
userId: Principal
): Promise<{ doc: Doc | undefined; usage: UserUsage | undefined }> => {
const { get_doc } = actor;

const key = `${userId.toText()}#${collection}`;

const userUsageCollection =
'Storage' in collectionType ? '#user_usage_storage' : '#user_usage_db';

const result = await get_doc(userUsageCollection, key);

const doc = fromNullable(result);

const usage = isNullish(doc) ? undefined : await fromArray<UserUsage>(doc.data);

return {
doc,
usage
};
};

beforeAll(async () => {
pic = await PocketIc.create(inject('PIC_URL'));

Expand Down Expand Up @@ -93,23 +122,24 @@ describe('Satellite User Usage', () => {
it('should get a usage count after set documents', async () => {
await Promise.all(Array.from({ length: countSetDocs }).map(createDoc));

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
const { doc, usage } = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
user.getPrincipal()
);

assertNonNullish(doc);
assertNonNullish(usage);

expect(usage.changes_count).toEqual(countSetDocs);

expect(usage.updated_at).not.toBeUndefined();
expect(usage.updated_at).toBeGreaterThan(0n);
expect(usage.created_at).not.toBeUndefined();
expect(usage.created_at).toBeGreaterThan(0n);
expect(usage.updated_at).toBeGreaterThan(usage.created_at);
expect(doc.updated_at).not.toBeUndefined();
expect(doc.updated_at).toBeGreaterThan(0n);
expect(doc.created_at).not.toBeUndefined();
expect(doc.created_at).toBeGreaterThan(0n);
expect(doc.updated_at).toBeGreaterThan(doc.created_at);

expect(usage.version).toEqual(toNullable(BigInt(countSetDocs)));
expect(doc.version).toEqual(toNullable(BigInt(countSetDocs)));
});

const countSetManyDocs = 5;
Expand All @@ -131,16 +161,17 @@ describe('Satellite User Usage', () => {

await set_many_docs(docs);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
const { doc, usage } = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
user.getPrincipal()
);

assertNonNullish(doc);
assertNonNullish(usage);

expect(usage.changes_count).toEqual(countSetManyDocs + countSetDocs);
expect(usage.version).toEqual(toNullable(BigInt(countSetManyDocs + countSetDocs)));
expect(doc.version).toEqual(toNullable(BigInt(countSetManyDocs + countSetDocs)));
});

const countDelDoc = 1;
Expand All @@ -156,16 +187,17 @@ describe('Satellite User Usage', () => {
version: doc.version ?? []
});

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
const { doc: docRead, usage } = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
user.getPrincipal()
);

assertNonNullish(docRead);
assertNonNullish(usage);

expect(usage.changes_count).toEqual(countSetManyDocs + countSetDocs + countDelDoc);
expect(usage.version).toEqual(
expect(docRead.version).toEqual(
toNullable(BigInt(countSetManyDocs + countSetDocs + countDelDoc))
);
});
Expand All @@ -187,18 +219,19 @@ describe('Satellite User Usage', () => {

await del_many_docs(docs);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
const { doc, usage } = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
user.getPrincipal()
);

assertNonNullish(doc);
assertNonNullish(usage);

expect(usage.changes_count).toEqual(
countSetManyDocs + countSetDocs + countDelDoc + countDelManyDocs
);
expect(usage.version).toEqual(
expect(doc.version).toEqual(
toNullable(BigInt(countSetManyDocs + countSetDocs + countDelDoc + countDelManyDocs))
);
});
Expand All @@ -208,12 +241,13 @@ describe('Satellite User Usage', () => {

await del_filtered_docs(TEST_COLLECTION, NO_FILTER_PARAMS);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
const { doc, usage } = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
user.getPrincipal()
);

assertNonNullish(doc);
assertNonNullish(usage);

const countRemainingDocs = countSetManyDocs + countSetDocs - countDelDoc - countDelManyDocs;
Expand All @@ -222,31 +256,29 @@ describe('Satellite User Usage', () => {
countSetManyDocs + countSetDocs + countDelDoc + countDelManyDocs + countRemainingDocs;

expect(usage.changes_count).toEqual(countTotalChanges);
expect(usage.version).toEqual(toNullable(BigInt(countTotalChanges)));
expect(doc.version).toEqual(toNullable(BigInt(countTotalChanges)));
});
});

describe('Guards', () => {
const user1 = Ed25519KeyIdentity.generate();

const fetchUsage = async (userId?: Principal): Promise<UserUsage | undefined> => {
const { get_user_usage } = actor;

const usageResponse = await get_user_usage(
const fetchUsage = async (
userId?: Principal
): Promise<{ doc: Doc | undefined; usage: UserUsage | undefined }> => {

Check warning on line 268 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected block statement surrounding arrow body; move the returned value immediately after the `=>`
return await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
toNullable(userId)
userId ?? user.getPrincipal()
);

return fromNullable(usageResponse);
};

it('should not get usage of another user', async () => {
actor.setIdentity(user1);

await createDoc();

const usage = await fetchUsage();
const { usage } = await fetchUsage();

assertNonNullish(usage);

Expand All @@ -264,7 +296,7 @@ describe('Satellite User Usage', () => {
it('should get usage of user if controller', async () => {
actor.setIdentity(controller);

const usage = await fetchUsage(user1.getPrincipal());
const { usage } = await fetchUsage(user1.getPrincipal());

assertNonNullish(usage);

Expand All @@ -274,10 +306,15 @@ describe('Satellite User Usage', () => {
it('should throw errors on set usage', async () => {
actor.setIdentity(user1);

const { set_user_usage } = actor;
const { set_doc } = actor;

const key = `${user1.getPrincipal().toText()}#${TEST_COLLECTION}`;

const userUsageCollection =
'Storage' in collectionType ? '#user_usage_storage' : '#user_usage_db';

Check failure on line 314 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Cannot find name 'collectionType'. Did you mean 'COLLECTION_TYPE'?

Check failure on line 314 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Cannot find name 'collectionType'. Did you mean 'COLLECTION_TYPE'?

await expect(
set_user_usage(TEST_COLLECTION, COLLECTION_TYPE, user1.getPrincipal(), {
set_doc(userUsageCollection, key, {
changes_count: 345

Check failure on line 318 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Object literal may only specify known properties, and 'changes_count' does not exist in type 'SetDoc'.

Check failure on line 318 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Object literal may only specify known properties, and 'changes_count' does not exist in type 'SetDoc'.
})
).rejects.toThrow(SATELLITE_ADMIN_ERROR_MSG);
Expand Down Expand Up @@ -385,8 +422,6 @@ describe('Satellite User Usage', () => {
Array.from({ length: countUploadAssets }).map(async (_, i) => await upload(i))
);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

Check failure on line 425 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type '[] | [unknown]' is not assignable to parameter of type 'Principal'.

Check failure on line 425 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type '[] | [unknown]' is not assignable to parameter of type 'Principal'.

const usage = fromNullable(usageResponse);

Check failure on line 427 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type '{ doc: Doc | undefined; usage: UserUsage | undefined; }' is not assignable to parameter of type '[] | [unknown]'.

Check failure on line 427 in src/tests/specs/satellite.user-usage.spec.ts

View workflow job for this annotation

GitHub Actions / tests

Argument of type '{ doc: Doc | undefined; usage: UserUsage | undefined; }' is not assignable to parameter of type '[] | [unknown]'.
Expand Down Expand Up @@ -415,8 +450,6 @@ describe('Satellite User Usage', () => {

await del_asset(TEST_COLLECTION, asset.key.full_path);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
Expand All @@ -441,8 +474,6 @@ describe('Satellite User Usage', () => {

await del_many_assets(assets);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
Expand All @@ -460,8 +491,6 @@ describe('Satellite User Usage', () => {

await del_filtered_assets(TEST_COLLECTION, NO_FILTER_PARAMS);

const { get_user_usage } = actor;

const usageResponse = await get_user_usage(TEST_COLLECTION, COLLECTION_TYPE, toNullable());

const usage = fromNullable(usageResponse);
Expand All @@ -487,8 +516,6 @@ describe('Satellite User Usage', () => {
});

const fetchUsage = async (userId?: Principal): Promise<UserUsage | undefined> => {
const { get_user_usage } = actor;

const usageResponse = await get_user_usage(
TEST_COLLECTION,
COLLECTION_TYPE,
Expand Down

0 comments on commit 91f7962

Please sign in to comment.