Skip to content

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

fix: preserve current CLI changes in HTTP status repair

Rudy Celekli119e0dc76db3
diff --cc tests/clis/exa-answer.test.cjs
index 0000000,88fdafe..01abd96
mode 000000,100644..100644
--- a/tests/clis/exa-answer.test.cjs
+++ b/tests/clis/exa-answer.test.cjs
@@@ -1,0 -1,36 +1,36 @@@
+ 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/exa.js')
+ function run(args, network = true) {
+ const script = `global.fetch = async (url, options) => {
+ if (!${network}) throw new Error('Unexpected network request');
- return {status:200, text:async()=>JSON.stringify({url,method:options.method,request:JSON.parse(options.body),answer:'Fixture answer',citations:[{url:'https://example.org/source'}]})};
++ return {ok:true,status:200, text:async()=>JSON.stringify({url,method:options.method,request:JSON.parse(options.body),answer:'Fixture answer',citations:[{url:'https://example.org/source'}]})};
+ }; process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath,['-e',script],{encoding:'utf8',timeout:5000,env:{PATH:process.env.PATH,EXA_API_KEY:'fixture-secret'}})
+ }
+ function output(r) { assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout) }
+ test('cited answers use the documented endpoint and preserve sources',()=>{
+ const result=output(run(['answer','What','changed?']))
+ assert.equal(result.url,'https://api.exa.ai/answer');assert.equal(result.method,'POST')
+ assert.deepEqual(result.request,{query:'What changed?'});assert.equal(result.answer,'Fixture answer')
+ assert.deepEqual(result.citations,[{url:'https://example.org/source'}])
+ })
+ test('answer supports structured output and source text without streaming',()=>{
+ const schema={type:'object',properties:{summary:{type:'string'}}}
+ const result=output(run(['answer','--query','Research','--model','exa-pro','--system-prompt','Use dated sources','--text','false','--output-schema',JSON.stringify(schema)]))
+ assert.deepEqual(result.request,{query:'Research',model:'exa-pro',systemPrompt:'Use dated sources',text:false,outputSchema:schema})
+ assert.equal(Object.hasOwn(result.request,'stream'),false)
+ assert.equal(output(run(['answer','Research','--text'])).request.text,true)
+ })
+ test('invalid query, booleans and schemas fail before a billed request',()=>{
+ for(const args of [['answer'],['answer','--query'],['answer','Q','--text','maybe'],['answer','Q','--output-schema','null'],['answer','Q','--output-schema','[]'],['answer','Q','--output-schema','{'],['answer','Q','--model']]) {
+ const result=run(args,false);assert.equal(result.status,1);assert.doesNotMatch(result.stderr,/Unexpected network/)
+ }
+ })
+ test('answer dry run shows the request without disclosing auth',()=>{
+ const result=output(run(['answer','Research','--dry-run'],false))
+ assert.equal(result.url,'https://api.exa.ai/answer');assert.equal(result.body.query,'Research');assert.equal(result._dry_run,true)
+ assert.equal(JSON.stringify(result).includes('fixture-secret'),false)
+ })
diff --cc tests/clis/exa-structured-search.test.cjs
index 0000000,c726a65..d2aae39
mode 000000,100644..100644
--- a/tests/clis/exa-structured-search.test.cjs
+++ b/tests/clis/exa-structured-search.test.cjs
@@@ -1,0 -1,26 +1,26 @@@
+ 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/exa.js')
+ function run(args, network = true) {
- const code=`global.fetch=async(url,opts)=>{if(!${network})throw Error('Unexpected request');return{status:200,text:async()=>JSON.stringify({url,body:JSON.parse(opts.body),output:{content:{vendors:['Example']},grounding:[{field:'vendors',citations:[{url:'https://example.com'}],confidence:'low'}]},requestId:'test',costDollars:{total:0.02}})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
++ const code=`global.fetch=async(url,opts)=>{if(!${network})throw Error('Unexpected request');return{ok:true,status:200,text:async()=>JSON.stringify({url,body:JSON.parse(opts.body),output:{content:{vendors:['Example']},grounding:[{field:'vendors',citations:[{url:'https://example.com'}],confidence:'low'}]},requestId:'test',costDollars:{total:0.02}})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath,['-e',code],{encoding:'utf8',timeout:5000,env:{...process.env,EXA_API_KEY:'fake-key'}})
+ }
+ function output(r){assert.equal(r.status,0,r.stderr);return JSON.parse(r.stdout)}
+ const schema={type:'object',properties:{vendors:{type:'array',items:{type:'string'}}},required:['vendors']}
+ test('structured search forwards the schema and research context and preserves grounding',()=>{
+ const data=output(run(['search','--query','newsletter platforms','--type','deep','--output-schema',JSON.stringify(schema),'--objective','Find vendors for a small publisher','--system-prompt','Use primary sources','--additional-queries',JSON.stringify(['newsletter pricing','newsletter deliverability']),'--highlights']));
+ assert.deepEqual(data.body.outputSchema,schema);assert.equal(data.body.objective,'Find vendors for a small publisher');assert.equal(data.body.systemPrompt,'Use primary sources');assert.deepEqual(data.body.additionalQueries,['newsletter pricing','newsletter deliverability']);assert.deepEqual(data.body.contents,{highlights:true});assert.equal(data.output.grounding[0].citations[0].url,'https://example.com');assert.equal(data.costDollars.total,0.02);
+ })
+ test('text schema and existing search filters work together in a redacted dry run',()=>{
+ const data=output(run(['search','--query','newsletters','--output-schema','{"type":"text"}','--include-domains','example.com','--num','3','--dry-run'],false));
+ assert.deepEqual(data.body.outputSchema,{type:'text'});assert.deepEqual(data.body.includeDomains,['example.com']);assert.equal(data.body.numResults,3);assert.equal(data.headers['x-api-key'],'***');assert.doesNotMatch(JSON.stringify(data),/fake-key/)
+ })
+ test('malformed schema and research parameters fail before a request',()=>{
+ const cases=[['--output-schema','not-json'],['--output-schema','[]'],['--output-schema','{"type":"array"}'],['--output-schema'],['--objective'],['--objective','x'.repeat(4097)],['--system-prompt'],['--additional-queries','[]'],['--additional-queries','[1]'],['--additional-queries','["q"]'],['--additional-queries',JSON.stringify(Array(11).fill('q')),'--type','deep']];
+ for(const args of cases){const r=run(['search','--query','q',...args],false);assert.equal(r.status,1,r.stdout);assert.match(r.stderr,/output-schema|objective|system-prompt|additional-queries/);assert.doesNotMatch(r.stderr,/Unexpected request/)}
+ })
+ test('plain search preserves its existing payload when structured options are omitted',()=>{
+ const data=output(run(['search','--query','newsletter platforms','--num','5']));assert.deepEqual(data.body,{query:'newsletter platforms',numResults:5});assert.equal(data.url,'https://api.exa.ai/search')
+ })
diff --cc tests/clis/outreach-pagination.test.cjs
index 0000000,f291b25..31301dd
mode 000000,100644..100644
--- a/tests/clis/outreach-pagination.test.cjs
+++ b/tests/clis/outreach-pagination.test.cjs
@@@ -1,0 -1,48 +1,48 @@@
+ 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/outreach.js')
+ function run(args) {
- const code = `global.fetch = async (url, options) => ({status:200,text:async()=>JSON.stringify({url,method:options.method,cursor:new URL(url).searchParams.get('page[after]')})});process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
++ const code = `global.fetch = async (url, options) => ({ok:true,status:200,text:async()=>JSON.stringify({url,method:options.method,cursor:new URL(url).searchParams.get('page[after]')})});process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, OUTREACH_ACCESS_TOKEN: 'fixture-only' } })
+ }
+ for (const resource of ['prospects', 'sequences', 'mailings', 'accounts', 'tasks']) {
+ test(`${resource} list forwards cursor and size instead of repeating the first page`, () => {
+ const result = run([resource, 'list', '--after', 'opaque+cursor=/part', '--per-page', '25', '--sequence-id', '123', '--state', 'pending'])
+ assert.equal(result.status, 0, result.stderr)
+ const output = JSON.parse(result.stdout)
+ assert.equal(output.cursor, 'opaque+cursor=/part')
+ const query = new URL(output.url).searchParams
+ assert.equal(query.get('page[size]'), '25')
+ if (resource === 'mailings') assert.equal(query.get('filter[sequence][id]'), '123')
+ if (resource === 'tasks') assert.equal(query.get('filter[state]'), 'pending')
+ })
+ }
+ test('legacy page selection is translated into the documented offset contract', () => {
+ const result = run(['prospects', 'list', '--page', '3', '--per-page', '25'])
+ assert.equal(result.status, 0, result.stderr)
+ const query = new URL(JSON.parse(result.stdout).url).searchParams
+ assert.equal(query.get('page[offset]'), '50')
+ assert.equal(query.has('page[number]'), false)
+ assert.equal(query.has('page[size]'), false)
+ assert.equal(query.get('page[limit]'), '25')
+ const defaultQuery = new URL(JSON.parse(run(['prospects', 'list', '--page', '3']).stdout).url).searchParams
+ assert.equal(defaultQuery.get('page[offset]'), '100')
+ assert.equal(defaultQuery.get('page[limit]'), '50')
+ })
+ test('before cursor and dry-run preserve the exact token and credential redaction', () => {
+ const result = run(['accounts', 'list', '--before', 'previous+token', '--dry-run'])
+ assert.equal(result.status, 0, result.stderr)
+ const output = JSON.parse(result.stdout)
+ assert.equal(new URL(output.url).searchParams.get('page[before]'), 'previous+token')
+ assert.equal(output.headers.Authorization, 'Bearer ***')
+ assert.equal(new URL(JSON.parse(run(['accounts', 'list']).stdout).url).search, '')
+ })
+ test('contradictory or invalid page selections fail before any request', () => {
+ for (const args of [['--after','a','--before','b'],['--after','a','--page','2'],['--page','0'],['--page','1junk'],['--per-page'],['--per-page','1.5'],['--per-page','1001'],['--page','202','--per-page','50']]) {
+ const result = run(['accounts','list',...args])
+ assert.notEqual(result.status, 0)
+ assert.ok(JSON.parse(result.stderr).error)
+ }
+ })
diff --cc tests/clis/postmark-model-json.test.cjs
index 0000000,0bb731e..6562652
mode 000000,100644..100644
--- a/tests/clis/postmark-model-json.test.cjs
+++ b/tests/clis/postmark-model-json.test.cjs
@@@ -1,0 -1,39 +1,39 @@@
+ 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')
+ function run(extra) {
+ const args = ['email', 'send-template', '--from', '[email protected]', '--to', '[email protected]', '--template', 'receipt', ...extra]
- const code = `global.fetch=async(url,options)=>({status:200,text:async()=>JSON.stringify({url,body:JSON.parse(options.body)})});process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
++ const code = `global.fetch=async(url,options)=>({ok:true,status:200,text:async()=>JSON.stringify({url,body:JSON.parse(options.body)})});process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, POSTMARK_API_KEY: 'fixture-only' } })
+ }
+ const model = { customer: { name: 'Growth, Inc.' }, items: [{ title: 'Service: priority', quantity: 2 }], paid: true, total: 29.5 }
+ test('structured template models retain nested arrays and numeric/boolean values in the actual request', () => {
+ const r = run(['--model-json', JSON.stringify(model)])
+ assert.equal(r.status, 0, r.stderr)
+ const out = JSON.parse(r.stdout)
+ assert.equal(out.url, 'https://api.postmarkapp.com/email/withTemplate')
+ assert.deepEqual(out.body.TemplateModel, model)
+ assert.equal(out.body.TemplateAlias, 'receipt')
+ })
+ test('legacy shorthand remains intact and previews preserve model types without exposing credentials', () => {
+ const legacy = run(['--model', 'name:Rudy,url:https://example.com'])
+ assert.equal(legacy.status, 0, legacy.stderr)
+ assert.deepEqual(JSON.parse(legacy.stdout).body.TemplateModel, { name: 'Rudy', url: 'https://example.com' })
+ const preview = run(['--model-json', JSON.stringify(model), '--dry-run'])
+ assert.equal(preview.status, 0, preview.stderr)
+ const out = JSON.parse(preview.stdout)
+ assert.deepEqual(out.body.TemplateModel, model)
+ assert.equal(out.headers['X-Postmark-Server-Token'], '***')
+ })
+ for (const value of ['null', '[]', '"text"', 'true', '{bad']) test(`invalid object model ${value} fails before delivery`, () => {
+ const r = run(['--model-json', value])
+ assert.notEqual(r.status, 0)
+ assert.match(JSON.parse(r.stderr).error, /model-json/)
+ })
+ test('ambiguous legacy and JSON model inputs fail before delivery', () => {
+ const r = run(['--model', 'name:old', '--model-json', '{}'])
+ assert.notEqual(r.status, 0)
+ assert.match(JSON.parse(r.stderr).error, /not both/)
+ })
diff --cc tests/clis/segment-delivery-metadata.test.cjs
index 0000000,938c9b7..ed8026e
mode 000000,100644..100644
--- a/tests/clis/segment-delivery-metadata.test.cjs
+++ b/tests/clis/segment-delivery-metadata.test.cjs
@@@ -1,0 -1,49 +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/segment.js')
+ function run(command, extra = [], allowFetch = true) {
+ const args = [...command, '--user-id', 'customer-1', ...extra]
- const code = `global.fetch=async(url,options)=>{if(!${allowFetch})throw new Error('unexpected fetch');return {status:200,text:async()=>JSON.stringify({url,body:JSON.parse(options.body)})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
++ const code = `global.fetch=async(url,options)=>{if(!${allowFetch})throw new Error('unexpected fetch');return {ok:true,status:200,text:async()=>JSON.stringify({url,body:JSON.parse(options.body)})}};process.argv=['node',${JSON.stringify(cli)},...${JSON.stringify(args)}];require(${JSON.stringify(cli)});`
+ return spawnSync(process.execPath, ['-e', code], { encoding: 'utf8', env: { ...process.env, SEGMENT_WRITE_KEY: 'fixture-only' } })
+ }
+ for (const [command, endpoint] of [[['track', 'event', '--event', 'Order Completed'], '/track'], [['identify', 'user', '--traits', '{"tier":"Gold"}'], '/identify'], [['page', 'view', '--name', 'Pricing'], '/page']]) {
+ test(`${command.slice(0, 2).join(' ')} retains occurrence time and retry identity`, () => {
+ const timestamp = '2024-04-12T10:15:30.123+02:00'
+ const r = run(command, ['--timestamp', timestamp, '--message-id', 'source-event:123'])
+ assert.equal(r.status, 0, r.stderr)
+ const out = JSON.parse(r.stdout)
+ assert.equal(out.url, `https://api.segment.io/v1${endpoint}`)
+ assert.equal(out.body.timestamp, timestamp)
+ assert.equal(out.body.messageId, 'source-event:123')
+ assert.equal(out.body.userId, 'customer-1')
+ })
+ }
+ test('without metadata flags the provider defaults remain untouched', () => {
+ const out = JSON.parse(run(['track', 'event', '--event', 'Signup']).stdout)
+ assert.equal(Object.hasOwn(out.body, 'timestamp'), false)
+ assert.equal(Object.hasOwn(out.body, 'messageId'), false)
+ })
+ test('preview keeps metadata and masks credentials', () => {
+ const r = run(['page', 'view'], ['--timestamp', '2024-04-12T10:15:30Z', '--message-id', 'x'.repeat(100), '--dry-run'], false)
+ assert.equal(r.status, 0, r.stderr)
+ const out = JSON.parse(r.stdout)
+ assert.equal(out.body.messageId.length, 100)
+ assert.equal(out.headers.Authorization, '***')
+ })
+ for (const extra of [['--timestamp'], ['--timestamp', 'yesterday'], ['--timestamp', '2024-04-12'], ['--timestamp', '2024-04-12T10:15:30'], ['--timestamp', '2024-13-12T10:15:30Z'], ['--timestamp', '2024-02-30T10:15:30Z'], ['--timestamp', '2024-02-31T10:15:30Z'], ['--timestamp', '2023-02-29T10:15:30Z'], ['--timestamp', '2024-04-31T10:15:30Z'], ['--timestamp', '2024-04-12T24:00:00Z'], ['--message-id'], ['--message-id', ''], ['--message-id', 'x'.repeat(101)]]) {
+ test(`invalid delivery metadata ${extra[0]} ${String(extra[1]).slice(0, 20)} fails before request`, () => {
+ const r = run(['track', 'event', '--event', 'Signup'], extra, false)
+ assert.equal(r.status, 1)
+ assert.match(JSON.parse(r.stderr).error, /timestamp|message-id/)
+ })
+ }
+
+ test('valid leap days and timezone offsets preserve literal occurrence time', () => {
+ for (const timestamp of ['2024-02-29T10:15:30Z', '2000-02-29T10:15:30.123-05:30', '2024-12-31T23:59:59+02:00']) {
+ const r = run(['track', 'event', '--event', 'Signup'], ['--timestamp', timestamp])
+ assert.equal(r.status, 0, r.stderr)
+ assert.equal(JSON.parse(r.stdout).body.timestamp, timestamp)
+ }
+ })