|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { createMemoryCache } from './memory-cache'; |
| 3 | +import { createMemoryQueue } from './memory-queue'; |
| 4 | +import { createMemoryJob } from './memory-job'; |
| 5 | +import { CORE_FALLBACK_FACTORIES } from './index'; |
| 6 | + |
| 7 | +describe('CORE_FALLBACK_FACTORIES', () => { |
| 8 | + it('should have exactly 3 entries: cache, queue, job', () => { |
| 9 | + expect(Object.keys(CORE_FALLBACK_FACTORIES)).toEqual(['cache', 'queue', 'job']); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should map to factory functions', () => { |
| 13 | + for (const factory of Object.values(CORE_FALLBACK_FACTORIES)) { |
| 14 | + expect(typeof factory).toBe('function'); |
| 15 | + } |
| 16 | + }); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('createMemoryCache', () => { |
| 20 | + it('should return an object with _fallback: true', () => { |
| 21 | + const cache = createMemoryCache(); |
| 22 | + expect(cache._fallback).toBe(true); |
| 23 | + expect(cache._serviceName).toBe('cache'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should set and get a value', async () => { |
| 27 | + const cache = createMemoryCache(); |
| 28 | + await cache.set('key1', 'value1'); |
| 29 | + expect(await cache.get('key1')).toBe('value1'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return undefined for missing key', async () => { |
| 33 | + const cache = createMemoryCache(); |
| 34 | + expect(await cache.get('nonexistent')).toBeUndefined(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should delete a key', async () => { |
| 38 | + const cache = createMemoryCache(); |
| 39 | + await cache.set('key1', 'value1'); |
| 40 | + expect(await cache.delete('key1')).toBe(true); |
| 41 | + expect(await cache.get('key1')).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should check if a key exists with has()', async () => { |
| 45 | + const cache = createMemoryCache(); |
| 46 | + expect(await cache.has('key1')).toBe(false); |
| 47 | + await cache.set('key1', 'value1'); |
| 48 | + expect(await cache.has('key1')).toBe(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should clear all entries', async () => { |
| 52 | + const cache = createMemoryCache(); |
| 53 | + await cache.set('a', 1); |
| 54 | + await cache.set('b', 2); |
| 55 | + await cache.clear(); |
| 56 | + expect(await cache.has('a')).toBe(false); |
| 57 | + expect(await cache.has('b')).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should expire entries based on TTL', async () => { |
| 61 | + const cache = createMemoryCache(); |
| 62 | + // Set with very short TTL (0.001 seconds = 1ms) |
| 63 | + await cache.set('temp', 'data', 0.001); |
| 64 | + // Wait for expiry |
| 65 | + await new Promise(r => setTimeout(r, 20)); |
| 66 | + expect(await cache.get('temp')).toBeUndefined(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should track hit/miss stats', async () => { |
| 70 | + const cache = createMemoryCache(); |
| 71 | + await cache.set('key1', 'value1'); |
| 72 | + await cache.get('key1'); // hit |
| 73 | + await cache.get('missing'); // miss |
| 74 | + const stats = await cache.stats(); |
| 75 | + expect(stats.hits).toBe(1); |
| 76 | + expect(stats.misses).toBe(1); |
| 77 | + expect(stats.keyCount).toBe(1); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe('createMemoryQueue', () => { |
| 82 | + it('should return an object with _fallback: true', () => { |
| 83 | + const queue = createMemoryQueue(); |
| 84 | + expect(queue._fallback).toBe(true); |
| 85 | + expect(queue._serviceName).toBe('queue'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should publish and deliver to subscriber synchronously', async () => { |
| 89 | + const queue = createMemoryQueue(); |
| 90 | + const received: any[] = []; |
| 91 | + await queue.subscribe('test-q', async (msg: any) => { received.push(msg); }); |
| 92 | + const id = await queue.publish('test-q', { hello: 'world' }); |
| 93 | + expect(id).toMatch(/^fallback-msg-/); |
| 94 | + expect(received).toHaveLength(1); |
| 95 | + expect(received[0].data).toEqual({ hello: 'world' }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should not deliver to unsubscribed queue', async () => { |
| 99 | + const queue = createMemoryQueue(); |
| 100 | + const received: any[] = []; |
| 101 | + await queue.subscribe('q1', async (msg: any) => { received.push(msg); }); |
| 102 | + await queue.unsubscribe('q1'); |
| 103 | + await queue.publish('q1', 'data'); |
| 104 | + expect(received).toHaveLength(0); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should return queue size of 0', async () => { |
| 108 | + const queue = createMemoryQueue(); |
| 109 | + expect(await queue.getQueueSize()).toBe(0); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should purge a queue', async () => { |
| 113 | + const queue = createMemoryQueue(); |
| 114 | + const received: any[] = []; |
| 115 | + await queue.subscribe('q1', async (msg: any) => { received.push(msg); }); |
| 116 | + await queue.purge('q1'); |
| 117 | + await queue.publish('q1', 'data'); |
| 118 | + expect(received).toHaveLength(0); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe('createMemoryJob', () => { |
| 123 | + it('should return an object with _fallback: true', () => { |
| 124 | + const job = createMemoryJob(); |
| 125 | + expect(job._fallback).toBe(true); |
| 126 | + expect(job._serviceName).toBe('job'); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should schedule and list jobs', async () => { |
| 130 | + const job = createMemoryJob(); |
| 131 | + await job.schedule('daily-report', '0 0 * * *', async () => {}); |
| 132 | + expect(await job.listJobs()).toEqual(['daily-report']); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should cancel a job', async () => { |
| 136 | + const job = createMemoryJob(); |
| 137 | + await job.schedule('temp-job', '* * * * *', async () => {}); |
| 138 | + await job.cancel('temp-job'); |
| 139 | + expect(await job.listJobs()).toEqual([]); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should trigger a job handler', async () => { |
| 143 | + const job = createMemoryJob(); |
| 144 | + let triggered = false; |
| 145 | + await job.schedule('my-job', '* * * * *', async (ctx: any) => { |
| 146 | + triggered = true; |
| 147 | + expect(ctx.jobId).toBe('my-job'); |
| 148 | + expect(ctx.data).toEqual({ key: 'val' }); |
| 149 | + }); |
| 150 | + await job.trigger('my-job', { key: 'val' }); |
| 151 | + expect(triggered).toBe(true); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return empty executions', async () => { |
| 155 | + const job = createMemoryJob(); |
| 156 | + expect(await job.getExecutions()).toEqual([]); |
| 157 | + }); |
| 158 | +}); |
0 commit comments