Skip to content

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

fix(mailchimp): validate member commands against their documented arguments

Rudy Celeklia95b6c996dd3
diff --git a/tests/mailchimp-members.test.cjs b/tests/mailchimp-members.test.cjs
new file mode 100644
index 0000000..14d34ba
--- /dev/null
+++ b/tests/mailchimp-members.test.cjs
@@ -0,0 +1,41 @@
+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/mailchimp.js')
+function run(args, fetch = false) {
+ const source = `global.fetch = async (url, options) => {
+ if (!${fetch}) throw new Error('Unexpected network request');
+ return { status: 200, text: async () => JSON.stringify({ url, method: options.method, body: options.body ? JSON.parse(options.body) : null }) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', env: { ...process.env, MAILCHIMP_API_KEY: 'fake-key-us7' }, timeout: 5000 })
+ assert.equal(result.status, 0, result.stderr)
+ return JSON.parse(result.stdout)
+}
+
+test('documented member add posts without an unused positional ID', () => {
+ const result = run(['members', 'add', '--list-id', 'audience123', '--email', '[email protected]', '--status', 'pending', '--first-name', 'Zoë', '--tags', 'newsletter,trial'], true)
+ assert.equal(result.method, 'POST')
+ assert.equal(result.url, 'https://us7.api.mailchimp.com/3.0/lists/audience123/members')
+ assert.deepEqual(result.body, { email_address: '[email protected]', status: 'pending', merge_fields: { FNAME: 'Zoë' }, tags: ['newsletter', 'trial'] })
+})
+test('member add dry run redacts authorization and never fetches', () => {
+ const result = run(['members', 'add', '--list-id', 'audience123', '--email', '[email protected]', '--dry-run'])
+ assert.equal(result.method, 'POST')
+ assert.equal(result.headers.Authorization, '***')
+ assert.equal(JSON.stringify(result).includes('fake-key'), false)
+})
+test('member update without subscriber hash stops before fetching an undefined member', () => {
+ const result = run(['members', 'update', '--list-id', 'audience123', '--status', 'unsubscribed'])
+ assert.match(result.error, /Subscriber hash required/)
+})
+test('existing member update preserves the selected audience and hash', () => {
+ const result = run(['members', 'update', 'hash123', '--list-id', 'audience123', '--status', 'unsubscribed'], true)
+ assert.equal(result.method, 'PATCH')
+ assert.equal(result.url.endsWith('/lists/audience123/members/hash123'), true)
+ assert.deepEqual(result.body, { status: 'unsubscribed' })
+})
+test('member add still requires email and audience', () => {
+ assert.match(run(['members', 'add', '--list-id', 'audience123']).error, /email/)
+ assert.match(run(['members', 'add', '--email', '[email protected]']).error, /list-id/)
+})
diff --git a/tools/clis/mailchimp.js b/tools/clis/mailchimp.js
index 5add6aa..a1dc03b 100755
--- a/tools/clis/mailchimp.js
+++ b/tools/clis/mailchimp.js
@@ -92,7 +92,6 @@ async function main() {
break
}
case 'add': {
- if (!rest[0]) { result = { error: 'List ID required' }; break }
if (!args.email) { result = { error: '--email required' }; break }
if (!args['list-id']) {
result = { error: '--list-id is required for members add' }
@@ -117,6 +116,7 @@ async function main() {
break
}
const subscriberHash = rest[0]
+ if (!subscriberHash) { result = { error: 'Subscriber hash required for members update' }; break }
const body = {}
if (args.status) body.status = args.status
if (args['first-name'] || args['last-name']) {