@wtfalch/email 0.21.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,85 +0,0 @@
1
- /**
2
- * A mailbox in memory, for stories, for a development build with no server,
3
- * and for tests that want to exercise this package rather than a stub.
4
- *
5
- * ```ts
6
- * import { fakeMail } from '@wtfalch/email/mailbox/fake'
7
- *
8
- * const { client, mailbox, dispose } = fakeMail()
9
- * const tree = await mailboxes(client) // the real function
10
- * ```
11
- */
12
- import { MailClient } from "../client.js";
13
- import { FakeMailbox, } from "./mailbox.js";
14
- import { SAMPLE } from "./sample.js";
15
- export { FakeMailbox };
16
- export { SAMPLE };
17
- const ORIGIN = 'https://email.example.com';
18
- /**
19
- * The interception, and why it is here rather than a `fetch` option on
20
- * `MailClient`.
21
- *
22
- * `jmap-jam` calls the bare global `fetch`, and takes no way to pass another
23
- * one. A `fetch` option would therefore have to swap the global around each
24
- * call and put it back, which is wrong under any overlap: two calls in flight
25
- * save each other's replacement as "the original" and one of them restores
26
- * the fake permanently.
27
- *
28
- * So the global is replaced once, by a function that **delegates**: anything
29
- * not addressed to the fake's origin goes to the real `fetch` untouched.
30
- * Nothing else in the application changes behaviour, which is the property
31
- * that makes replacing a global acceptable at all. A count keeps several
32
- * fakes from uninstalling each other.
33
- */
34
- let installed = null;
35
- let live = 0;
36
- function install(route) {
37
- live += 1;
38
- if (installed)
39
- return;
40
- const original = globalThis.fetch;
41
- installed = original;
42
- globalThis.fetch = ((input, init) => {
43
- const answered = route(input, init);
44
- return answered ?? original(input, init);
45
- });
46
- }
47
- function uninstall() {
48
- live -= 1;
49
- if (live > 0 || !installed)
50
- return;
51
- globalThis.fetch = installed;
52
- installed = null;
53
- }
54
- /**
55
- * A client and the mailbox it talks to.
56
- *
57
- * Seeded with `SAMPLE` unless told otherwise, because a fake with an empty
58
- * inbox demonstrates the empty state and nothing else.
59
- */
60
- export function fakeMail(options = {}) {
61
- const mailbox = new FakeMailbox({ threads: SAMPLE, ...options });
62
- install((input, init) => {
63
- const url = String(typeof input === 'object' && input !== null && 'url' in input
64
- ? input.url
65
- : input);
66
- if (!url.startsWith(ORIGIN))
67
- return null;
68
- return mailbox.fetch(url, init);
69
- });
70
- const client = new MailClient({
71
- sessionUrl: `${ORIGIN}/.well-known/jmap`,
72
- bearerToken: 'fake',
73
- });
74
- let disposed = false;
75
- return {
76
- client,
77
- mailbox,
78
- dispose: () => {
79
- if (disposed)
80
- return;
81
- disposed = true;
82
- uninstall();
83
- },
84
- };
85
- }
@@ -1,90 +0,0 @@
1
- /**
2
- * A mailbox in memory, and the JMAP that answers questions about it.
3
- *
4
- * **A fake transport, not a fake API.** The obvious shortcut is to stub the
5
- * exported functions -- `mailboxes()` returns three objects, `listThreads()`
6
- * returns five rows -- and it is the wrong one: every story then exercises the
7
- * stub, and the four-call chain, the result references, the body-value
8
- * resolution and the null-handling that make up most of this package are
9
- * never run. This answers JMAP instead, so a component drawn against the fake
10
- * is drawn through exactly the code that will face a real server.
11
- *
12
- * It implements only the methods this package sends. Anything else answers
13
- * `unknownMethod`, which is what a real server does and what the error path
14
- * expects.
15
- */
16
- import type { EmailAddress } from '../types.ts';
17
- export type SeedMessage = {
18
- from: EmailAddress;
19
- to?: readonly EmailAddress[];
20
- subject: string;
21
- text: string;
22
- html?: string;
23
- /** Minutes before "now". Later numbers are older. */
24
- agoMinutes: number;
25
- seen?: boolean;
26
- flagged?: boolean;
27
- attachments?: {
28
- name: string;
29
- type: string;
30
- size: number;
31
- }[];
32
- };
33
- export type SeedThread = {
34
- mailbox: string;
35
- messages: SeedMessage[];
36
- };
37
- export type FakeOptions = {
38
- /** The signed-in person. */
39
- me?: EmailAddress;
40
- threads?: SeedThread[];
41
- /** Fixed "now", so a story's timestamps do not move between runs. */
42
- now?: Date;
43
- /** Milliseconds of delay on every request, for looking at a loading state. */
44
- latency?: number;
45
- /** The session's `maxSizeUpload`. Stalwart's own default when not given. */
46
- maxSizeUpload?: number;
47
- /** The directory `Principal/query` searches: colleagues with a mail
48
- * account of their own. Empty when not given. */
49
- principals?: SeedPrincipal[];
50
- };
51
- export type SeedPrincipal = {
52
- id: string;
53
- name: string;
54
- email: string;
55
- /** The account a share would be granted to. Empty: no mail account. */
56
- accountId: string;
57
- type?: string;
58
- };
59
- export declare class FakeMailbox {
60
- #private;
61
- readonly me: EmailAddress;
62
- /** Everything `EmailSubmission/set` accepted, for a test that asserts a
63
- * message was actually sent rather than only composed. */
64
- readonly submissions: {
65
- emailId: string;
66
- identityId: string;
67
- }[];
68
- /** Every blob uploaded, for a test that asserts the bytes reached the
69
- * server rather than only the composer. */
70
- readonly uploads: {
71
- blobId: string;
72
- type: string;
73
- size: number;
74
- }[];
75
- constructor(options?: FakeOptions);
76
- /** Move the fake's clock forward by `ms` milliseconds. The only way `#now`
77
- * changes after construction, so a test can hold a submission, advance
78
- * past its `sendAt`, and see the cancel window close -- deterministically,
79
- * with no real wait and no wall-clock read. */
80
- advance(ms: number): void;
81
- /** Put a thread in the mailbox. Returns its id. */
82
- add(seed: SeedThread): string;
83
- /** The session document, as Stalwart writes it. */
84
- session(origin?: string): Record<string, unknown>;
85
- /** Answer one method call. Exported for the transport, and for a test that
86
- * wants to assert on a single call's arguments. */
87
- call(method: string, args: Record<string, unknown>): Record<string, unknown>;
88
- /** A `fetch` that answers this mailbox. Hand it to `MailClient`. */
89
- fetch: (input: unknown, init?: RequestInit) => Promise<Response>;
90
- }