|  | 
|  | 1 | +import { beforeEach, describe, expect, it } from 'vitest' | 
|  | 2 | +import { handleCommentEvent, states } from '../src/entrypoints/background' | 
|  | 3 | +import type { CommentEvent, CommentSpot } from '../src/lib/enhancer' | 
|  | 4 | + | 
|  | 5 | +describe('Background Event Handler', () => { | 
|  | 6 | +  let mockSender: any | 
|  | 7 | +  let mockSpot: CommentSpot | 
|  | 8 | + | 
|  | 9 | +  beforeEach(() => { | 
|  | 10 | +    // Clear the shared states map before each test | 
|  | 11 | +    states.clear() | 
|  | 12 | + | 
|  | 13 | +    mockSender = { | 
|  | 14 | +      tab: { | 
|  | 15 | +        id: 123, | 
|  | 16 | +        windowId: 456, | 
|  | 17 | +      }, | 
|  | 18 | +    } | 
|  | 19 | + | 
|  | 20 | +    mockSpot = { | 
|  | 21 | +      type: 'TEST_SPOT', | 
|  | 22 | +      unique_key: 'test-key', | 
|  | 23 | +    } | 
|  | 24 | +  }) | 
|  | 25 | + | 
|  | 26 | +  describe('ENHANCED Event', () => { | 
|  | 27 | +    it('should create new comment state when textarea is enhanced', () => { | 
|  | 28 | +      const message: CommentEvent = { | 
|  | 29 | +        draft: undefined, | 
|  | 30 | +        spot: mockSpot, | 
|  | 31 | +        type: 'ENHANCED', | 
|  | 32 | +      } | 
|  | 33 | + | 
|  | 34 | +      handleCommentEvent(message, mockSender) | 
|  | 35 | + | 
|  | 36 | +      const expectedKey = { | 
|  | 37 | +        spot: mockSpot, | 
|  | 38 | +        tab: { tabId: 123, windowId: 456 }, | 
|  | 39 | +      } | 
|  | 40 | + | 
|  | 41 | +      const state = states.get(expectedKey) | 
|  | 42 | +      expect(state).toBeDefined() | 
|  | 43 | +      expect(state?.tab).toEqual({ tabId: 123, windowId: 456 }) | 
|  | 44 | +      expect(state?.spot).toEqual(mockSpot) | 
|  | 45 | +      expect(state?.drafts).toEqual([]) | 
|  | 46 | +    }) | 
|  | 47 | + | 
|  | 48 | +    it('should not handle ENHANCED event without tab info', () => { | 
|  | 49 | +      const message: CommentEvent = { | 
|  | 50 | +        draft: undefined, | 
|  | 51 | +        spot: mockSpot, | 
|  | 52 | +        type: 'ENHANCED', | 
|  | 53 | +      } | 
|  | 54 | + | 
|  | 55 | +      const senderWithoutTab = { tab: null } | 
|  | 56 | + | 
|  | 57 | +      handleCommentEvent(message, senderWithoutTab) | 
|  | 58 | + | 
|  | 59 | +      expect(states.size).toBe(0) | 
|  | 60 | +    }) | 
|  | 61 | +  }) | 
|  | 62 | + | 
|  | 63 | +  describe('DESTROYED Event', () => { | 
|  | 64 | +    it('should remove comment state when textarea is destroyed', () => { | 
|  | 65 | +      // First create a state using the actual handler | 
|  | 66 | +      const enhanceMessage: CommentEvent = { | 
|  | 67 | +        draft: undefined, | 
|  | 68 | +        spot: mockSpot, | 
|  | 69 | +        type: 'ENHANCED', | 
|  | 70 | +      } | 
|  | 71 | + | 
|  | 72 | +      handleCommentEvent(enhanceMessage, mockSender) | 
|  | 73 | +      expect(states.size).toBe(1) | 
|  | 74 | + | 
|  | 75 | +      // Then destroy it | 
|  | 76 | +      const destroyMessage: CommentEvent = { | 
|  | 77 | +        draft: undefined, | 
|  | 78 | +        spot: mockSpot, | 
|  | 79 | +        type: 'DESTROYED', | 
|  | 80 | +      } | 
|  | 81 | + | 
|  | 82 | +      handleCommentEvent(destroyMessage, mockSender) | 
|  | 83 | + | 
|  | 84 | +      expect(states.size).toBe(0) | 
|  | 85 | +    }) | 
|  | 86 | + | 
|  | 87 | +    it('should handle DESTROYED event for non-existent state gracefully', () => { | 
|  | 88 | +      const message: CommentEvent = { | 
|  | 89 | +        draft: undefined, | 
|  | 90 | +        spot: mockSpot, | 
|  | 91 | +        type: 'DESTROYED', | 
|  | 92 | +      } | 
|  | 93 | + | 
|  | 94 | +      // Should not throw error | 
|  | 95 | +      handleCommentEvent(message, mockSender) | 
|  | 96 | + | 
|  | 97 | +      expect(states.size).toBe(0) | 
|  | 98 | +    }) | 
|  | 99 | +  }) | 
|  | 100 | + | 
|  | 101 | +  describe('Invalid Events', () => { | 
|  | 102 | +    it('should ignore events with unsupported type', () => { | 
|  | 103 | +      const message: CommentEvent = { | 
|  | 104 | +        draft: undefined, | 
|  | 105 | +        spot: mockSpot, | 
|  | 106 | +        type: 'LOST_FOCUS', | 
|  | 107 | +      } | 
|  | 108 | + | 
|  | 109 | +      handleCommentEvent(message, mockSender) | 
|  | 110 | + | 
|  | 111 | +      expect(states.size).toBe(0) | 
|  | 112 | +    }) | 
|  | 113 | +  }) | 
|  | 114 | + | 
|  | 115 | +  describe('State Management', () => { | 
|  | 116 | +    it('should handle multiple enhanced textareas from different tabs', () => { | 
|  | 117 | +      const spot1: CommentSpot = { type: 'SPOT1', unique_key: 'key1' } | 
|  | 118 | +      const spot2: CommentSpot = { type: 'SPOT2', unique_key: 'key2' } | 
|  | 119 | + | 
|  | 120 | +      const sender1 = { tab: { id: 123, windowId: 456 } } | 
|  | 121 | +      const sender2 = { tab: { id: 789, windowId: 456 } } | 
|  | 122 | + | 
|  | 123 | +      handleCommentEvent({ draft: undefined, spot: spot1, type: 'ENHANCED' }, sender1) | 
|  | 124 | +      handleCommentEvent({ draft: undefined, spot: spot2, type: 'ENHANCED' }, sender2) | 
|  | 125 | + | 
|  | 126 | +      expect(states.size).toBe(2) | 
|  | 127 | +    }) | 
|  | 128 | + | 
|  | 129 | +    it('should handle same spot from same tab (overwrite)', () => { | 
|  | 130 | +      const message: CommentEvent = { | 
|  | 131 | +        draft: undefined, | 
|  | 132 | +        spot: mockSpot, | 
|  | 133 | +        type: 'ENHANCED', | 
|  | 134 | +      } | 
|  | 135 | + | 
|  | 136 | +      // Enhance same spot twice | 
|  | 137 | +      handleCommentEvent(message, mockSender) | 
|  | 138 | +      handleCommentEvent(message, mockSender) | 
|  | 139 | + | 
|  | 140 | +      // Should still be 1 entry (overwritten) | 
|  | 141 | +      expect(states.size).toBe(1) | 
|  | 142 | +    }) | 
|  | 143 | +  }) | 
|  | 144 | +}) | 
0 commit comments