fix(documents): preserve independent attachment metadata choices
This commit is contained in:
@@ -61,6 +61,34 @@ public sealed class AttachmentFlagsRecomputeTests
|
||||
Assert.True(updated.HasOtherAttachment);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public async Task Partial_metadata_changes_preserve_the_other_field_and_filename(bool aiFirst)
|
||||
{
|
||||
await using var db = TestHostFactory.CreateInMemoryDb();
|
||||
var (job, attachment) = await SeedJobWithAttachmentAsync(db, purpose: "resume");
|
||||
attachment.UseForAi = false;
|
||||
await db.SaveChangesAsync();
|
||||
var originalPath = attachment.FilePath;
|
||||
var controller = CreateController(db, Path.GetDirectoryName(originalPath));
|
||||
var purpose = new AttachmentsController.UpdateAttachmentRequest(null, "portfolio", null);
|
||||
var ai = new AttachmentsController.UpdateAttachmentRequest(null, null, true);
|
||||
|
||||
Assert.IsType<NoContentResult>(await controller.Rename(attachment.Id, aiFirst ? ai : purpose, CancellationToken.None));
|
||||
Assert.IsType<NoContentResult>(await controller.Rename(attachment.Id, aiFirst ? purpose : ai, CancellationToken.None));
|
||||
|
||||
db.ChangeTracker.Clear();
|
||||
var saved = await db.Attachments.SingleAsync(a => a.Id == attachment.Id);
|
||||
Assert.Equal("portfolio", saved.Purpose);
|
||||
Assert.True(saved.UseForAi);
|
||||
Assert.Equal("file.pdf", saved.FileName);
|
||||
Assert.Equal(originalPath, saved.FilePath);
|
||||
var updatedJob = await db.JobApplications.SingleAsync(j => j.Id == job.Id);
|
||||
Assert.True(updatedJob.HasPortfolio);
|
||||
Assert.False(updatedJob.HasResume);
|
||||
}
|
||||
|
||||
private static async Task<(JobApplication Job, Attachment Attachment)> SeedJobWithAttachmentAsync(JobTrackerContext db, string purpose)
|
||||
{
|
||||
var company = new Company { Name = "Acme", OwnerUserId = "user-1" };
|
||||
|
||||
@@ -77,6 +77,41 @@ async function open(action: "rename" | "delete") {
|
||||
return { view, dialog };
|
||||
}
|
||||
|
||||
test.each((['en', 'nb'] as const).flatMap(language => (['purpose first', 'AI first'] as const).map(order => [language, order] as const)))('independent attachment metadata edits preserve both server values (%s, %s)', async (language, order) => {
|
||||
localStorage.setItem('uiLanguage', language);
|
||||
const copy = translations[language];
|
||||
const persisted = { ...file, purpose: 'resume', useForAi: false };
|
||||
mockedApi.get.mockResolvedValue({ data: [{ ...persisted }] } as any);
|
||||
const responses = [deferred(), deferred()];
|
||||
let nextResponse = 0;
|
||||
mockedApi.patch.mockImplementation((_url: any, body: any) => {
|
||||
Object.assign(persisted, body);
|
||||
return responses[nextResponse++].promise;
|
||||
});
|
||||
render(page());
|
||||
const ai = await screen.findByRole('switch', { name: `${copy.attachmentsAiUse}: ${file.fileName}` });
|
||||
const purpose = screen.getByRole('combobox', { name: copy.attachmentsPurpose });
|
||||
const changePurpose = async () => {
|
||||
fireEvent.mouseDown(purpose);
|
||||
fireEvent.click(await screen.findByRole('option', { name: copy.attachmentsPurposePortfolio }));
|
||||
};
|
||||
try {
|
||||
if (order === 'purpose first') { await changePurpose(); fireEvent.click(ai); }
|
||||
else { fireEvent.click(ai); await changePurpose(); }
|
||||
expect(mockedApi.patch).toHaveBeenCalledTimes(2);
|
||||
expect(persisted).toMatchObject({ purpose: 'portfolio', useForAi: true });
|
||||
await act(async () => responses[1].resolve({ data: {} }));
|
||||
await act(async () => responses[0].resolve({ data: {} }));
|
||||
expect(ai).toBeChecked();
|
||||
expect(purpose).toHaveTextContent(copy.attachmentsPurposePortfolio);
|
||||
expect(mockedApi.get).toHaveBeenCalledTimes(1);
|
||||
expect(mockedApi.post).not.toHaveBeenCalled();
|
||||
expect(mockedApi.delete).not.toHaveBeenCalled();
|
||||
} finally {
|
||||
await act(async () => { responses.forEach(response => response.resolve({ data: {} })); });
|
||||
}
|
||||
});
|
||||
|
||||
test.each(['resolve', 'reject'] as const)('old attachment read %s cannot replace the next application', async outcome => {
|
||||
const pending = deferred();
|
||||
const current = { ...file, id: 77, fileName: 'Current application.pdf' };
|
||||
|
||||
@@ -183,13 +183,13 @@ test('attachments metadata controls update purpose and ai usage', async () => {
|
||||
fireEvent.click(await screen.findByRole('option', { name: /portfolio/i }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockedApi.patch).toHaveBeenCalledWith('/attachments/7', expect.objectContaining({ purpose: 'portfolio', useForAi: true }));
|
||||
expect(mockedApi.patch).toHaveBeenCalledWith('/attachments/7', { purpose: 'portfolio' });
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByRole('switch', { name: 'Use for AI: resume.pdf' }));
|
||||
|
||||
await waitFor(() => {
|
||||
expect(mockedApi.patch).toHaveBeenCalledWith('/attachments/7', expect.objectContaining({ purpose: 'portfolio', useForAi: false }));
|
||||
expect(mockedApi.patch).toHaveBeenCalledWith('/attachments/7', { useForAi: false });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -202,7 +202,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
||||
const active = captureAction();
|
||||
if (!active) return;
|
||||
try {
|
||||
await api.patch(`/attachments/${a.id}`, { purpose: patch.purpose ?? a.purpose ?? guessKind(a.fileName), useForAi: patch.useForAi ?? a.useForAi });
|
||||
await api.patch(`/attachments/${a.id}`, patch);
|
||||
if (!active()) return;
|
||||
setItems((current) => current.map((item) => item.id === a.id ? { ...item, ...patch } : item));
|
||||
toast(currentT.current("attachmentsUpdated"), "success");
|
||||
|
||||
Reference in New Issue
Block a user