session-lifecycle.test.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { describe, expect, test } from 'bun:test';
  2. import { SessionLifecycle } from './session-lifecycle';
  3. const noop = () => {};
  4. describe('SessionLifecycle', () => {
  5. test('dispatchSessionDeleted runs callbacks in order', () => {
  6. const lc = new SessionLifecycle(noop);
  7. const ran: string[] = [];
  8. lc.onSessionDeleted((id) => ran.push(`a:${id}`));
  9. lc.onSessionDeleted((id) => ran.push(`b:${id}`));
  10. lc.dispatchSessionDeleted('s1');
  11. expect(ran).toEqual(['a:s1', 'b:s1']);
  12. });
  13. test('dispatchSessionDeleted continues after callback error', () => {
  14. const lc = new SessionLifecycle(() => {});
  15. const ran: string[] = [];
  16. lc.onSessionDeleted(() => {
  17. throw new Error('fail');
  18. });
  19. lc.onSessionDeleted((id) => ran.push(id));
  20. lc.dispatchSessionDeleted('s1');
  21. expect(ran).toEqual(['s1']);
  22. });
  23. test('consumePending is atomic', () => {
  24. const lc = new SessionLifecycle(noop);
  25. lc.markPending('s1');
  26. expect(lc.consumePending('s1')).toBe(true);
  27. expect(lc.consumePending('s1')).toBe(false);
  28. });
  29. test('clearSession removes pending state', () => {
  30. const lc = new SessionLifecycle(noop);
  31. lc.markPending('s1');
  32. lc.clearSession('s1');
  33. expect(lc.consumePending('s1')).toBe(false);
  34. });
  35. });