Files
directus/packages/utils/node/process-id.test.ts
Rijk van Zanten b0f5baa394 Add @directus/memory package (#20514)
* Add analytics table

* Setup memory package boilerplate

* Bootstrap library

* [WIP] setup serialize/deserialize for data

* Finish compress/serialize utils

* Add get/set for local

* Add increments method

* Add delete + comments

* Add pub/sub methods

* Remove erroneous files

* Add has, setMax

* Restructure to 3 subclasses

* WIP add Redis cache

* Add set-max

* Add cacheMulti

* Add tests for redis cache

* Don't compress small values

* Only decompress values that are compressed

* Add test for arr-to-buf

* Add tests for multi-stage cache

* Add test for create

* Finish redis based messenger bus

* Add redis bus to createBus

* Resolve lock file conflict

* Align dev deps versions

* Format files

* Update packages/memory/package.json

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>

* It's okay, ESLint

* Add local rate limiter

* Add redis limiter

* Add simple readme

* Add changeset

* 'key value' -> 'key-value'

* Sort package.json

* Remove leftovers

* Move timer calls to corresponding test

* Add KV asbraction based on previous "cache"

* Update cache to use kv

* Add more specific console logs

* Only define setMax if not exists

* Run formatter

* Use parseJSON util

* Fix typo in class docs

* Install directus/utils

* Default to Map if LRU config isn't set

* Ignore errors thrown in handlers

* Create process-id util

* Use pubsub in multi-stage handler

* WIP Update tests

* Update package lock

* Fix formatting errors

* Fix timestamp in test

* Uno mas

* Fix mock order

---------

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
2023-12-13 11:29:22 -05:00

36 lines
1015 B
TypeScript

import { createHash, type Hash } from 'node:crypto';
import { hostname } from 'node:os';
import { afterEach, beforeEach, expect, test, vi } from 'vitest';
import { _cache, processId } from './process-id.js';
vi.mock('node:crypto');
vi.mock('node:os');
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
_cache.id = undefined;
vi.useRealTimers();
});
test('Returns cached value if exists', () => {
const val = 'test-value';
_cache.id = val;
expect(processId()).toBe(val);
});
test('Generates and returns hash if value does not exist yet', () => {
const mockHash = { update: vi.fn().mockReturnThis(), digest: vi.fn() } as unknown as Hash;
vi.mocked(createHash).mockReturnValue(mockHash);
vi.mocked(hostname).mockReturnValue('test-hostname');
vi.setSystemTime(new Date('2023-11-14T22:13:20Z'));
processId();
expect(createHash).toHaveBeenCalledWith('md5');
expect(hostname).toHaveBeenCalled();
expect(mockHash.update).toHaveBeenCalledWith(`test-hostname${process.pid}1700000000000`);
});