File size: 5,072 Bytes
05b4327
392f1be
 
 
 
 
 
 
 
05b4327
 
 
392f1be
05b4327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392f1be
05b4327
 
 
 
 
 
 
 
392f1be
 
 
05b4327
392f1be
05b4327
392f1be
05b4327
 
 
392f1be
05b4327
 
 
392f1be
05b4327
 
392f1be
05b4327
 
 
392f1be
05b4327
 
 
 
392f1be
05b4327
 
 
392f1be
05b4327
392f1be
05b4327
 
 
 
 
 
 
 
 
 
 
 
392f1be
05b4327
 
 
 
 
 
 
 
 
 
 
392f1be
05b4327
 
 
 
 
 
 
 
 
 
392f1be
05b4327
 
392f1be
05b4327
392f1be
05b4327
 
 
 
 
392f1be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05b4327
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { buildResultHtml, downloadResultReport, downloadPracticeResultHtml } from './reportGenerator'
import { testAPI } from '../api/api'

vi.mock('../api/api', () => ({
  testAPI: {
    downloadResultHtml: vi.fn(),
  },
}))

describe('reportGenerator', () => {
  describe('buildResultHtml', () => {
    it('generates correct HTML for a practice result object', () => {
      const mockResult = {
        test_title: 'Mock Test Title',
        attempt_number: 1,
        submitted_at: '2026-06-23T12:00:00Z',
        score: 10.5,
        total_marks: 20,
        percentage: 52.5,
        correct: 5,
        incorrect: 2,
        skipped: 3,
        answers: [
          {
            is_correct: true,
            options: ['Option A', 'Option <B>'],
            marks_awarded: 2,
            question_text: 'What is 1+1?',
            selected_answer: 'A',
            correct_answer: 'A',
            time_spent_seconds: 45,
          },
          {
            is_correct: null, // skipped
            options: [],
            marks_awarded: 0,
            question_text: 'NAT Question?',
            selected_answer: '',
            correct_answer: '42',
            time_spent_seconds: 10,
          },
        ],
      }

      const html = buildResultHtml(mockResult)

      // Basic generation checks
      expect(html).toContain('<!doctype html>')
      expect(html).toContain('<title>Mock Test Title result</title>')

      // Data and HTML escaping checks
      expect(html).toContain('Mock Test Title')
      expect(html).toContain('Option &lt;B&gt;') // Ensures escapeHtml works
      expect(html).toContain('Practice attempt')
      expect(html).toContain('What is 1+1?')
      expect(html).toContain('NAT Question?')

      // Status text
      expect(html).toContain('Correct</span>')
      expect(html).toContain('Skipped</span>')

      // Default formatting checks
      expect(html).toContain('10.50 / 20')
      expect(html).toContain('53%') // Math.round(52.5)
    })

    it('handles empty results without crashing', () => {
      const emptyResult = {}
      const html = buildResultHtml(emptyResult)

      expect(html).toContain('Practice attempt')
      expect(html).toContain('NaN%')
    })
  })

  describe('downloadResultReport', () => {
    let createObjectURLSpy
    let revokeObjectURLSpy
    let createElementSpy
    let appendChildSpy

    beforeEach(() => {
      createObjectURLSpy = vi.fn(() => 'blob:mock-url')
      revokeObjectURLSpy = vi.fn()

      global.URL.createObjectURL = createObjectURLSpy
      global.URL.revokeObjectURL = revokeObjectURLSpy

      vi.useFakeTimers()
    })

    afterEach(() => {
      vi.restoreAllMocks()
      vi.useRealTimers()
    })

    it('falls back to client-side practice report when attempt_id is missing', async () => {
      const mockLink = {
        href: '',
        download: '',
        click: vi.fn(),
        remove: vi.fn(),
      }
      createElementSpy = vi.spyOn(document, 'createElement').mockReturnValue(mockLink)
      appendChildSpy = vi.spyOn(document.body, 'appendChild').mockImplementation(() => {})

      const mockResult = {
        test_title: 'Practice Test 101',
      }

      await downloadResultReport(mockResult)

      expect(testAPI.downloadResultHtml).not.toHaveBeenCalled()
      expect(createObjectURLSpy).toHaveBeenCalledTimes(1)
      const blobArg = createObjectURLSpy.mock.calls[0][0]
      expect(blobArg).toBeInstanceOf(Blob)
      expect(blobArg.type).toBe('text/html;charset=utf-8')

      expect(mockLink.download).toBe('practice-test-101-practice-result.html')
      expect(mockLink.click).toHaveBeenCalledTimes(1)

      vi.runAllTimers()
      expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
    })

    it('downloads server-rendered HTML for persisted attempt_id', async () => {
      const mockLink = {
        href: '',
        download: '',
        click: vi.fn(),
        remove: vi.fn(),
      }
      createElementSpy = vi.spyOn(document, 'createElement').mockReturnValue(mockLink)
      appendChildSpy = vi.spyOn(document.body, 'appendChild').mockImplementation(() => {})

      const mockBlob = new Blob(['<!DOCTYPE html><html><body>Server Rendered Report</body></html>'], { type: 'text/html' })
      testAPI.downloadResultHtml.mockResolvedValueOnce({
        data: mockBlob,
      })

      const mockResult = {
        attempt_id: 42,
        test_title: 'GATE CS Mock 2026',
      }

      await downloadResultReport(mockResult)

      expect(testAPI.downloadResultHtml).toHaveBeenCalledWith(42)
      expect(createObjectURLSpy).toHaveBeenCalledTimes(1)
      const blobArg = createObjectURLSpy.mock.calls[0][0]
      expect(blobArg).toBeInstanceOf(Blob)
      expect(blobArg.type).toBe('text/html')

      expect(mockLink.download).toBe('gate-cs-mock-2026-result.html')
      expect(mockLink.click).toHaveBeenCalledTimes(1)

      vi.runAllTimers()
      expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
    })
  })
})