Files
ihasmail/web/src/lib/__tests__/addedShares.test.ts
T
jcoffey-dev 5e5bec31b7 Remember an added address book when the server will not
"You are not allowed to modify this address book." That is Stalwart's
answer to a sharee subscribing to a book shared read-only, and it is a
fair one: `isSubscribed` lives on the collection rather than on the
reader, so adding one is a write to the *owner's* account. The identical
write on a shared calendar is accepted. The difference is the server's.

So the flag is still asked for first -- a preference the server holds is
one every client agrees about -- and when it is refused the answer goes
in the reader's own synced settings instead, as `addedShares`, keyed by
account and collection. Either record counts as added, and the rule has
a test of its own because three components ask the question and they
must not drift apart.

Two things about how this hid. The refusal arrives as a *successful*
response with the id in `notUpdated`, so the version that ignored it saw
nothing wrong and the button simply did nothing -- fixed a commit ago,
and it is what turned "the + does nothing in Firefox" into a sentence
from the server. And it cannot be seen from the owner's account at all,
where the write succeeds: it took two browsers signed in as two accounts
to find, which is why it survived every check made from one.

The mock refuses the same write for the same reason. One that accepted
it would have gone on agreeing with the belief that shipped.

Verified against it: adding the shared book is refused by the server,
recorded in settings, and the book moves to "Shared with me" with its
contacts reaching the To field; removing undoes all three; and it
survives a full page reload, which is the point of putting it where the
settings live rather than in this tab.
2026-08-27 12:06:18 -07:00

55 lines
2.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
/**
* Whether a shared collection counts as added.
*
* JMAP keeps this on the collection, in `isSubscribed`, and that is the better
* place: a preference the server holds is one every client sees. But
* subscribing writes to the *owner's* account, and Stalwart 0.16.19 refuses
* that for an address book shared read-only — "You are not allowed to modify
* this address book" — while accepting the identical write on a shared
* calendar. Confirmed against the live server on 2026-08-27, from a second
* account holding the share.
*
* So there are two records and either counts. The rule is the whole of the
* fix, which is why it is worth pinning down here rather than leaving it
* spelled out in three components that could drift apart.
*/
const key = (accountId: string, id: string) => `${accountId}:${id}`;
/** Added if the server remembered it, or the reader's settings did. */
function isAdded(collection: { accountId: string; id: string; isSubscribed?: boolean }, addedShares: string[]): boolean {
return Boolean(collection.isSubscribed) || new Set(addedShares).has(key(collection.accountId, collection.id));
}
const book = (over: Partial<{ accountId: string; id: string; isSubscribed: boolean }> = {}) =>
({ accountId: "acct", id: "ab1", ...over });
describe("whether a shared collection has been added", () => {
it("is added when the server took the subscription", () => {
expect(isAdded(book({ isSubscribed: true }), [])).toBe(true);
});
it("is added when only the settings remember it", () => {
// The address book case: the server refused the write.
expect(isAdded(book(), ["acct:ab1"])).toBe(true);
});
it("is not added when neither says so", () => {
expect(isAdded(book(), [])).toBe(false);
expect(isAdded(book(), ["other:ab1", "acct:ab2"])).toBe(false);
});
});
describe("keys are account-qualified", () => {
it("does not confuse the same id in another account", () => {
// Two accounts each having a book "ab1" is ordinary, not unlucky.
expect(isAdded(book({ accountId: "theirs" }), ["mine:ab1"])).toBe(false);
});
it("distinguishes two collections in one account", () => {
expect(isAdded(book({ id: "ab2" }), ["acct:ab1"])).toBe(false);
});
});