Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(postmark): signal partially rejected email batches

Rudy Celeklia3583a5dfa8f
diff --git a/tests/clis/postmark-batch-status.test.cjs b/tests/clis/postmark-batch-status.test.cjs
new file mode 100644
index 0000000..9385190
--- /dev/null
+++ b/tests/clis/postmark-batch-status.test.cjs
@@ -0,0 +1,49 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+const cli = path.resolve(__dirname, '../../tools/clis/postmark.js')
+const args = ['email', 'send-batch', '--from', '[email protected]', '--to', '[email protected],[email protected]', '--subject', 'Fixture', '--text', 'Hello']
+function run(payload, command = args, offline = false) {
+ const source = `let calls = 0; global.fetch = async (url, options) => {
+ if (${offline}) throw new Error('Unexpected network request');
+ calls++; if (calls > 1) throw new Error('Unexpected retry of accepted messages');
+ return new Response(${JSON.stringify(JSON.stringify(payload))}, { status: 200 });
+ }; process.on('exit', () => { if (!${offline} && calls !== 1) process.exitCode = 99 });
+ process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(command)}]; require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 10000, env: { ...process.env, POSTMARK_API_KEY: 'fixture-secret' } })
+}
+function output(result) {
+ assert.equal(result.stderr, '')
+ assert.equal(result.signal, null)
+ return JSON.parse(result.stdout)
+}
+const accepted = { To: '[email protected]', ErrorCode: 0, Message: 'OK', MessageID: 'accepted-message-id', SubmittedAt: '2026-10-04T12:00:00Z' }
+const rejected = { To: '[email protected]', ErrorCode: 406, Message: 'Inactive recipient' }
+for (const payload of [[accepted, rejected], [rejected, accepted], [rejected, rejected]]) {
+ test(`HTTP200 batch with errors ${payload.map(row => row.ErrorCode).join(',')} fails without losing or retrying accepted messages`, () => {
+ const result = run(payload)
+ assert.deepEqual(output(result), payload)
+ assert.equal(result.status, 1)
+ })
+}
+test('an entirely accepted batch retains success and both ordered message IDs', () => {
+ const payload = [accepted, { ...accepted, To: '[email protected]', MessageID: 'second-message-id' }]
+ const result = run(payload)
+ assert.deepEqual(output(result), payload)
+ assert.equal(result.status, 0)
+})
+test('successful single-message sending is unchanged', () => {
+ const result = run(accepted, ['email', 'send', '--from', '[email protected]', '--to', '[email protected]', '--subject', 'Fixture', '--text', 'Hello'])
+ assert.deepEqual(output(result), accepted)
+ assert.equal(result.status, 0)
+})
+test('batch preview stays offline and retains the masked token and message order', () => {
+ const result = run(null, [...args, '--dry-run'], true)
+ const preview = output(result)
+ assert.equal(result.status, 0)
+ assert.equal(preview._dry_run, true)
+ assert.equal(preview.headers['X-Postmark-Server-Token'], '***')
+ assert.deepEqual(preview.body.map(row => row.To), ['[email protected]', '[email protected]'])
+ assert.equal(JSON.stringify(preview).includes('fixture-secret'), false)
+})
diff --git a/tools/clis/postmark.js b/tools/clis/postmark.js
index 5fee027..d5904cc 100755
--- a/tools/clis/postmark.js
+++ b/tools/clis/postmark.js
@@ -151,6 +151,9 @@ async function main() {
Tag: args.tag || undefined,
}))
result = await api('POST', '/email/batch', messages)
+ if (Array.isArray(result) && result.some(message => typeof message?.ErrorCode === 'number' && message.ErrorCode !== 0)) {
+ process.exitCode = 1
+ }
break
}
default: