|
| 1 | +import { describe, it, beforeEach, afterEach, expect, vi } from 'vitest'; |
| 2 | +import { shallowMount, type VueWrapper } from '@vue/test-utils'; |
| 3 | +import { createPinia, setActivePinia } from 'pinia'; |
| 4 | +import { createApp } from 'vue'; |
| 5 | + |
| 6 | +import type { JobHistoryDetail } from '@/backend-contract'; |
| 7 | +import { CorePiniaProviderPlugin } from '@/src/core/provider'; |
| 8 | +import { defer } from '@/src/utils'; |
| 9 | +import type { |
| 10 | + ProcessingJobStatus, |
| 11 | + ProcessingProvider, |
| 12 | +} from '@/src/processing/types'; |
| 13 | +import { |
| 14 | + jobStatus, |
| 15 | + makeFakeProvider, |
| 16 | + registerFake, |
| 17 | + type FakeProvider, |
| 18 | +} from '@/src/processing/__tests__/fakeProvider'; |
| 19 | +import JobList from '@/src/processing/components/JobList.vue'; |
| 20 | +import { |
| 21 | + POLL_INTERVAL_MS, |
| 22 | + useProcessingJobsStore, |
| 23 | +} from '@/src/processing/store'; |
| 24 | + |
| 25 | +const JOB_ID = 'job-1'; |
| 26 | +const PARTIAL_LOG = [ |
| 27 | + 'Traceback (most recent call last):\n', |
| 28 | + ' File "/opt/cli/volview_cli_base/cli.py", line 25, in run\n', |
| 29 | +]; |
| 30 | +const FINAL_LINE = 'ValueError: Lower threshold must be <= upper threshold'; |
| 31 | +const FULL_LOG = [...PARTIAL_LOG, `${FINAL_LINE}\n`]; |
| 32 | + |
| 33 | +const detail = (log: string[]): JobHistoryDetail => ({ |
| 34 | + jobId: JOB_ID, |
| 35 | + log, |
| 36 | + parameters: {}, |
| 37 | +}); |
| 38 | + |
| 39 | +const failed = jobStatus(JOB_ID, 'error', { |
| 40 | + errorTail: 'DockerException: exit code 1', |
| 41 | +}); |
| 42 | + |
| 43 | +const makeProvider = ( |
| 44 | + overrides: Partial<ProcessingProvider> = {} |
| 45 | +): FakeProvider => |
| 46 | + makeFakeProvider( |
| 47 | + { |
| 48 | + id: 'p1', |
| 49 | + label: 'P1', |
| 50 | + baseUrl: 'http://p1/', |
| 51 | + jobsBaseUrl: 'http://p1/jobs', |
| 52 | + }, |
| 53 | + { |
| 54 | + runTask: vi.fn().mockResolvedValue({ jobId: JOB_ID }), |
| 55 | + getJob: vi |
| 56 | + .fn() |
| 57 | + .mockResolvedValueOnce(jobStatus(JOB_ID, 'running')) |
| 58 | + .mockResolvedValue(failed), |
| 59 | + ...overrides, |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | +// Clicks reach toggleDetails through the attribute fallthrough on <button>. |
| 64 | +const btnStub = { template: '<button><slot /></button>' }; |
| 65 | +const slotStub = { template: '<div><slot /></div>' }; |
| 66 | + |
| 67 | +describe('JobList error log', () => { |
| 68 | + let pinia: ReturnType<typeof createPinia>; |
| 69 | + let wrapper: VueWrapper | undefined; |
| 70 | + |
| 71 | + beforeEach(() => { |
| 72 | + vi.useFakeTimers(); |
| 73 | + pinia = createPinia().use(CorePiniaProviderPlugin()); |
| 74 | + createApp({}).use(pinia); |
| 75 | + setActivePinia(pinia); |
| 76 | + }); |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + wrapper?.unmount(); |
| 80 | + wrapper = undefined; |
| 81 | + vi.useRealTimers(); |
| 82 | + }); |
| 83 | + |
| 84 | + // A bound base image keeps the completion path from fetching details to |
| 85 | + // rebuild an adopted job's parent, so every detail request comes from the row. |
| 86 | + const submit = async (provider: FakeProvider) => { |
| 87 | + const store = useProcessingJobsStore(); |
| 88 | + registerFake(store, provider); |
| 89 | + await store.submitJob( |
| 90 | + provider.config.id, |
| 91 | + 'threshold', |
| 92 | + {}, |
| 93 | + { |
| 94 | + activeDatasetId: 'base', |
| 95 | + } |
| 96 | + ); |
| 97 | + await vi.advanceTimersByTimeAsync(0); |
| 98 | + }; |
| 99 | + |
| 100 | + const mountList = () => { |
| 101 | + wrapper = shallowMount(JobList, { |
| 102 | + global: { |
| 103 | + plugins: [pinia], |
| 104 | + stubs: { |
| 105 | + 'v-btn': btnStub, |
| 106 | + 'expand-transition': slotStub, |
| 107 | + VSelect: { template: '<div />' }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }); |
| 111 | + return wrapper; |
| 112 | + }; |
| 113 | + |
| 114 | + const expandDetails = async (list: VueWrapper) => { |
| 115 | + await list.find('.details-btn').trigger('click'); |
| 116 | + await vi.advanceTimersByTimeAsync(0); |
| 117 | + }; |
| 118 | + |
| 119 | + it('shows the final log when a job fails after its details were opened', async () => { |
| 120 | + const provider = makeProvider({ |
| 121 | + getJobHistoryDetail: vi |
| 122 | + .fn() |
| 123 | + .mockResolvedValueOnce(detail(PARTIAL_LOG)) |
| 124 | + .mockResolvedValue(detail(FULL_LOG)), |
| 125 | + }); |
| 126 | + await submit(provider); |
| 127 | + const list = mountList(); |
| 128 | + |
| 129 | + await expandDetails(list); |
| 130 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 131 | + |
| 132 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 133 | + expect(provider.getJobHistoryDetail).toHaveBeenCalledTimes(2); |
| 134 | + }); |
| 135 | + |
| 136 | + it('keeps the final log when the earlier detail response arrives last', async () => { |
| 137 | + const partial = defer<JobHistoryDetail>(); |
| 138 | + const provider = makeProvider({ |
| 139 | + getJobHistoryDetail: vi |
| 140 | + .fn() |
| 141 | + .mockReturnValueOnce(partial.promise) |
| 142 | + .mockResolvedValue(detail(FULL_LOG)), |
| 143 | + }); |
| 144 | + await submit(provider); |
| 145 | + const list = mountList(); |
| 146 | + |
| 147 | + await expandDetails(list); |
| 148 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS); |
| 149 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 150 | + |
| 151 | + partial.resolve(detail(PARTIAL_LOG)); |
| 152 | + await vi.advanceTimersByTimeAsync(0); |
| 153 | + |
| 154 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 155 | + }); |
| 156 | + |
| 157 | + // Adoption completes a job it read once, without registering a poller. |
| 158 | + it('keeps the final log when an adopted job fails while its earlier detail is in flight', async () => { |
| 159 | + const partial = defer<JobHistoryDetail>(); |
| 160 | + const adoptedStatus = defer<ProcessingJobStatus>(); |
| 161 | + const provider = makeProvider({ |
| 162 | + listJobHistory: vi.fn().mockResolvedValue({ |
| 163 | + jobs: [ |
| 164 | + { |
| 165 | + jobId: JOB_ID, |
| 166 | + taskId: 'threshold', |
| 167 | + taskTitle: 'Threshold', |
| 168 | + createdBy: { id: 'u1', name: 'User One' }, |
| 169 | + createdAt: '2026-09-21T12:00:00Z', |
| 170 | + state: 'running', |
| 171 | + resultState: 'waiting', |
| 172 | + }, |
| 173 | + ], |
| 174 | + nextCursor: null, |
| 175 | + }), |
| 176 | + getJob: vi.fn().mockReturnValue(adoptedStatus.promise), |
| 177 | + getJobHistoryDetail: vi |
| 178 | + .fn() |
| 179 | + .mockReturnValueOnce(partial.promise) |
| 180 | + .mockResolvedValue(detail(FULL_LOG)), |
| 181 | + }); |
| 182 | + const store = useProcessingJobsStore(); |
| 183 | + registerFake(store, provider); |
| 184 | + const adoption = store.adoptJobHistory(); |
| 185 | + await vi.advanceTimersByTimeAsync(0); |
| 186 | + const list = mountList(); |
| 187 | + |
| 188 | + await expandDetails(list); |
| 189 | + adoptedStatus.resolve(failed); |
| 190 | + await adoption; |
| 191 | + await vi.advanceTimersByTimeAsync(0); |
| 192 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 193 | + |
| 194 | + partial.resolve(detail(PARTIAL_LOG)); |
| 195 | + await vi.advanceTimersByTimeAsync(0); |
| 196 | + |
| 197 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 198 | + }); |
| 199 | + |
| 200 | + it('fetches the details of an already failed job once', async () => { |
| 201 | + const provider = makeProvider({ |
| 202 | + runTask: vi.fn().mockResolvedValue({ jobId: JOB_ID, status: failed }), |
| 203 | + getJobHistoryDetail: vi.fn().mockResolvedValue(detail(FULL_LOG)), |
| 204 | + }); |
| 205 | + await submit(provider); |
| 206 | + const list = mountList(); |
| 207 | + |
| 208 | + await expandDetails(list); |
| 209 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS * 5); |
| 210 | + |
| 211 | + expect(list.find('.error-log').text()).toContain(FINAL_LINE); |
| 212 | + expect(provider.getJobHistoryDetail).toHaveBeenCalledTimes(1); |
| 213 | + }); |
| 214 | + |
| 215 | + it('fetches no details when a job whose details were never opened finishes', async () => { |
| 216 | + const provider = makeProvider({ |
| 217 | + getJobHistoryDetail: vi.fn().mockResolvedValue(detail(FULL_LOG)), |
| 218 | + }); |
| 219 | + await submit(provider); |
| 220 | + const list = mountList(); |
| 221 | + |
| 222 | + await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS * 5); |
| 223 | + |
| 224 | + expect(list.find('.job-row').exists()).toBe(true); |
| 225 | + expect(provider.getJobHistoryDetail).not.toHaveBeenCalled(); |
| 226 | + }); |
| 227 | +}); |
0 commit comments