Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116import { beforeEach, expect, it, vi } from 'vitest';vi.mock('../jmap-client', () => ({ jmapClient: { comailAccount: { inboxUrl: 'https://inbox.comail.at', did: 'did:plc:owner', groupwareEnabled: true, sessionToken: 'synthetic-native-session', }, accountId: 'mail', authHeader: 'Basic synthetic', request: vi.fn(), },}));vi.mock('../comail', () => ({ comailFetch: vi.fn() }));import { jmapClient } from '../jmap-client';import { comailFetch } from '../comail';import { createAddressBook, getAddressBooks, getContacts, getAllContacts } from '../contacts';const request = vi.mocked(comailFetch);const id = 'urn:uuid:11111111-1111-4111-8111-111111111111';beforeEach(() => { vi.resetAllMocks(); Object.assign(jmapClient, { comailAccount: { ...jmapClient.comailAccount } });});
it('loads the canonical PDS address book into the existing native contact screens', async () => { request.mockResolvedValue( new Response( JSON.stringify({ actorDID: 'did:plc:owner', addressBookId: 'personal', state: 'revision-1', contacts: [ { id, card: { '@type': 'Card', version: '1.0', uid: id, name: { full: 'Ada' }, }, }, ], }), ), ); const books = await getAddressBooks(); expect(books[0].id).toBe('personal'); request.mockResolvedValue( new Response( JSON.stringify({ actorDID: 'did:plc:owner', addressBookId: 'personal', state: 'revision-1', contacts: [ { id, card: { '@type': 'Card', version: '1.0', uid: id, name: { full: 'Ada' }, }, }, ], }), ), ); expect((await getContacts([id]))[0].name?.full).toBe('Ada'); expect(jmapClient.request).not.toHaveBeenCalled();});
it('keeps configured-provider errors and unsupported mutations out of Stalwart', async () => { request.mockRejectedValue(new Error('PDS unavailable')); await expect(getAddressBooks()).rejects.toThrow('PDS unavailable'); await expect(createAddressBook('other')).rejects.toThrow(); expect(jmapClient.request).not.toHaveBeenCalled();});
function book() { return { actorDID: 'did:plc:owner', addressBookId: 'personal', state: 'revision-1', contacts: [ { id, card: { '@type': 'Card', version: '1.0', uid: id, name: { full: 'Ada' } } }, ] };}
it('loads all Comail contacts in one read instead of downloading the book for query and get', async () => { request.mockImplementation(async () => new Response(JSON.stringify(book()))); const cards = await getAllContacts(); expect(cards[0].name?.full).toBe('Ada'); expect(request).toHaveBeenCalledTimes(1);});
it('shares the concurrent book and contact load but keeps a later refresh fresh', async () => { let release!: (response: Response) => void; request.mockImplementationOnce(() => new Promise<Response>(resolve => { release = resolve; })); const books = getAddressBooks(); const contacts = getAllContacts(); expect(request).toHaveBeenCalledTimes(1); release(new Response(JSON.stringify(book()))); await Promise.all([books, contacts]); request.mockImplementation(async () => new Response(JSON.stringify({ ...book(), state: 'revision-2', contacts: [] }))); expect(await getAllContacts()).toEqual([]); expect(request).toHaveBeenCalledTimes(2);});
it('releases a failed shared Contacts read so the next refresh can recover', async () => { request.mockRejectedValueOnce(new Error('Access denied')); const results = await Promise.allSettled([getAddressBooks(), getAllContacts()]); expect(results.map(result => result.status)).toEqual(['rejected', 'rejected']); expect(request).toHaveBeenCalledTimes(1); request.mockResolvedValueOnce(new Response(JSON.stringify(book()))); expect((await getAllContacts())[0].name?.full).toBe('Ada'); expect(request).toHaveBeenCalledTimes(2); expect(jmapClient.request).not.toHaveBeenCalled();});