@mailpier/inbox 0.0.0-stage → 0.1.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.
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/chunk-FR6JEAV7.js +317 -0
- package/dist/client-CqysQIQJ.d.cts +194 -0
- package/dist/client-CqysQIQJ.d.ts +194 -0
- package/dist/cypress/commands.cjs +79 -0
- package/dist/cypress/commands.d.cts +48 -0
- package/dist/cypress/commands.d.ts +48 -0
- package/dist/cypress/commands.js +53 -0
- package/dist/cypress/index.cjs +358 -0
- package/dist/cypress/index.d.cts +32 -0
- package/dist/cypress/index.d.ts +32 -0
- package/dist/cypress/index.js +27 -0
- package/dist/index.cjs +349 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +18 -0
- package/dist/playwright.cjs +410 -0
- package/dist/playwright.d.cts +50 -0
- package/dist/playwright.d.ts +50 -0
- package/dist/playwright.js +78 -0
- package/dist/testing/index.cjs +350 -0
- package/dist/testing/index.d.cts +75 -0
- package/dist/testing/index.d.ts +75 -0
- package/dist/testing/index.js +321 -0
- package/package.json +107 -4
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
/** A mailbox from a header: display name (may be null) and address. */
|
|
2
|
+
interface InboxAddress {
|
|
3
|
+
name?: string | null;
|
|
4
|
+
address: string;
|
|
5
|
+
}
|
|
6
|
+
/** The parsed headers (what the sender wrote). */
|
|
7
|
+
interface InboxHeaders {
|
|
8
|
+
from?: InboxAddress | null;
|
|
9
|
+
to?: InboxAddress[];
|
|
10
|
+
cc?: InboxAddress[];
|
|
11
|
+
replyTo?: InboxAddress | null;
|
|
12
|
+
subject?: string | null;
|
|
13
|
+
/** The Date header (ISO 8601); null when missing or unreadable. */
|
|
14
|
+
date?: string | null;
|
|
15
|
+
messageId?: string | null;
|
|
16
|
+
}
|
|
17
|
+
interface InboxLink {
|
|
18
|
+
href: string;
|
|
19
|
+
text?: string | null;
|
|
20
|
+
}
|
|
21
|
+
interface InboxAttachment {
|
|
22
|
+
index: number;
|
|
23
|
+
fileName?: string | null;
|
|
24
|
+
contentType?: string | null;
|
|
25
|
+
size: number;
|
|
26
|
+
}
|
|
27
|
+
/** A mail as the list call returns it. `to` holds the environment's own (envelope) recipients. */
|
|
28
|
+
interface InboxMessageSummary {
|
|
29
|
+
id: string;
|
|
30
|
+
/** ISO 8601, UTC. */
|
|
31
|
+
receivedUtc: string;
|
|
32
|
+
from?: string | null;
|
|
33
|
+
to: string[];
|
|
34
|
+
subject?: string | null;
|
|
35
|
+
sizeBytes: number;
|
|
36
|
+
/** Exact up to 16 KiB, a best guess above; getMessage is exact. */
|
|
37
|
+
hasAttachments: boolean;
|
|
38
|
+
}
|
|
39
|
+
/** The full JSON of GET /v1/messages/{id} and GET /v1/messages/wait. */
|
|
40
|
+
interface InboxMessageData extends InboxMessageSummary {
|
|
41
|
+
text?: string | null;
|
|
42
|
+
/** The HTML part as received, NOT sanitised: never render it in a page of your own origin. */
|
|
43
|
+
html?: string | null;
|
|
44
|
+
headers?: InboxHeaders | null;
|
|
45
|
+
links?: InboxLink[];
|
|
46
|
+
/** One-time codes found in the mail (verification codes, OTPs, PINs), best guess first. */
|
|
47
|
+
codes?: string[];
|
|
48
|
+
attachments?: InboxAttachment[];
|
|
49
|
+
/** Sender authentication verdicts (SPF/DKIM/DMARC) as the server reports them. */
|
|
50
|
+
auth?: unknown;
|
|
51
|
+
/** Spam check result as the server reports it. */
|
|
52
|
+
spam?: unknown;
|
|
53
|
+
}
|
|
54
|
+
/** A full mail with helpers: `message.code` and `message.firstLink("verify")`. */
|
|
55
|
+
declare class InboxMessage implements InboxMessageData {
|
|
56
|
+
id: string;
|
|
57
|
+
receivedUtc: string;
|
|
58
|
+
from?: string | null;
|
|
59
|
+
to: string[];
|
|
60
|
+
subject?: string | null;
|
|
61
|
+
sizeBytes: number;
|
|
62
|
+
hasAttachments: boolean;
|
|
63
|
+
text?: string | null;
|
|
64
|
+
html?: string | null;
|
|
65
|
+
headers?: InboxHeaders | null;
|
|
66
|
+
links: InboxLink[];
|
|
67
|
+
codes: string[];
|
|
68
|
+
attachments: InboxAttachment[];
|
|
69
|
+
auth?: unknown;
|
|
70
|
+
spam?: unknown;
|
|
71
|
+
constructor(data: InboxMessageData);
|
|
72
|
+
/** The first one-time code in the mail (best guess first), or undefined when it has none. */
|
|
73
|
+
get code(): string | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* The first link whose address (or text) contains `contains` (case-insensitive), or the first link when it is omitted;
|
|
76
|
+
* undefined when none matches.
|
|
77
|
+
*/
|
|
78
|
+
firstLink(contains?: string): string | undefined;
|
|
79
|
+
/** The plain JSON (what the API returned), e.g. to pass through a Cypress task. */
|
|
80
|
+
toJSON(): InboxMessageData & {
|
|
81
|
+
code?: string;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** The public API. */
|
|
86
|
+
declare const DEFAULT_BASE_URL = "https://api.mailpier.io";
|
|
87
|
+
/** The server caps one long-poll at 120 seconds; longer waits are made of several polls. */
|
|
88
|
+
declare const MAX_POLL_SECONDS = 120;
|
|
89
|
+
declare const VERSION = "0.1.0";
|
|
90
|
+
interface MailPierInboxOptions {
|
|
91
|
+
/** An API key from the environment's API keys page (`mpi_...`). Defaults to the MAILPIER_API_KEY environment variable. */
|
|
92
|
+
apiKey?: string;
|
|
93
|
+
/** API root; defaults to MAILPIER_BASE_URL or https://api.mailpier.io. */
|
|
94
|
+
baseUrl?: string;
|
|
95
|
+
/** How often one call is retried on 429 (after Retry-After), 502/503/504 and network errors. Default 5. */
|
|
96
|
+
maxRetries?: number;
|
|
97
|
+
/** The longest single pause between retries, in ms (Retry-After is honoured up to this). Default 60000. */
|
|
98
|
+
maxRetryDelayMs?: number;
|
|
99
|
+
/** A fetch implementation (default: the global fetch of Node 18+). */
|
|
100
|
+
fetch?: typeof fetch;
|
|
101
|
+
}
|
|
102
|
+
interface NewAddressOptions {
|
|
103
|
+
/** One of the environment's base domains; omitted for its default. */
|
|
104
|
+
domain?: string;
|
|
105
|
+
/** A readable start of the address (a-z, 0-9, '-', at most 30). */
|
|
106
|
+
prefix?: string;
|
|
107
|
+
}
|
|
108
|
+
interface WaitForMessageOptions {
|
|
109
|
+
/** The full address the mail was sent to. */
|
|
110
|
+
to: string;
|
|
111
|
+
/** Only a mail whose subject contains this (case-insensitive). */
|
|
112
|
+
subjectContains?: string;
|
|
113
|
+
/** How long to wait in total, in ms. Default 60000. */
|
|
114
|
+
timeoutMs?: number;
|
|
115
|
+
/** Only a mail received at or after this time. Pass the time your test started to ignore older mail to the same address. */
|
|
116
|
+
since?: Date | string;
|
|
117
|
+
/** Together with `since`: skip this mail id (and the ones stored before it in the same millisecond). */
|
|
118
|
+
afterId?: string;
|
|
119
|
+
signal?: AbortSignal;
|
|
120
|
+
}
|
|
121
|
+
interface ListMessagesOptions {
|
|
122
|
+
to?: string;
|
|
123
|
+
subjectContains?: string;
|
|
124
|
+
since?: Date | string;
|
|
125
|
+
afterId?: string;
|
|
126
|
+
/** 1..100, default 50. */
|
|
127
|
+
take?: number;
|
|
128
|
+
skip?: number;
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
}
|
|
131
|
+
interface DeleteAllResult {
|
|
132
|
+
deleted: number;
|
|
133
|
+
/** Mails that were also sent outside this environment; they expire with the retention period instead. */
|
|
134
|
+
skippedShared: number;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Client for the MailPier Inbox API. Create one per API key and reuse it.
|
|
138
|
+
*
|
|
139
|
+
* ```ts
|
|
140
|
+
* const inbox = new MailPierInbox({ apiKey: process.env.MAILPIER_API_KEY });
|
|
141
|
+
* const address = await inbox.newAddress({ prefix: "signup" });
|
|
142
|
+
* // ... make your app send a mail to address ...
|
|
143
|
+
* const mail = await inbox.waitForMessage({ to: address, subjectContains: "Confirm", timeoutMs: 60_000 });
|
|
144
|
+
* mail.code; // "482913"
|
|
145
|
+
* mail.firstLink("verify"); // "https://app.example.com/verify?t=..."
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
declare class MailPierInbox {
|
|
149
|
+
readonly baseUrl: string;
|
|
150
|
+
private readonly apiKey;
|
|
151
|
+
private readonly maxRetries;
|
|
152
|
+
private readonly maxRetryDelayMs;
|
|
153
|
+
private readonly fetchImpl;
|
|
154
|
+
constructor(options?: MailPierInboxOptions);
|
|
155
|
+
/** A fresh random address of the key's environment, e.g. `signup-k3v9q2mz7a@qa.acme.mailpier.io`. */
|
|
156
|
+
newAddress(options?: NewAddressOptions): Promise<string>;
|
|
157
|
+
/**
|
|
158
|
+
* Waits until a mail to `to` arrives and returns it (the OLDEST match received at or after `since`).
|
|
159
|
+
* Throws MailPierTimeoutError when nothing arrives within `timeoutMs`.
|
|
160
|
+
*/
|
|
161
|
+
waitForMessage(options: WaitForMessageOptions): Promise<InboxMessage>;
|
|
162
|
+
/**
|
|
163
|
+
* Waits for the next mail to `to` after `previous` (one you already received): passes its receivedUtc as `since` and its id as
|
|
164
|
+
* `afterId`, so a mail stored in the same millisecond is not missed and `previous` itself is never returned again.
|
|
165
|
+
*/
|
|
166
|
+
waitForNextMessage(previous: Pick<InboxMessageSummary, "id" | "receivedUtc">, options: Omit<WaitForMessageOptions, "since" | "afterId">): Promise<InboxMessage>;
|
|
167
|
+
/** Mail of the environment, newest first (at most 100 per call). */
|
|
168
|
+
listMessages(options?: ListMessagesOptions): Promise<InboxMessageSummary[]>;
|
|
169
|
+
/** One mail with its text, HTML, headers, links, codes and attachment list; null when it does not exist (or expired). */
|
|
170
|
+
getMessage(id: string): Promise<InboxMessage | null>;
|
|
171
|
+
/** The original message (.eml bytes); null when it does not exist. */
|
|
172
|
+
getRaw(id: string): Promise<Uint8Array | null>;
|
|
173
|
+
/** One attachment's bytes; null when it does not exist. */
|
|
174
|
+
getAttachment(id: string, index: number): Promise<Uint8Array | null>;
|
|
175
|
+
/** Deletes one mail. False when it did not exist. Throws (409) when the mail was also sent outside this environment. */
|
|
176
|
+
deleteMessage(id: string): Promise<boolean>;
|
|
177
|
+
/** Deletes every mail to one address; returns how many were deleted. */
|
|
178
|
+
deleteAll(to: string): Promise<number>;
|
|
179
|
+
/** Like deleteAll, but also says how many shared mails were skipped. */
|
|
180
|
+
deleteAllDetailed(to: string): Promise<DeleteAllResult>;
|
|
181
|
+
/** The key's account, environment, domains, plan and usage (GET /v1/me). */
|
|
182
|
+
me(): Promise<Record<string, unknown>>;
|
|
183
|
+
private download;
|
|
184
|
+
private url;
|
|
185
|
+
/**
|
|
186
|
+
* One call with retries: 429 waits Retry-After (default 1 s), 502/503/504 and network errors back off 0.5, 1, 2, 4 ... s
|
|
187
|
+
* (both capped at maxRetryDelayMs). Other errors throw MailPierInboxError with the server's {"error": "..."} text.
|
|
188
|
+
*/
|
|
189
|
+
private request;
|
|
190
|
+
private backoff;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export { DEFAULT_BASE_URL as D, MAX_POLL_SECONDS as M, VERSION as V, InboxMessage as e, MailPierInbox as h };
|
|
194
|
+
export type { InboxAddress as I, ListMessagesOptions as L, NewAddressOptions as N, WaitForMessageOptions as W, DeleteAllResult as a, InboxAttachment as b, InboxHeaders as c, InboxLink as d, InboxMessageData as f, InboxMessageSummary as g, MailPierInboxOptions as i };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/cypress/commands.ts
|
|
21
|
+
var commands_exports = {};
|
|
22
|
+
__export(commands_exports, {
|
|
23
|
+
pickLink: () => pickLink,
|
|
24
|
+
registerMailPierCommands: () => registerMailPierCommands
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(commands_exports);
|
|
27
|
+
function pickLink(message, contains) {
|
|
28
|
+
const links = message.links ?? [];
|
|
29
|
+
if (!contains) return links[0]?.href;
|
|
30
|
+
const n = contains.toLowerCase();
|
|
31
|
+
return links.find((l) => l.href.toLowerCase().includes(n) || (l.text ?? "").toLowerCase().includes(n))?.href;
|
|
32
|
+
}
|
|
33
|
+
function registerMailPierCommands(cypress, cyRef) {
|
|
34
|
+
const created = /* @__PURE__ */ new Map();
|
|
35
|
+
const lastSeen = /* @__PURE__ */ new Map();
|
|
36
|
+
const waitArgs = (to, o = {}) => {
|
|
37
|
+
const key = to.toLowerCase();
|
|
38
|
+
const prev = lastSeen.get(key);
|
|
39
|
+
return {
|
|
40
|
+
to,
|
|
41
|
+
subjectContains: o.subjectContains,
|
|
42
|
+
timeoutMs: o.timeoutMs ?? 6e4,
|
|
43
|
+
since: prev?.receivedUtc ?? created.get(key),
|
|
44
|
+
afterId: prev?.id
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const wait = (to, o = {}) => {
|
|
48
|
+
const args = waitArgs(to, o);
|
|
49
|
+
return cyRef().task("mailpier:waitForMessage", args, { timeout: (args.timeoutMs ?? 6e4) + 15e3 }).then((m) => {
|
|
50
|
+
lastSeen.set(to.toLowerCase(), { receivedUtc: m.receivedUtc, id: m.id });
|
|
51
|
+
return m;
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
cypress.Commands.add("mailpierNewAddress", (options) => {
|
|
55
|
+
const started = new Date(Date.now() - 1e3).toISOString();
|
|
56
|
+
return cyRef().task("mailpier:newAddress", options ?? null).then((address) => {
|
|
57
|
+
created.set(address.toLowerCase(), started);
|
|
58
|
+
return address;
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
cypress.Commands.add("mailpierWaitForMessage", (to, options) => wait(to, options));
|
|
62
|
+
cypress.Commands.add("mailpierWaitForCode", (to, options) => wait(to, options).then((m) => {
|
|
63
|
+
if (!m.code) throw new Error(`The mail "${m.subject ?? ""}" to ${to} holds no one-time code.`);
|
|
64
|
+
return m.code;
|
|
65
|
+
}));
|
|
66
|
+
cypress.Commands.add("mailpierWaitForLink", (to, contains, options) => wait(to, options).then((m) => {
|
|
67
|
+
const link = pickLink(m, contains);
|
|
68
|
+
if (!link) throw new Error(`The mail "${m.subject ?? ""}" to ${to} holds no link${contains ? ` containing "${contains}"` : ""}.`);
|
|
69
|
+
return link;
|
|
70
|
+
}));
|
|
71
|
+
cypress.Commands.add("mailpierDeleteAll", (to) => cyRef().task("mailpier:deleteAll", { to }));
|
|
72
|
+
}
|
|
73
|
+
var g = globalThis;
|
|
74
|
+
if (g.Cypress?.Commands) registerMailPierCommands(g.Cypress, () => g.cy);
|
|
75
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
76
|
+
0 && (module.exports = {
|
|
77
|
+
pickLink,
|
|
78
|
+
registerMailPierCommands
|
|
79
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { MailPierTaskMessage } from './index.cjs';
|
|
2
|
+
import '../client-CqysQIQJ.cjs';
|
|
3
|
+
|
|
4
|
+
interface MailPierCommandWaitOptions {
|
|
5
|
+
subjectContains?: string;
|
|
6
|
+
/** Default 60000. The cy.task timeout is set 15 s above it. */
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
}
|
|
9
|
+
declare global {
|
|
10
|
+
namespace Cypress {
|
|
11
|
+
interface Chainable<Subject = any> {
|
|
12
|
+
/** A fresh MailPier Inbox address of the key's environment. */
|
|
13
|
+
mailpierNewAddress(options?: {
|
|
14
|
+
prefix?: string;
|
|
15
|
+
domain?: string;
|
|
16
|
+
}): Chainable<string>;
|
|
17
|
+
/** The next mail to `to` (after the address was made, or after the previous mail this spec got for it). */
|
|
18
|
+
mailpierWaitForMessage(to: string, options?: MailPierCommandWaitOptions): Chainable<MailPierTaskMessage>;
|
|
19
|
+
/** The one-time code of the next mail to `to`. */
|
|
20
|
+
mailpierWaitForCode(to: string, options?: MailPierCommandWaitOptions): Chainable<string>;
|
|
21
|
+
/** The first link containing `contains` (any link when omitted) of the next mail to `to`. */
|
|
22
|
+
mailpierWaitForLink(to: string, contains?: string, options?: MailPierCommandWaitOptions): Chainable<string>;
|
|
23
|
+
/** Deletes the mail to `to`; yields how many. */
|
|
24
|
+
mailpierDeleteAll(to: string): Chainable<number>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** The parts of Cypress the commands use (so they can be unit-tested without Cypress). */
|
|
29
|
+
interface CypressLike {
|
|
30
|
+
Commands: {
|
|
31
|
+
add(name: string, fn: (...args: any[]) => unknown): void;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
interface CyLike {
|
|
35
|
+
task(name: string, arg?: unknown, options?: {
|
|
36
|
+
timeout?: number;
|
|
37
|
+
log?: boolean;
|
|
38
|
+
}): {
|
|
39
|
+
then(fn: (v: any) => unknown): unknown;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** The first link whose href or text contains `contains` (case-insensitive); any link when omitted. */
|
|
43
|
+
declare function pickLink(message: Pick<MailPierTaskMessage, "links">, contains?: string): string | undefined;
|
|
44
|
+
/** Registers the cy.mailpier* commands. Called automatically when this module loads inside Cypress. */
|
|
45
|
+
declare function registerMailPierCommands(cypress: CypressLike, cyRef: () => CyLike): void;
|
|
46
|
+
|
|
47
|
+
export { pickLink, registerMailPierCommands };
|
|
48
|
+
export type { CyLike, CypressLike, MailPierCommandWaitOptions };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { MailPierTaskMessage } from './index.js';
|
|
2
|
+
import '../client-CqysQIQJ.js';
|
|
3
|
+
|
|
4
|
+
interface MailPierCommandWaitOptions {
|
|
5
|
+
subjectContains?: string;
|
|
6
|
+
/** Default 60000. The cy.task timeout is set 15 s above it. */
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
}
|
|
9
|
+
declare global {
|
|
10
|
+
namespace Cypress {
|
|
11
|
+
interface Chainable<Subject = any> {
|
|
12
|
+
/** A fresh MailPier Inbox address of the key's environment. */
|
|
13
|
+
mailpierNewAddress(options?: {
|
|
14
|
+
prefix?: string;
|
|
15
|
+
domain?: string;
|
|
16
|
+
}): Chainable<string>;
|
|
17
|
+
/** The next mail to `to` (after the address was made, or after the previous mail this spec got for it). */
|
|
18
|
+
mailpierWaitForMessage(to: string, options?: MailPierCommandWaitOptions): Chainable<MailPierTaskMessage>;
|
|
19
|
+
/** The one-time code of the next mail to `to`. */
|
|
20
|
+
mailpierWaitForCode(to: string, options?: MailPierCommandWaitOptions): Chainable<string>;
|
|
21
|
+
/** The first link containing `contains` (any link when omitted) of the next mail to `to`. */
|
|
22
|
+
mailpierWaitForLink(to: string, contains?: string, options?: MailPierCommandWaitOptions): Chainable<string>;
|
|
23
|
+
/** Deletes the mail to `to`; yields how many. */
|
|
24
|
+
mailpierDeleteAll(to: string): Chainable<number>;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/** The parts of Cypress the commands use (so they can be unit-tested without Cypress). */
|
|
29
|
+
interface CypressLike {
|
|
30
|
+
Commands: {
|
|
31
|
+
add(name: string, fn: (...args: any[]) => unknown): void;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
interface CyLike {
|
|
35
|
+
task(name: string, arg?: unknown, options?: {
|
|
36
|
+
timeout?: number;
|
|
37
|
+
log?: boolean;
|
|
38
|
+
}): {
|
|
39
|
+
then(fn: (v: any) => unknown): unknown;
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** The first link whose href or text contains `contains` (case-insensitive); any link when omitted. */
|
|
43
|
+
declare function pickLink(message: Pick<MailPierTaskMessage, "links">, contains?: string): string | undefined;
|
|
44
|
+
/** Registers the cy.mailpier* commands. Called automatically when this module loads inside Cypress. */
|
|
45
|
+
declare function registerMailPierCommands(cypress: CypressLike, cyRef: () => CyLike): void;
|
|
46
|
+
|
|
47
|
+
export { pickLink, registerMailPierCommands };
|
|
48
|
+
export type { CyLike, CypressLike, MailPierCommandWaitOptions };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/cypress/commands.ts
|
|
2
|
+
function pickLink(message, contains) {
|
|
3
|
+
const links = message.links ?? [];
|
|
4
|
+
if (!contains) return links[0]?.href;
|
|
5
|
+
const n = contains.toLowerCase();
|
|
6
|
+
return links.find((l) => l.href.toLowerCase().includes(n) || (l.text ?? "").toLowerCase().includes(n))?.href;
|
|
7
|
+
}
|
|
8
|
+
function registerMailPierCommands(cypress, cyRef) {
|
|
9
|
+
const created = /* @__PURE__ */ new Map();
|
|
10
|
+
const lastSeen = /* @__PURE__ */ new Map();
|
|
11
|
+
const waitArgs = (to, o = {}) => {
|
|
12
|
+
const key = to.toLowerCase();
|
|
13
|
+
const prev = lastSeen.get(key);
|
|
14
|
+
return {
|
|
15
|
+
to,
|
|
16
|
+
subjectContains: o.subjectContains,
|
|
17
|
+
timeoutMs: o.timeoutMs ?? 6e4,
|
|
18
|
+
since: prev?.receivedUtc ?? created.get(key),
|
|
19
|
+
afterId: prev?.id
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
const wait = (to, o = {}) => {
|
|
23
|
+
const args = waitArgs(to, o);
|
|
24
|
+
return cyRef().task("mailpier:waitForMessage", args, { timeout: (args.timeoutMs ?? 6e4) + 15e3 }).then((m) => {
|
|
25
|
+
lastSeen.set(to.toLowerCase(), { receivedUtc: m.receivedUtc, id: m.id });
|
|
26
|
+
return m;
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
cypress.Commands.add("mailpierNewAddress", (options) => {
|
|
30
|
+
const started = new Date(Date.now() - 1e3).toISOString();
|
|
31
|
+
return cyRef().task("mailpier:newAddress", options ?? null).then((address) => {
|
|
32
|
+
created.set(address.toLowerCase(), started);
|
|
33
|
+
return address;
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
cypress.Commands.add("mailpierWaitForMessage", (to, options) => wait(to, options));
|
|
37
|
+
cypress.Commands.add("mailpierWaitForCode", (to, options) => wait(to, options).then((m) => {
|
|
38
|
+
if (!m.code) throw new Error(`The mail "${m.subject ?? ""}" to ${to} holds no one-time code.`);
|
|
39
|
+
return m.code;
|
|
40
|
+
}));
|
|
41
|
+
cypress.Commands.add("mailpierWaitForLink", (to, contains, options) => wait(to, options).then((m) => {
|
|
42
|
+
const link = pickLink(m, contains);
|
|
43
|
+
if (!link) throw new Error(`The mail "${m.subject ?? ""}" to ${to} holds no link${contains ? ` containing "${contains}"` : ""}.`);
|
|
44
|
+
return link;
|
|
45
|
+
}));
|
|
46
|
+
cypress.Commands.add("mailpierDeleteAll", (to) => cyRef().task("mailpier:deleteAll", { to }));
|
|
47
|
+
}
|
|
48
|
+
var g = globalThis;
|
|
49
|
+
if (g.Cypress?.Commands) registerMailPierCommands(g.Cypress, () => g.cy);
|
|
50
|
+
export {
|
|
51
|
+
pickLink,
|
|
52
|
+
registerMailPierCommands
|
|
53
|
+
};
|