fix(interviews): distinguish linked CV loading failure and empty state

This commit is contained in:
cesnimda
2026-11-17 18:49:46 +01:00
parent 06d92b3827
commit bf9b6d5e84
2 changed files with 71 additions and 5 deletions
@@ -216,6 +216,9 @@ function InterviewAiAssistant({ jobId, onAccepted }: { jobId: number; onAccepted
const { canUseAi } = useAccountPlan();
const { language: uiLanguage, t } = useI18n();
const [cv, setCv] = useState<ApplicationCv | null>(null);
const [cvLoading, setCvLoading] = useState(true);
const [cvError, setCvError] = useState<{ cause: unknown } | null>(null);
const [cvReadAttempt, setCvReadAttempt] = useState(0);
const [focus, setFocus] = useState<(typeof INTERVIEW_AI_FOCUS)[number]["key"]>("full");
const [language, setLanguage] = useState<"en" | "nb-NO">(() => uiLanguage === "nb" ? "nb-NO" : "en");
const [instructions, setInstructions] = useState("");
@@ -225,11 +228,13 @@ function InterviewAiAssistant({ jobId, onAccepted }: { jobId: number; onAccepted
useEffect(() => {
let active = true;
setCv(null); setCvLoading(true); setCvError(null);
applicationAssetsApi.cv(jobId)
.then((result) => { if (active) setCv(result); })
.catch(() => { if (active) setCv(null); });
.catch((cause) => { if (active) setCvError({ cause }); })
.finally(() => { if (active) setCvLoading(false); });
return () => { active = false; };
}, [jobId]);
}, [jobId, cvReadAttempt]);
const selected = INTERVIEW_AI_FOCUS.find((item) => item.key === focus) ?? INTERVIEW_AI_FOCUS[0];
const generate = async () => {
@@ -274,15 +279,16 @@ function InterviewAiAssistant({ jobId, onAccepted }: { jobId: number; onAccepted
}
};
const hasCv = !!cv?.attachedVariantId;
const hasCv = !cvLoading && !cvError && !!cv?.attachedVariantId;
return (
<Shell title={t("interviewAiTitle")} subtitle={t("interviewAiSubtitle")} loading={false} error={null}>
<Stack spacing={2}>
{hasCv ? (
<ViewStateNotice title={t("assetsLoadFailed")} loading={cvLoading} error={cvError ? { kind: "error", message: getApiErrorMessage(cvError.cause, t("assetsLoadFailed")), retryable: true } : null} onRetry={() => setCvReadAttempt(attempt => attempt + 1)} compact />
{!cvLoading && !cvError && (hasCv ? (
<Alert severity="success" variant="outlined">{t("interviewAiUsing")} <strong>{cv?.attachedVariantName}</strong></Alert>
) : (
<Alert severity="info">{t("interviewAiSelectCv")}</Alert>
)}
))}
<Stack direction={{ xs: "column", sm: "row" }} spacing={1.5}>
<TextField select size="small" label={t("interviewAiFocus")} value={focus} onChange={(event) => setFocus(event.target.value as typeof focus)} sx={{ minWidth: 220 }}>
{INTERVIEW_AI_FOCUS.map((item) => <MenuItem key={item.key} value={item.key}>{t(item.labelKey)}</MenuItem>)}
@@ -318,6 +318,66 @@ test("AI preparation uses the linked CV and only saves after explicit acceptance
));
});
test.each(['en', 'nb'] as const)('pending linked CV is loading, not an empty selection (%s)', async language => {
localStorage.setItem('uiLanguage', language);
mockedApi.get.mockReset(); routeGet();
const read = mockedApi.get.getMockImplementation()!;
let finish!: (value: any) => void;
const pending = new Promise(resolve => { finish = resolve; });
mockedApi.get.mockImplementation((url: string) => url.endsWith('/cv') ? pending as any : read(url));
render(<ApplicationInterviewPrep jobId={7} />);
const assistant = within(screen.getByRole('region', { name: translations[language].interviewAiTitle }));
expect(assistant.getByRole('progressbar', { name: translations[language].loading })).toBeVisible();
expect(assistant.queryByText(translations[language].interviewAiSelectCv)).not.toBeInTheDocument();
expect(assistant.getByRole('button', { name: translations[language].interviewAiGenerate })).toBeDisabled();
await act(async () => finish({ data: { attachedVariantId: null, attachedVariantName: null } }));
expect(await assistant.findByText(translations[language].interviewAiSelectCv)).toBeVisible();
expect(assistant.queryByRole('progressbar')).not.toBeInTheDocument();
expect(mockedApi.post).not.toHaveBeenCalled();
});
test.each(['en', 'nb'] as const)('linked-CV failure has current-language GET-only retry and preserves instructions (%s)', async from => {
localStorage.setItem('uiLanguage', from);
const to = from === 'en' ? 'nb' : 'en';
mockedApi.get.mockReset(); routeGet();
const read = mockedApi.get.getMockImplementation()!;
let cvReads = 0;
mockedApi.get.mockImplementation((url: string) => url.endsWith('/cv') && ++cvReads === 1 ? Promise.reject(new Error('synthetic linked-CV failure')) : read(url));
render(<ApplicationInterviewPrep jobId={7} />);
const region = screen.getByRole('region', { name: translations[from].interviewAiTitle });
const assistant = within(region);
expect(await assistant.findByText(translations[from].assetsLoadFailed)).toBeVisible();
expect(assistant.queryByText(translations[from].interviewAiSelectCv)).not.toBeInTheDocument();
fireEvent.change(assistant.getByLabelText(translations[from].coverAiAdditionalInstructions), { target: { value: 'Keep my context Øresund' } });
await act(async () => window.dispatchEvent(new StorageEvent('storage', { key: 'uiLanguage', newValue: to })));
expect(assistant.getByText(translations[to].assetsLoadFailed)).toBeVisible();
expect(cvReads).toBe(1);
expect(assistant.getByRole('combobox', { name: translations[to].coverAiDocumentLanguage })).toHaveTextContent(translations[to][from === 'en' ? 'languageEnglish' : 'languageNorwegianBokmal']);
fireEvent.click(assistant.getByRole('button', { name: translations[to].retry }));
expect(await assistant.findByText('Backend CV')).toBeVisible();
expect(cvReads).toBe(2);
expect(assistant.getByLabelText(translations[to].coverAiAdditionalInstructions)).toHaveValue('Keep my context Øresund');
expect(mockedApi.post).not.toHaveBeenCalled(); expect(mockedApi.patch).not.toHaveBeenCalled(); expect(mockedApi.put).not.toHaveBeenCalled();
});
test.each(['en', 'nb'] as const)('changing application hides the previous linked CV while the new read is pending (%s)', async language => {
localStorage.setItem('uiLanguage', language);
mockedApi.get.mockReset(); routeGet();
const read = mockedApi.get.getMockImplementation()!;
let finish!: (value: any) => void;
const pending = new Promise(resolve => { finish = resolve; });
mockedApi.get.mockImplementation((url: string) => url === '/jobapplications/8/cv' ? pending as any : read(url));
const view = render(<ApplicationInterviewPrep jobId={7} />);
await screen.findByText('Backend CV');
view.rerender(<I18nProvider><ConfirmProvider><ApplicationInterviewPrep jobId={8} /></ConfirmProvider></I18nProvider>);
const assistant = within(screen.getByRole('region', { name: translations[language].interviewAiTitle }));
expect(assistant.queryByText('Backend CV')).not.toBeInTheDocument();
expect(assistant.getByRole('progressbar', { name: translations[language].loading })).toBeVisible();
await act(async () => finish({ data: { attachedVariantId: 8, attachedVariantName: 'Current application CV' } }));
expect(await assistant.findByText('Current application CV')).toBeVisible();
expect(mockedApi.post).not.toHaveBeenCalled();
});
test("prep surfaces a load error", async () => {
mockedApi.get.mockRejectedValue(new Error("boom"));