11import { Sandbox } from "@e2b/code-interpreter" ;
2+ import { log , runTestWithSandbox } from "../utils.ts" ;
23
34// Helper function to stream command output
45async function streamCommandOutput ( command : string , args : string [ ] ) {
@@ -15,7 +16,7 @@ async function streamCommandOutput(command: string, args: string[]) {
1516
1617 const readStream = async (
1718 stream : ReadableStream < Uint8Array > ,
18- logFn : ( msg : Uint8Array ) => void ,
19+ logFn : ( msg : Uint8Array ) => void
1920 ) => {
2021 for await ( const chunk of stream ) {
2122 logFn ( chunk ) ;
@@ -38,6 +39,22 @@ async function streamCommandOutput(command: string, args: string[]) {
3839 return { status, output } ;
3940}
4041
42+ async function deleteTemplate ( templateID : string ) {
43+ const output = await streamCommandOutput ( "deno" , [
44+ "run" ,
45+ "--allow-all" ,
46+ "@e2b/cli" ,
47+ "template" ,
48+ "delete" ,
49+ "-y" ,
50+ templateID ,
51+ ] ) ;
52+
53+ if ( output . status . code !== 0 ) {
54+ throw new Error ( `❌ Delete failed with code ${ output . status . code } ` ) ;
55+ }
56+ }
57+
4158const uniqueID = crypto . randomUUID ( ) ;
4259const templateName = `test-template-${ uniqueID } ` ;
4360console . log ( "ℹ️ templateName:" , templateName ) ;
@@ -73,53 +90,43 @@ if (!templateID) {
7390// sleep for 15 seconds to create a time delta
7491await new Promise ( ( resolve ) => setTimeout ( resolve , 15000 ) ) ;
7592
76- try {
77- // remove the file to make script idempotent in local testing
78- await Deno . remove ( "e2b.toml" ) ;
93+ // remove the file to make script idempotent in local testing
94+ await Deno . remove ( "e2b.toml" ) ;
7995
80- if ( ! templateID ) {
81- throw new Error ( "❌ Template not found" ) ;
82- }
83- console . log ( "ℹ️ creating sandbox" ) ;
84- const sandbox = await Sandbox . create ( templateID , { timeoutMs : 10000 } ) ;
85- console . log ( "ℹ️ sandbox created" , sandbox . sandboxId ) ;
86-
87- console . log ( "ℹ️ running command" ) ;
88-
89- console . log ( "ℹ️ starting command" ) ;
90- const localDateStart = new Date ( ) . getTime ( ) / 1000 ;
91- const date = await sandbox . commands . run ( "date +%s%3N" ) ;
92- const localDateEnd = new Date ( ) . getTime ( ) / 1000 ;
93- const dateUnix = parseFloat ( date . stdout ) / 1000 ;
94-
95- console . log ( "local date - start of request" , localDateStart ) ;
96- console . log ( "local date - end of request" , localDateEnd ) ;
97- console . log ( "sandbox date" , dateUnix ) ;
98-
99- // check if the diff between sandbox time and local time is less than 1 second (taking into consideration the request latency)
100- if ( dateUnix < localDateStart - 1 || dateUnix > localDateEnd + 1 ) {
101- throw new Error ( "❌ Date is not synchronized" ) ;
102- }
103-
104- console . log ( "✅ date is synchronized" ) ;
96+ if ( ! templateID ) {
97+ throw new Error ( "❌ Template not found" ) ;
98+ }
10599
106- // kill sandbox
107- await sandbox . kill ( ) ;
100+ log ( "ℹ️ creating sandbox" ) ;
101+ let sandbox : Sandbox ;
102+ try {
103+ sandbox = await Sandbox . create ( templateID , { timeoutMs : 10000 } ) ;
104+ log ( "ℹ️ sandbox created" , sandbox . sandboxId ) ;
108105} catch ( e ) {
109- console . error ( "Error while running sandbox or commands" , e ) ;
106+ await deleteTemplate ( templateID ) ;
110107 throw e ;
111- } finally { // delete template
112- const output = await streamCommandOutput ( "deno" , [
113- "run" ,
114- "--allow-all" ,
115- "@e2b/cli" ,
116- "template" ,
117- "delete" ,
118- "-y" ,
119- templateID ,
120- ] ) ;
108+ }
121109
122- if ( output . status . code !== 0 ) {
123- throw new Error ( `❌ Delete failed with code ${ output . status . code } ` ) ;
124- }
110+ try {
111+ await runTestWithSandbox ( sandbox , "time-is-synchronized" , async ( ) => {
112+ log ( "ℹ️ starting command" ) ;
113+ const localDateStart = new Date ( ) . getTime ( ) / 1000 ;
114+ const date = await sandbox . commands . run ( "date +%s%3N" ) ;
115+ const localDateEnd = new Date ( ) . getTime ( ) / 1000 ;
116+ const dateUnix = parseFloat ( date . stdout ) / 1000 ;
117+
118+ log ( "local date - start of request" , localDateStart ) ;
119+ log ( "local date - end of request" , localDateEnd ) ;
120+ log ( "sandbox date" , dateUnix ) ;
121+
122+ // check if the diff between sandbox time and local time is less than 1 second (taking into consideration the request latency)
123+ if ( dateUnix < localDateStart - 1 || dateUnix > localDateEnd + 1 ) {
124+ throw new Error ( "❌ Date is not synchronized" ) ;
125+ }
126+
127+ log ( "✅ date is synchronized" ) ;
128+ } ) ;
129+ } finally {
130+ // delete template
131+ await deleteTemplate ( templateID ) ;
125132}
0 commit comments