|
| 1 | +import { afterEach, beforeEach, describe, it, expect, vi } from 'vitest'; |
| 2 | + |
| 3 | +const mockOpen = vi.fn(); |
| 4 | +const mockClose = vi.fn(); |
| 5 | + |
| 6 | +vi.mock('@nitpicker/crawler', () => ({ |
| 7 | + Archive: { |
| 8 | + open: mockOpen, |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('./debug.js', () => ({ |
| 13 | + archiveLog: vi.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +describe('getArchive', () => { |
| 17 | + beforeEach(() => { |
| 18 | + vi.clearAllMocks(); |
| 19 | + mockOpen.mockResolvedValue({ close: mockClose }); |
| 20 | + }); |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.restoreAllMocks(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('archive と removeSignalHandlers を含むオブジェクトを返す', async () => { |
| 27 | + const { getArchive } = await import('./archive.js'); |
| 28 | + const handle = await getArchive('/tmp/test.nitpicker'); |
| 29 | + |
| 30 | + expect(handle).toHaveProperty('archive'); |
| 31 | + expect(handle).toHaveProperty('removeSignalHandlers'); |
| 32 | + expect(typeof handle.removeSignalHandlers).toBe('function'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('シグナルリスナーを登録する', async () => { |
| 36 | + const { getArchive } = await import('./archive.js'); |
| 37 | + const before = process.listenerCount('SIGINT'); |
| 38 | + |
| 39 | + await getArchive('/tmp/test.nitpicker'); |
| 40 | + |
| 41 | + expect(process.listenerCount('SIGINT')).toBe(before + 1); |
| 42 | + expect(process.listenerCount('SIGHUP')).toBeGreaterThan(0); |
| 43 | + expect(process.listenerCount('SIGABRT')).toBeGreaterThan(0); |
| 44 | + }); |
| 45 | + |
| 46 | + it('removeSignalHandlers を呼ぶとシグナルリスナーが解除される', async () => { |
| 47 | + const { getArchive } = await import('./archive.js'); |
| 48 | + const before = process.listenerCount('SIGINT'); |
| 49 | + |
| 50 | + const { removeSignalHandlers } = await getArchive('/tmp/test.nitpicker'); |
| 51 | + removeSignalHandlers(); |
| 52 | + |
| 53 | + expect(process.listenerCount('SIGINT')).toBe(before); |
| 54 | + }); |
| 55 | + |
| 56 | + it('複数回呼び出してもリスナーが蓄積しない(removeSignalHandlers で解除した場合)', async () => { |
| 57 | + const { getArchive } = await import('./archive.js'); |
| 58 | + const before = process.listenerCount('SIGINT'); |
| 59 | + |
| 60 | + const handle1 = await getArchive('/tmp/test1.nitpicker'); |
| 61 | + handle1.removeSignalHandlers(); |
| 62 | + |
| 63 | + const handle2 = await getArchive('/tmp/test2.nitpicker'); |
| 64 | + handle2.removeSignalHandlers(); |
| 65 | + |
| 66 | + const handle3 = await getArchive('/tmp/test3.nitpicker'); |
| 67 | + handle3.removeSignalHandlers(); |
| 68 | + |
| 69 | + expect(process.listenerCount('SIGINT')).toBe(before); |
| 70 | + }); |
| 71 | +}); |
0 commit comments