Skip to content

Commit

Permalink
add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Nov 28, 2023
1 parent 6676626 commit b001602
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 0 deletions.
77 changes: 77 additions & 0 deletions packages/nocap/src/classes/DeferredWrapper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { DeferredWrapper } from './DeferredWrapper.js'
import Deferred from 'promise-deferred'

describe('DeferredWrapper', () => {
let deferredWrapper;
let mockSession;
let mockTimeout;

beforeEach(() => {
mockSession = { get: vi.fn(() => 'mockSession') };
mockTimeout = {
create: vi.fn(),
has: vi.fn(),
get: vi.fn()
};
deferredWrapper = new DeferredWrapper(mockSession, mockTimeout);
});

it('should initialize correctly', () => {
expect(deferredWrapper.promises).toEqual({});
expect(deferredWrapper.timeout).toBe(mockTimeout);
expect(deferredWrapper.$session).toBe(mockSession);
});

it('should add a new deferred promise', () => {
const deferred = deferredWrapper.add('testKey');
expect(typeof deferred.then).toBe('function')
expect(deferredWrapper.promises['mockSession']['testKey']).toBeInstanceOf(Deferred);
});

it('should resolve a promise', async () => {
const promise = deferredWrapper.add('testKey');
deferredWrapper.resolve('testKey', 'testResult');
await expect(promise).resolves.toEqual('testResult');
});

it('should reject a promise', async () => {
const promise = deferredWrapper.add('testKey');
const testError = new Error('testError');
deferredWrapper.reject('testKey', testError);
await expect(promise).rejects.toThrow(testError);
});

it('should reflect the state of a promise', async () => {
const promise = deferredWrapper.add('testKey');
const { state, reflectedPromise } = deferredWrapper.reflect('testKey');
expect(state.isPending).toBe(true);
deferredWrapper.resolve('testKey', 'testResult');
await expect(reflectedPromise).resolves.toEqual('testResult');
expect(state.isFulfilled).toBe(true);
expect(state.isPending).toBe(false);
});

it('should clear session promises', () => {
deferredWrapper.add('testKey');
deferredWrapper.clearSessionPromises();
expect(deferredWrapper.promises['mockSession']).toBeUndefined();
});

it('should create a new promise', () => {
deferredWrapper.create('testKey');
expect(deferredWrapper.promises['mockSession']['testKey']).toBeInstanceOf(Deferred);
});

it('should get an existing promise', () => {
deferredWrapper.add('testKey');
const promise = deferredWrapper.get('testKey');
expect(promise).toBeInstanceOf(Deferred);
});

it('should reset all promises', () => {
deferredWrapper.add('testKey');
deferredWrapper.reset();
expect(deferredWrapper.promises).toEqual({});
});
});
91 changes: 91 additions & 0 deletions packages/nocap/src/classes/SessionHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { SessionHelper } from './SessionHelper.js'
import murmurhash from 'murmurhash'
import { random } from '../utils.js'
import * as utilsModule from '../utils.js'

describe('SessionHelper', () => {
let sessionHelper;

beforeEach(() => {
sessionHelper = new SessionHelper();
console.log(sessionHelper.salt, typeof sessionHelper.salt)
});

afterEach(() => {
});

it('should initialize with correct values', () => {
expect(typeof sessionHelper.salt).toBe('number');
expect(sessionHelper.id).toBeInstanceOf(Object);
});

it('keys to change ', () => {
const random1 = murmurhash.v3(random(50))
const random2 = murmurhash.v3(random(50))
const key1 = murmurhash.v3('key', random1)
const key2 = murmurhash.v3('key', random2)

console.log(random1, random2)
expect(random1 === random2).toBe(false)
console.log(key1, key2)
expect(key1 === key2).toBe(false)
})

it('murmurhash behaves', () => {
const random1 = murmurhash.v3(random(50))
const random2 = murmurhash.v3(random(50))
const key1 = murmurhash.v3('key', random1)
const key2 = murmurhash.v3('key', random2)

console.log(key1, key2)
expect(key1 === key2).toBe(false)
})



it('should create hash ids correctly', () => {
const expectedSessionId = murmurhash.v3('session', sessionHelper.salt);
const expectedConnectId = murmurhash.v3('connect', sessionHelper.salt);
const expectedReadId = murmurhash.v3('read', sessionHelper.salt);
const expectedWriteId = murmurhash.v3('write', sessionHelper.salt);
const expectedInfoId = murmurhash.v3('info', sessionHelper.salt);
const expectedGeoId = murmurhash.v3('geo', sessionHelper.salt);

expect(sessionHelper.id.session).toEqual(expectedSessionId);
expect(sessionHelper.id.connect).toEqual(expectedConnectId);
expect(sessionHelper.id.read).toEqual(expectedReadId);
expect(sessionHelper.id.write).toEqual(expectedWriteId);
expect(sessionHelper.id.info).toEqual(expectedInfoId);
expect(sessionHelper.id.geo).toEqual(expectedGeoId);
});

it('should setup new ids on new()', () => {
const oldIds = Object.assign({}, sessionHelper.id)
console.log(oldIds)

sessionHelper.new();

console.log(sessionHelper.new())

expect(sessionHelper.id.session).not.toEqual(oldIds.session);
expect(sessionHelper.id.connect).not.toEqual(oldIds.connect);
expect(sessionHelper.id.read).not.toEqual(oldIds.read);
expect(sessionHelper.id.write).not.toEqual(oldIds.write);
expect(sessionHelper.id.info).not.toEqual(oldIds.info);
expect(sessionHelper.id.geo).not.toEqual(oldIds.geo);
});

it('should return correct id for get(key)', () => {
expect(sessionHelper.get('session')).toEqual(sessionHelper.id.session);
expect(sessionHelper.get('connect')).toEqual(sessionHelper.id.connect);
expect(sessionHelper.get('read')).toEqual(sessionHelper.id.read);
expect(sessionHelper.get('write')).toEqual(sessionHelper.id.write);
expect(sessionHelper.get('info')).toEqual(sessionHelper.id.info);
expect(sessionHelper.get('geo')).toEqual(sessionHelper.id.geo);
});

it('should return session id for get() with no key', () => {
expect(sessionHelper.get()).toEqual(sessionHelper.id.session);
});
});
48 changes: 48 additions & 0 deletions packages/nocap/src/classes/Validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { Validator } from './Validator.js'

describe('Validator', () => {
let validator;

beforeEach(() => {
validator = new Validator();
validator.defaults = {
name: 'defaultName',
age: 30,
isActive: false
};
});

it('should set a value correctly', () => {
validator.set('name', 'John');
expect(validator.name).toBe('John');
});

it('should throw an error for setting value of wrong type', () => {
expect(() => validator.set('age', '35')).toThrow();
});

it('should get a value correctly', () => {
validator.name = 'Jane';
expect(validator.get('name')).toBe('Jane');
});

it('should throw an error for getting an undefined property', () => {
expect(() => validator.get('unknown')).toThrow();
});

it('should set multiple values correctly', () => {
validator.setMany({ name: 'Alice', age: 28 });
expect(validator.name).toBe('Alice');
expect(validator.age).toBe(28);
});

it('should dump all properties except defaults', () => {
validator.name = 'Bob';
validator.age = 40;
const dumped = validator.dump();

expect(dumped).toEqual({ name: 'Bob', age: 40 });
expect(dumped.defaults).toBeUndefined();
});
});

0 comments on commit b001602

Please sign in to comment.