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

test: logs with serverless functions #1209

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
test: log error, keys and capacity
  • Loading branch information
peterpeterparker committed Feb 8, 2025
commit f2dc288ba1d710be4320c4f3400c5a5e12ed440d
2 changes: 1 addition & 1 deletion src/tests/fixtures/test_satellite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["cdylib"]
[dependencies]
candid.workspace = true
ic-cdk.workspace = true
junobuild-satellite = { path = "../../../libs/satellite", default-features = false, features = ["on_set_doc"] }
junobuild-satellite = { path = "../../../libs/satellite", default-features = false, features = ["on_set_doc", "on_delete_doc"] }
junobuild-macros = { path = "../../../libs/macros" }
11 changes: 9 additions & 2 deletions src/tests/fixtures/test_satellite/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use junobuild_macros::on_set_doc;
use junobuild_satellite::{include_satellite, info, OnSetDocContext};
use junobuild_macros::{on_set_doc, on_delete_doc};
use junobuild_satellite::{include_satellite, info, error, OnSetDocContext, OnDeleteDocContext};

#[on_set_doc]
fn on_set_doc(_context: OnSetDocContext) -> Result<(), String> {
Expand All @@ -8,4 +8,11 @@ fn on_set_doc(_context: OnSetDocContext) -> Result<(), String> {
Ok(())
}

#[on_delete_doc]
fn on_delete_doc(_context: OnDeleteDocContext) -> Result<(), String> {
error("Delete Hello world".to_string())?;

Ok(())
}

include_satellite!();
91 changes: 81 additions & 10 deletions src/tests/specs/satellite.logs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { type Actor, PocketIc } from '@hadronous/pic';
import { fromArray } from '@junobuild/utils';
import { afterAll, beforeAll, describe, expect, inject } from 'vitest';
import { mockSetRule } from './mocks/collection.mocks';
import { mockListParams } from './mocks/list.mocks';
import { tick } from './utils/pic-tests.utils';
import { createDoc as createDocUtils } from './utils/satellite-doc-tests.utils';
import { controllersInitArgs, TEST_SATELLITE_WASM_PATH } from './utils/setup-tests.utils';
Expand Down Expand Up @@ -51,23 +52,26 @@ describe('Satellite Logging', () => {
await pic?.tearDown();
});

it('should log an info when on_set_doc hook is fired', async () => {
await createDocUtils({
const waitServerlessFunction = async () => {
// Wait for the serverless function to being fired
await tick(pic);
};

const createDoc = async (): Promise<string> => {
return createDocUtils({
actor,
collection: TEST_COLLECTION
});
};

// Wait for the serverless function to being fired
await tick(pic);
it('should log an info when on_set_doc hook is fired', async () => {
await createDoc();

await waitServerlessFunction();

const { list_docs } = actor;

const { items: logs } = await list_docs('#log', {
matcher: toNullable(),
order: toNullable(),
owner: toNullable(),
paginate: toNullable()
});
const { items: logs } = await list_docs('#log', mockListParams);

expect(logs).toHaveLength(1);

Expand All @@ -82,4 +86,71 @@ describe('Satellite Logging', () => {
message: 'Hello world'
});
});

it('should log an error when on_delete_doc hook is fired', async () => {
const { list_docs, del_doc } = actor;

const { items } = await list_docs(TEST_COLLECTION, mockListParams);

const [item] = items;
const [key, doc] = item;

await del_doc(TEST_COLLECTION, key, doc);

await waitServerlessFunction();

const { items: logs } = await list_docs('#log', {
...mockListParams,
order: toNullable({
desc: true,
field: { CreatedAt: null }
})
});

expect(logs).toHaveLength(2);

const [log, _] = logs;
const [__, docLog] = log;

const data = await fromArray(docLog.data);

expect(data).toEqual({
data: null,
level: 'Error',
message: 'Delete Hello world'
});
});

it('should create logs with different random keys', async () => {
await Promise.all(Array.from({ length: 10 }).map(createDoc));

await waitServerlessFunction();

const { list_docs } = actor;

const { items: logs } = await list_docs('#log', mockListParams);

expect(logs).toHaveLength(12);

const keys = new Set([...logs.map(([key, _]) => key)]);
expect(keys).toHaveLength(12);

// Log key is format!("{}-{}", time(), nonce)
// When we create doc and log with serverless without tick, the time should be the same
// Therefore, without nonce, the count of entry should be smaller than the effective count of time we logged
const trimmedKey = new Set([...logs.map(([key, _]) => key.split('-')[0])]);
expect(trimmedKey.size).toBeLessThan(12);
});

it('should limit log entries to hundred', async () => {
await Promise.all(Array.from({ length: 101 }).map(createDoc));

await waitServerlessFunction();

const { list_docs } = actor;

const { items: logs } = await list_docs('#log', mockListParams);

expect(logs).toHaveLength(100);
});
});