|
| 1 | +/** |
| 2 | + * archiveApi integration tests — verifies the archive API module boundary. |
| 3 | + * |
| 4 | + * No archiveStore exists; the archiveApi is consumed directly by ArchiveView. |
| 5 | + * These tests exercise the archiveApi → http chain including error handling, |
| 6 | + * query parameter construction, and response shape validation. |
| 7 | + */ |
| 8 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | +import http from '../../api/http' |
| 10 | +import { archiveApi } from '../../api/archiveApi' |
| 11 | +import type { ArchiveItem, RestoreArchiveResult } from '../../types/archive' |
| 12 | + |
| 13 | +vi.mock('../../api/http', () => ({ |
| 14 | + default: { |
| 15 | + get: vi.fn(), |
| 16 | + post: vi.fn(), |
| 17 | + put: vi.fn(), |
| 18 | + patch: vi.fn(), |
| 19 | + delete: vi.fn(), |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +function makeArchiveItem(overrides: Partial<ArchiveItem> = {}): ArchiveItem { |
| 24 | + return { |
| 25 | + id: 'arch-1', |
| 26 | + entityType: 'card', |
| 27 | + entityId: 'card-1', |
| 28 | + boardId: 'board-1', |
| 29 | + name: 'Archived Card', |
| 30 | + archivedByUserId: 'user-1', |
| 31 | + archivedAt: '2026-01-01T00:00:00Z', |
| 32 | + reason: null, |
| 33 | + restoreStatus: 'Available', |
| 34 | + restoredAt: null, |
| 35 | + restoredByUserId: null, |
| 36 | + createdAt: '2026-01-01T00:00:00Z', |
| 37 | + updatedAt: '2026-01-01T00:00:00Z', |
| 38 | + ...overrides, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +describe('archiveApi — integration (mocked HTTP)', () => { |
| 43 | + beforeEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + }) |
| 46 | + |
| 47 | + // ── getItems ────────────────────────────────────────────────────────────── |
| 48 | + |
| 49 | + describe('getItems', () => { |
| 50 | + it('calls GET /archive/items and returns the response array', async () => { |
| 51 | + const items = [makeArchiveItem(), makeArchiveItem({ id: 'arch-2', name: 'Second' })] |
| 52 | + vi.mocked(http.get).mockResolvedValue({ data: items }) |
| 53 | + |
| 54 | + const result = await archiveApi.getItems() |
| 55 | + |
| 56 | + expect(result).toHaveLength(2) |
| 57 | + expect(result[0].id).toBe('arch-1') |
| 58 | + expect(result[1].id).toBe('arch-2') |
| 59 | + expect(http.get).toHaveBeenCalledWith('/archive/items') |
| 60 | + }) |
| 61 | + |
| 62 | + it('appends entityType filter to the query string', async () => { |
| 63 | + vi.mocked(http.get).mockResolvedValue({ data: [] }) |
| 64 | + |
| 65 | + await archiveApi.getItems({ entityType: 'card' }) |
| 66 | + |
| 67 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('entityType=card')) |
| 68 | + }) |
| 69 | + |
| 70 | + it('appends boardId filter to the query string', async () => { |
| 71 | + vi.mocked(http.get).mockResolvedValue({ data: [] }) |
| 72 | + |
| 73 | + await archiveApi.getItems({ boardId: 'board-xyz' }) |
| 74 | + |
| 75 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('boardId=board-xyz')) |
| 76 | + }) |
| 77 | + |
| 78 | + it('appends status filter to the query string', async () => { |
| 79 | + vi.mocked(http.get).mockResolvedValue({ data: [] }) |
| 80 | + |
| 81 | + await archiveApi.getItems({ status: 'Available' }) |
| 82 | + |
| 83 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('status=Available')) |
| 84 | + }) |
| 85 | + |
| 86 | + it('combines multiple filters in the query string', async () => { |
| 87 | + vi.mocked(http.get).mockResolvedValue({ data: [] }) |
| 88 | + |
| 89 | + await archiveApi.getItems({ entityType: 'card', boardId: 'board-1', limit: 50 }) |
| 90 | + |
| 91 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('entityType=card')) |
| 92 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('boardId=board-1')) |
| 93 | + expect(http.get).toHaveBeenCalledWith(expect.stringContaining('limit=50')) |
| 94 | + }) |
| 95 | + |
| 96 | + it('propagates errors from the HTTP layer', async () => { |
| 97 | + vi.mocked(http.get).mockRejectedValue(new Error('Network Error')) |
| 98 | + |
| 99 | + await expect(archiveApi.getItems()).rejects.toThrow('Network Error') |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + // ── restoreItem ─────────────────────────────────────────────────────────── |
| 104 | + |
| 105 | + describe('restoreItem', () => { |
| 106 | + it('posts to /archive/:entityType/:entityId/restore and returns the result', async () => { |
| 107 | + const result: RestoreArchiveResult = { |
| 108 | + success: true, |
| 109 | + restoredEntityId: 'card-restored', |
| 110 | + errorMessage: null, |
| 111 | + resolvedName: 'My Card', |
| 112 | + } |
| 113 | + vi.mocked(http.post).mockResolvedValue({ data: result }) |
| 114 | + |
| 115 | + const response = await archiveApi.restoreItem('card', 'card-1', { |
| 116 | + targetBoardId: 'board-1', |
| 117 | + restoreMode: 0, |
| 118 | + conflictStrategy: 0, |
| 119 | + }) |
| 120 | + |
| 121 | + expect(response.success).toBe(true) |
| 122 | + expect(response.restoredEntityId).toBe('card-restored') |
| 123 | + expect(http.post).toHaveBeenCalledWith( |
| 124 | + '/archive/card/card-1/restore', |
| 125 | + expect.objectContaining({ targetBoardId: 'board-1' }), |
| 126 | + ) |
| 127 | + }) |
| 128 | + |
| 129 | + it('URL-encodes special characters in entityType and entityId', async () => { |
| 130 | + vi.mocked(http.post).mockResolvedValue({ |
| 131 | + data: { success: true, restoredEntityId: null, errorMessage: null, resolvedName: null }, |
| 132 | + }) |
| 133 | + |
| 134 | + await archiveApi.restoreItem('card/type', 'id+special', { |
| 135 | + targetBoardId: null, |
| 136 | + restoreMode: 0, |
| 137 | + conflictStrategy: 0, |
| 138 | + }) |
| 139 | + |
| 140 | + expect(http.post).toHaveBeenCalledWith( |
| 141 | + expect.stringContaining('card%2Ftype'), |
| 142 | + expect.any(Object), |
| 143 | + ) |
| 144 | + expect(http.post).toHaveBeenCalledWith( |
| 145 | + expect.stringContaining('id%2Bspecial'), |
| 146 | + expect.any(Object), |
| 147 | + ) |
| 148 | + }) |
| 149 | + |
| 150 | + it('returns failure result when the backend rejects the restore', async () => { |
| 151 | + const failResult: RestoreArchiveResult = { |
| 152 | + success: false, |
| 153 | + restoredEntityId: null, |
| 154 | + errorMessage: 'Board no longer exists', |
| 155 | + resolvedName: null, |
| 156 | + } |
| 157 | + vi.mocked(http.post).mockResolvedValue({ data: failResult }) |
| 158 | + |
| 159 | + const response = await archiveApi.restoreItem('card', 'card-1', { |
| 160 | + targetBoardId: 'deleted-board', |
| 161 | + restoreMode: 0, |
| 162 | + conflictStrategy: 0, |
| 163 | + }) |
| 164 | + |
| 165 | + expect(response.success).toBe(false) |
| 166 | + expect(response.errorMessage).toBe('Board no longer exists') |
| 167 | + }) |
| 168 | + |
| 169 | + it('propagates HTTP errors from the restore endpoint', async () => { |
| 170 | + vi.mocked(http.post).mockRejectedValue({ |
| 171 | + response: { status: 404, data: { message: 'Archive item not found' } }, |
| 172 | + }) |
| 173 | + |
| 174 | + await expect( |
| 175 | + archiveApi.restoreItem('card', 'missing', { |
| 176 | + targetBoardId: null, |
| 177 | + restoreMode: 0, |
| 178 | + conflictStrategy: 0, |
| 179 | + }), |
| 180 | + ).rejects.toBeDefined() |
| 181 | + }) |
| 182 | + |
| 183 | + it('propagates 409 Conflict when restoring with name collision', async () => { |
| 184 | + vi.mocked(http.post).mockRejectedValue({ |
| 185 | + response: { status: 409, data: { message: 'Name conflict' } }, |
| 186 | + }) |
| 187 | + |
| 188 | + await expect( |
| 189 | + archiveApi.restoreItem('card', 'card-1', { |
| 190 | + targetBoardId: 'board-1', |
| 191 | + restoreMode: 0, |
| 192 | + conflictStrategy: 0, |
| 193 | + }), |
| 194 | + ).rejects.toBeDefined() |
| 195 | + }) |
| 196 | + }) |
| 197 | +}) |
0 commit comments