11import { beforeEach , describe , expect , it , vi } from "vitest" ;
22
3- const { mockFetch } = vi . hoisted ( ( ) => ( {
3+ const { mockFetch, mockRunTaskInCloud } = vi . hoisted ( ( ) => ( {
44 mockFetch : vi . fn ( ) ,
5+ mockRunTaskInCloud : vi . fn ( ) ,
56} ) ) ;
67
78vi . mock ( "expo/fetch" , ( ) => ( {
@@ -34,6 +35,10 @@ vi.mock("@/lib/api", () => ({
3435 } ) ,
3536} ) ) ;
3637
38+ vi . mock ( "@/lib/posthogApiClient" , ( ) => ( {
39+ getPostHogApiClient : ( ) => ( { runTaskInCloud : mockRunTaskInCloud } ) ,
40+ } ) ) ;
41+
3742import { cancelRun , HttpError , runTaskInCloud } from "./api" ;
3843
3944function bodyOf ( call : unknown ) : Record < string , unknown > {
@@ -43,34 +48,37 @@ function bodyOf(call: unknown): Record<string, unknown> {
4348
4449describe ( "runTaskInCloud" , ( ) => {
4550 beforeEach ( ( ) => {
46- mockFetch . mockReset ( ) ;
47- mockFetch . mockResolvedValue (
48- new Response ( JSON . stringify ( { id : "task-1" } ) , { status : 200 } ) ,
49- ) ;
51+ mockRunTaskInCloud . mockReset ( ) ;
52+ mockRunTaskInCloud . mockResolvedValue ( { id : "task-1" } ) ;
5053 } ) ;
5154
5255 it . each ( [ true , false ] ) (
5356 "forwards auto_publish=%s to the payload" ,
5457 async ( flag ) => {
5558 await runTaskInCloud ( "task-1" , { autoPublish : flag } ) ;
5659
57- expect ( bodyOf ( mockFetch . mock . calls [ 0 ] ) ) . toMatchObject ( {
58- auto_publish : flag ,
59- } ) ;
60+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
61+ "task-1" ,
62+ undefined ,
63+ expect . objectContaining ( { autoPublish : flag } ) ,
64+ ) ;
6065 } ,
6166 ) ;
6267
6368 it ( "omits auto_publish when not provided" , async ( ) => {
6469 await runTaskInCloud ( "task-1" , { model : "claude-opus-4-8" } ) ;
6570
66- expect ( bodyOf ( mockFetch . mock . calls [ 0 ] ) ) . not . toHaveProperty ( "auto_publish" ) ;
71+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
72+ "task-1" ,
73+ undefined ,
74+ expect . objectContaining ( { autoPublish : undefined } ) ,
75+ ) ;
6776 } ) ;
6877
6978 it ( "sends no body for the plain initial run" , async ( ) => {
7079 await runTaskInCloud ( "task-1" ) ;
7180
72- const [ , init ] = mockFetch . mock . calls [ 0 ] as [ string , RequestInit ] ;
73- expect ( init . body ) . toBeUndefined ( ) ;
81+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith ( "task-1" ) ;
7482 } ) ;
7583
7684 it ( "forwards the selected sandbox environment and custom image" , async ( ) => {
@@ -79,10 +87,14 @@ describe("runTaskInCloud", () => {
7987 customImageId : "image-123" ,
8088 } ) ;
8189
82- expect ( bodyOf ( mockFetch . mock . calls [ 0 ] ) ) . toMatchObject ( {
83- sandbox_environment_id : "environment-123" ,
84- custom_image_id : "image-123" ,
85- } ) ;
90+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
91+ "task-1" ,
92+ undefined ,
93+ expect . objectContaining ( {
94+ sandboxEnvironmentId : "environment-123" ,
95+ customImageId : "image-123" ,
96+ } ) ,
97+ ) ;
8698 } ) ;
8799
88100 it ( "omits the sandbox environment and custom image when unset" , async ( ) => {
@@ -92,24 +104,34 @@ describe("runTaskInCloud", () => {
92104 customImageId : null ,
93105 } ) ;
94106
95- const body = bodyOf ( mockFetch . mock . calls [ 0 ] ) ;
96- expect ( body ) . not . toHaveProperty ( "sandbox_environment_id" ) ;
97- expect ( body ) . not . toHaveProperty ( "custom_image_id" ) ;
107+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
108+ "task-1" ,
109+ undefined ,
110+ expect . objectContaining ( {
111+ sandboxEnvironmentId : undefined ,
112+ customImageId : undefined ,
113+ } ) ,
114+ ) ;
98115 } ) ;
99116
100117 it ( "sends rtk_enabled=false when the run opts out" , async ( ) => {
101118 await runTaskInCloud ( "task-1" , { rtkEnabled : false } ) ;
102119
103- expect ( bodyOf ( mockFetch . mock . calls [ 0 ] ) ) . toMatchObject ( {
104- rtk_enabled : false ,
105- } ) ;
120+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
121+ "task-1" ,
122+ undefined ,
123+ expect . objectContaining ( { rtkEnabled : false } ) ,
124+ ) ;
106125 } ) ;
107126
108127 it ( "omits rtk_enabled when the run keeps compression on" , async ( ) => {
109128 await runTaskInCloud ( "task-1" , { rtkEnabled : true } ) ;
110129
111- const [ , init ] = mockFetch . mock . calls [ 0 ] as [ string , RequestInit ] ;
112- expect ( init . body ) . toBeUndefined ( ) ;
130+ expect ( mockRunTaskInCloud ) . toHaveBeenCalledWith (
131+ "task-1" ,
132+ undefined ,
133+ expect . objectContaining ( { rtkEnabled : true } ) ,
134+ ) ;
113135 } ) ;
114136} ) ;
115137
0 commit comments