@@ -429,6 +429,81 @@ describe("GET /projects/:id/renders/file/* — path safety", () => {
429429 } ) ;
430430} ) ;
431431
432+ describe ( "POST /render/:jobId/cancel" , ( ) => {
433+ async function startJob ( app : Hono ) : Promise < string > {
434+ const res = await app . request ( "http://localhost/projects/demo/render" , {
435+ method : "POST" ,
436+ headers : { "content-type" : "application/json" } ,
437+ body : JSON . stringify ( { fps : 30 , quality : "standard" , format : "mp4" } ) ,
438+ } ) ;
439+ expect ( res . status ) . toBe ( 200 ) ;
440+ return ( ( await res . json ( ) ) as { jobId : string } ) . jobId ;
441+ }
442+
443+ it ( "marks a rendering job cancelled and invokes the adapter abort hook" , async ( ) => {
444+ const spy = vi . fn ( ) ;
445+ let aborted = false ;
446+ const { adapter, rendersDir } = createAdapter ( spy ) ;
447+ const baseStartRender = adapter . startRender . bind ( adapter ) ;
448+ adapter . startRender = ( opts ) => {
449+ const state = baseStartRender ( opts ) ;
450+ state . cancel = ( ) => {
451+ aborted = true ;
452+ } ;
453+ return state ;
454+ } ;
455+ const app = new Hono ( ) ;
456+ registerRenderRoutes ( app , adapter ) ;
457+ try {
458+ const jobId = await startJob ( app ) ;
459+ const res = await app . request ( `http://localhost/render/${ jobId } /cancel` , { method : "POST" } ) ;
460+ expect ( res . status ) . toBe ( 200 ) ;
461+ expect ( ( ( await res . json ( ) ) as { status : string } ) . status ) . toBe ( "cancelled" ) ;
462+ expect ( aborted ) . toBe ( true ) ;
463+ // SSE progress for a cancelled job must terminate (status is terminal).
464+ const progress = await app . request ( `http://localhost/render/${ jobId } /progress` ) ;
465+ expect ( progress . status ) . toBe ( 200 ) ;
466+ } finally {
467+ rmSync ( rendersDir , { recursive : true , force : true } ) ;
468+ }
469+ } ) ;
470+
471+ it ( "does not cancel a job that already completed" , async ( ) => {
472+ const spy = vi . fn ( ) ;
473+ const states : Array < { status : string } > = [ ] ;
474+ const { adapter, rendersDir } = createAdapter ( spy ) ;
475+ const baseStartRender = adapter . startRender . bind ( adapter ) ;
476+ adapter . startRender = ( opts ) => {
477+ const state = baseStartRender ( opts ) ;
478+ states . push ( state ) ;
479+ return state ;
480+ } ;
481+ const app = new Hono ( ) ;
482+ registerRenderRoutes ( app , adapter ) ;
483+ try {
484+ const jobId = await startJob ( app ) ;
485+ const [ state ] = states ;
486+ if ( state ) state . status = "complete" ;
487+ const res = await app . request ( `http://localhost/render/${ jobId } /cancel` , { method : "POST" } ) ;
488+ expect ( res . status ) . toBe ( 200 ) ;
489+ expect ( ( ( await res . json ( ) ) as { status : string } ) . status ) . toBe ( "complete" ) ;
490+ } finally {
491+ rmSync ( rendersDir , { recursive : true , force : true } ) ;
492+ }
493+ } ) ;
494+
495+ it ( "404s for unknown jobs" , async ( ) => {
496+ const spy = vi . fn ( ) ;
497+ const { app, cleanup } = buildApp ( spy ) ;
498+ try {
499+ const res = await app . request ( "http://localhost/render/nope/cancel" , { method : "POST" } ) ;
500+ expect ( res . status ) . toBe ( 404 ) ;
501+ } finally {
502+ cleanup ( ) ;
503+ }
504+ } ) ;
505+ } ) ;
506+
432507describe ( "POST /projects/:id/render — telemetryDistinctId forwarding" , ( ) => {
433508 it ( "forwards the browser telemetryDistinctId to the adapter as distinctId" , async ( ) => {
434509 const spy = vi . fn ( ) ;
0 commit comments