kordexa 0.4.2 → 0.4.3

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.
@@ -0,0 +1,177 @@
1
+ import assert from "node:assert/strict";
2
+ import fs from "node:fs";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import test from "node:test";
6
+
7
+ import { main } from "../../src/cli.js";
8
+ import { dailyEmailKey, resendConfigFromEnv, sendWithResend } from "../../src/email.js";
9
+
10
+ process.env.KORDEXA_OFFLINE = "1";
11
+
12
+ const TEST_KEY = "re_test_value_not_real";
13
+ const resend = { apiKey: TEST_KEY, from: "Kordexa <onboarding@resend.dev>" };
14
+ const message = { to: "learner@gmail.com", subject: "Day 1", html: "<p>Plan</p>", text: "Plan" };
15
+ const noWait = async () => {};
16
+
17
+ function reply(status, body, headers = {}) {
18
+ return { ok: status < 400, status, headers: { get: (name) => headers[name] ?? null }, json: async () => body };
19
+ }
20
+
21
+ async function withFetch(replies, fn) {
22
+ const original = global.fetch;
23
+ const calls = [];
24
+ global.fetch = async (url, init) => {
25
+ calls.push({ url: String(url), init });
26
+ const next = replies.shift();
27
+ if (next instanceof Error) throw next;
28
+ return next;
29
+ };
30
+ try {
31
+ return await fn(calls);
32
+ } finally {
33
+ global.fetch = original;
34
+ }
35
+ }
36
+
37
+ async function inProject(fn) {
38
+ const oldCwd = process.cwd();
39
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), "kordexa-resend-"));
40
+ const log = console.log;
41
+ const printed = [];
42
+ console.log = (line) => printed.push(String(line));
43
+ process.chdir(dir);
44
+ try {
45
+ await main(["--yes", "--offline", "--name", "Asha", "--email", "learner@gmail.com", "--role", "Data Scientist"]);
46
+ fs.appendFileSync(path.join(dir, ".env.local"), `RESEND_API_KEY=${TEST_KEY}\n`);
47
+ return await fn(dir, printed);
48
+ } finally {
49
+ console.log = log;
50
+ process.chdir(oldCwd);
51
+ fs.rmSync(dir, { recursive: true, force: true });
52
+ }
53
+ }
54
+
55
+ test("Resend is used only when an API key is configured", () => {
56
+ assert.equal(resendConfigFromEnv({}), null);
57
+ assert.deepEqual(resendConfigFromEnv({ RESEND_API_KEY: TEST_KEY }), resend);
58
+ assert.equal(resendConfigFromEnv({ RESEND_API_KEY: TEST_KEY, RESEND_FROM: "Plans <a@b.co>" }).from, "Plans <a@b.co>");
59
+ });
60
+
61
+ test("daily email key is stable, private and specific to each plan day", () => {
62
+ const key = dailyEmailKey("Learner@Gmail.com", "2026-11-18", 3);
63
+ assert.equal(key, dailyEmailKey("learner@gmail.com", "2026-11-18", 3));
64
+ assert.notEqual(key, dailyEmailKey("learner@gmail.com", "2026-11-18", 4));
65
+ assert.doesNotMatch(key, /gmail|learner/i);
66
+ assert.ok(key.length <= 256);
67
+ });
68
+
69
+ test("an email is sent with the required Resend request format", async () => {
70
+ await withFetch([reply(200, { id: "email_1" })], async (calls) => {
71
+ const result = await sendWithResend({ resend, message, idempotencyKey: "day-1" });
72
+ assert.deepEqual(result, { sent: true, to: message.to, subject: "Day 1", transport: "resend", id: "email_1" });
73
+ assert.equal(calls[0].url, "https://api.resend.com/emails");
74
+ assert.equal(calls[0].init.headers.authorization, `Bearer ${TEST_KEY}`);
75
+ assert.equal(calls[0].init.headers["idempotency-key"], "day-1");
76
+ assert.ok(calls[0].init.headers["user-agent"]);
77
+ assert.deepEqual(JSON.parse(calls[0].init.body), { from: resend.from, to: [message.to], subject: "Day 1", html: "<p>Plan</p>", text: "Plan" });
78
+ });
79
+ });
80
+
81
+ test("temporary failures retry with the same key so no duplicate is sent", async () => {
82
+ await withFetch([reply(429, {}, { "retry-after": "1" }), new Error("network down"), reply(200, { id: "email_2" })], async (calls) => {
83
+ const result = await sendWithResend({ resend, message, idempotencyKey: "day-2", wait: noWait });
84
+ assert.equal(result.id, "email_2");
85
+ assert.equal(calls.length, 3);
86
+ assert.deepEqual(calls.map((call) => call.init.headers["idempotency-key"]), ["day-2", "day-2", "day-2"]);
87
+ });
88
+ });
89
+
90
+ test("permanent failures stop immediately with a clear message", async () => {
91
+ const cases = [
92
+ [401, { message: "API key is invalid" }, /didn't accept the API key/],
93
+ [403, { message: "You can only send testing emails to your own email address. Please verify a domain." }, /can only email the address on your Resend account/],
94
+ [422, { message: "Invalid to field" }, /error 422.*Invalid to field/]
95
+ ];
96
+ for (const [status, body, expected] of cases) {
97
+ await withFetch([reply(status, body)], async (calls) => {
98
+ await assert.rejects(sendWithResend({ resend, message, wait: noWait }), (error) => {
99
+ assert.match(error.message, expected);
100
+ assert.doesNotMatch(error.message, new RegExp(TEST_KEY));
101
+ return true;
102
+ });
103
+ assert.equal(calls.length, 1);
104
+ });
105
+ }
106
+ });
107
+
108
+ test("repeated outages end with a helpful error", async () => {
109
+ await withFetch([new Error("down"), new Error("down"), new Error("down")], async (calls) => {
110
+ await assert.rejects(sendWithResend({ resend, message, wait: noWait }), /Couldn't reach Resend/);
111
+ assert.equal(calls.length, 3);
112
+ });
113
+ await withFetch([reply(500, {}), reply(503, {}), reply(500, { message: "Server error" })], async (calls) => {
114
+ await assert.rejects(sendWithResend({ resend, message, wait: noWait }), /error 500/);
115
+ assert.equal(calls.length, 3);
116
+ });
117
+ });
118
+
119
+ test("send-today delivers through Resend and keeps the key out of saved results", async () => {
120
+ await inProject(async (dir, printed) => {
121
+ await withFetch([reply(200, { id: "email_3" })], async (calls) => {
122
+ await main(["send-today", "--day", "1", "--offline"]);
123
+ assert.equal(calls.length, 1);
124
+ assert.match(calls[0].init.headers["idempotency-key"], /-day-1$/);
125
+ });
126
+ const latest = fs.readFileSync(path.join(dir, "data/latest-email.json"), "utf8");
127
+ assert.equal(JSON.parse(latest).transport, "resend");
128
+ assert.doesNotMatch(latest + printed.join("\n"), new RegExp(TEST_KEY));
129
+ });
130
+ });
131
+
132
+ test("a dry run never contacts Resend", async () => {
133
+ await inProject(async () => {
134
+ await withFetch([], async (calls) => {
135
+ await main(["send-today", "--dry-run", "--day", "1", "--offline"]);
136
+ assert.equal(calls.length, 0);
137
+ });
138
+ });
139
+ });
140
+
141
+ test("the scheduled workflow passes Resend settings from GitHub secrets", async () => {
142
+ await inProject(async (dir) => {
143
+ const workflow = fs.readFileSync(path.join(dir, ".github/workflows/kordexa-daily.yml"), "utf8");
144
+ assert.match(workflow, /RESEND_API_KEY: \$\{\{ secrets\.RESEND_API_KEY \}\}/);
145
+ assert.match(workflow, /RESEND_FROM: \$\{\{ secrets\.RESEND_FROM \}\}/);
146
+ assert.doesNotMatch(workflow, new RegExp(TEST_KEY));
147
+ });
148
+ });
149
+
150
+ test("setup upgrades the earlier generated workflow to include Resend", async () => {
151
+ await inProject(async (dir) => {
152
+ const file = path.join(dir, ".github/workflows/kordexa-daily.yml");
153
+ const earlier = fs.readFileSync(file, "utf8").replace(/ {10}RESEND_API_KEY: .*\n {10}RESEND_FROM: .*\n/, "");
154
+ fs.writeFileSync(file, earlier);
155
+ assert.equal((await main(["doctor"])).status, "needs-upgrade");
156
+ await main(["--yes", "--offline"]);
157
+ assert.match(fs.readFileSync(file, "utf8"), /RESEND_API_KEY/);
158
+ assert.equal((await main(["doctor"])).status, "ready");
159
+ });
160
+ });
161
+
162
+ test("setup-secrets asks for the Resend key without printing it", async () => {
163
+ await inProject(async () => {
164
+ const result = await main(["setup-secrets"]);
165
+ assert.deepEqual(result.commands, ["gh secret set RESEND_API_KEY"]);
166
+ assert.doesNotMatch(JSON.stringify(result), new RegExp(TEST_KEY));
167
+ });
168
+ });
169
+
170
+ test("doctor counts a Resend key as connected email", async () => {
171
+ await inProject(async () => {
172
+ const result = await main(["doctor"]);
173
+ assert.equal(result.checks.localEmail, true);
174
+ assert.equal(result.status, "ready");
175
+ assert.doesNotMatch(JSON.stringify(result), new RegExp(TEST_KEY));
176
+ });
177
+ });