11import { describe , test , expect , mock , spyOn } from "bun:test" ;
2- import { analyzeCommand , formatSummary , runAnalyze , type Analysis } from "../src/commands/analyze.ts" ;
3- import { createClient , type Client } from "../src/http/client.ts" ;
2+ import { formatSummary , runAnalyze , type Analysis } from "../src/commands/analyze.ts" ;
3+ import { type Client } from "../src/http/client.ts" ;
44
55const steady = {
66 bpm : 120.01 ,
@@ -25,7 +25,7 @@ async function capture(run: () => Promise<unknown>) {
2525 const err = spyOn ( console , "error" ) . mockImplementation ( ( ) => { } ) ;
2626 try {
2727 const result = await run ( ) ;
28- return { result, lines : log . mock . calls . map ( ( c ) => String ( c [ 0 ] ) ) } ;
28+ return { result, lines : log . mock . calls . map ( ( c ) => String ( c [ 0 ] ) ) , errors : err . mock . calls . map ( ( c ) => String ( c [ 0 ] ) ) } ;
2929 } finally {
3030 log . mockRestore ( ) ;
3131 err . mockRestore ( ) ;
@@ -81,28 +81,26 @@ describe("runAnalyze", () => {
8181 } ) ;
8282} ) ;
8383
84- describe ( "analyze command" , ( ) => {
85- test ( "sends the account's API key to /generate but not to the map URL" , async ( ) => {
86- const saved = process . env . SHOTSTACK_API_KEY ;
87- process . env . SHOTSTACK_API_KEY = "test-key" ;
88- const log = spyOn ( console , "log" ) . mockImplementation ( ( ) => { } ) ;
89- const fetchMock = spyOn ( globalThis , "fetch" ) . mockImplementation ( async ( input ) =>
90- String ( input ) . endsWith ( "/generate" )
91- ? new Response ( JSON . stringify ( { id : "a" , status : "done" , url : MAP_URL } ) )
92- : new Response ( JSON . stringify ( steady ) ) ,
93- ) ;
84+ describe ( "map fetch" , ( ) => {
85+ test ( "a non-2xx map response exits 1 with a clear message" , async ( ) => {
86+ const { client } = fakeClient ( { id : "a" , status : "done" , url : MAP_URL } ) ;
87+ const fetchMock = spyOn ( globalThis , "fetch" ) . mockResolvedValue ( new Response ( "nope" , { status : 403 } ) ) ;
9488 try {
95- await analyzeCommand . parseAsync ( [ "node" , "analyze" , "https://example.com/a.mp3" , "--output " , "json" ] ) ;
96- const [ generate , map ] = fetchMock . mock . calls ;
97- expect ( ( generate ! [ 1 ] ! . headers as Record < string , string > ) [ "x-api-key" ] ) . toBe ( "test-key" ) ;
98- expect ( map ) . toEqual ( [ MAP_URL ] ) ;
89+ const { result , lines , errors } = await capture ( ( ) => runAnalyze ( client , "https://example.com/a.mp3" , "json " , 0 ) ) ;
90+ expect ( result ) . toMatchObject ( { exitCode : 1 } ) ;
91+ expect ( lines ) . toEqual ( [ ] ) ;
92+ expect ( errors ) . toEqual ( [ `could not fetch the analysis map (403) from ${ MAP_URL } ` ] ) ;
9993 } finally {
10094 fetchMock . mockRestore ( ) ;
101- log . mockRestore ( ) ;
102- if ( saved === undefined ) delete process . env . SHOTSTACK_API_KEY ;
103- else process . env . SHOTSTACK_API_KEY = saved ;
10495 }
10596 } ) ;
97+
98+ test ( "a done job with no url exits 1 with a clear message" , async ( ) => {
99+ const { client } = fakeClient ( { id : "a" , status : "done" } ) ;
100+ const { result, errors } = await capture ( ( ) => runAnalyze ( client , "https://example.com/a.mp3" , "json" , 0 ) ) ;
101+ expect ( result ) . toMatchObject ( { exitCode : 1 } ) ;
102+ expect ( errors ) . toEqual ( [ "analysis finished without a map URL" ] ) ;
103+ } ) ;
106104} ) ;
107105
108106describe ( "formatSummary" , ( ) => {
@@ -115,15 +113,3 @@ describe("formatSummary", () => {
115113 expect ( formatSummary ( varies ) ) . toBe ( "tempo varies (about 120 BPM): 822 beats, 227 downbeats, 2 drops, 9 sections" ) ;
116114 } ) ;
117115} ) ;
118-
119- describe ( "createClient" , ( ) => {
120- test ( "sends no API key when it has none" , async ( ) => {
121- const fetchMock = spyOn ( globalThis , "fetch" ) . mockResolvedValue ( new Response ( "{}" ) ) ;
122- try {
123- await createClient ( { env : { name : "v1" , baseUrl : "https://api.example.com" } } ) . get ( "/x" ) ;
124- expect ( fetchMock . mock . calls [ 0 ] ! [ 1 ] ! . headers ) . not . toHaveProperty ( "x-api-key" ) ;
125- } finally {
126- fetchMock . mockRestore ( ) ;
127- }
128- } ) ;
129- } ) ;
0 commit comments