@@ -6,6 +6,7 @@ import assert from "node:assert/strict";
66import fs from "node:fs" ;
77import os from "node:os" ;
88import path from "node:path" ;
9+ import { pathToFileURL } from "node:url" ;
910import { testing as voiceCallCliTesting } from "../../extensions/voice-call/src/cli.ts" ;
1011import { loadSessionLogs , loadSessionUsageTimeSeries } from "../../src/infra/session-cost-usage.ts" ;
1112import {
@@ -14,6 +15,15 @@ import {
1415 resetDiagnosticPhasesForTest ,
1516} from "../../src/logging/diagnostic-phase.ts" ;
1617
18+ export async function withProofTempRoot ( callback ) {
19+ const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-proof-" ) ) ;
20+ try {
21+ return await callback ( root ) ;
22+ } finally {
23+ fs . rmSync ( root , { force : true , recursive : true } ) ;
24+ }
25+ }
26+
1727async function main ( ) {
1828 resetDiagnosticPhasesForTest ( ) ;
1929 recordDiagnosticPhase ( {
@@ -40,77 +50,80 @@ async function main() {
4050 assert . equal ( zeroPhases . length , 0 ) ;
4151 console . log ( "getRecentDiagnosticPhases(0).length =" , zeroPhases . length ) ;
4252
43- const root = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-proof-" ) ) ;
44- const sessionFile = path . join ( root , "s.jsonl" ) ;
45- fs . writeFileSync (
46- sessionFile ,
47- [
48- JSON . stringify ( {
49- type : "message" ,
50- timestamp : "2026-01-01T00:00:00.000Z" ,
51- message : { role : "user" , content : "a" } ,
52- } ) ,
53- JSON . stringify ( {
54- type : "message" ,
55- timestamp : "2026-01-01T00:01:00.000Z" ,
56- message : {
57- role : "assistant" ,
58- content : "b" ,
59- provider : "openai" ,
60- model : "gpt-5.5" ,
61- usage : {
62- input : 1 ,
63- output : 2 ,
64- cacheRead : 0 ,
65- cacheWrite : 0 ,
66- totalTokens : 3 ,
67- cost : { total : 0.001 } ,
53+ await withProofTempRoot ( async ( root ) => {
54+ const sessionFile = path . join ( root , "s.jsonl" ) ;
55+ fs . writeFileSync (
56+ sessionFile ,
57+ [
58+ JSON . stringify ( {
59+ type : "message" ,
60+ timestamp : "2026-01-01T00:00:00.000Z" ,
61+ message : { role : "user" , content : "a" } ,
62+ } ) ,
63+ JSON . stringify ( {
64+ type : "message" ,
65+ timestamp : "2026-01-01T00:01:00.000Z" ,
66+ message : {
67+ role : "assistant" ,
68+ content : "b" ,
69+ provider : "openai" ,
70+ model : "gpt-5.5" ,
71+ usage : {
72+ input : 1 ,
73+ output : 2 ,
74+ cacheRead : 0 ,
75+ cacheWrite : 0 ,
76+ totalTokens : 3 ,
77+ cost : { total : 0.001 } ,
78+ } ,
6879 } ,
69- } ,
70- } ) ,
71- JSON . stringify ( {
72- type : "message " ,
73- timestamp : "2026-01-01T00:02:00.000Z" ,
74- message : {
75- role : "assistant " ,
76- content : "c " ,
77- provider : "openai " ,
78- model : "gpt-5.5" ,
79- usage : {
80- input : 3 ,
81- output : 4 ,
82- cacheRead : 0 ,
83- cacheWrite : 0 ,
84- totalTokens : 7 ,
85- cost : { total : 0.002 } ,
80+ } ) ,
81+ JSON . stringify ( {
82+ type : "message" ,
83+ timestamp : "2026-01-01T00:02:00.000Z " ,
84+ message : {
85+ role : "assistant" ,
86+ content : "c " ,
87+ provider : "openai " ,
88+ model : "gpt-5.5 " ,
89+ usage : {
90+ input : 3 ,
91+ output : 4 ,
92+ cacheRead : 0 ,
93+ cacheWrite : 0 ,
94+ totalTokens : 7 ,
95+ cost : { total : 0.002 } ,
96+ } ,
8697 } ,
87- } ,
88- } ) ,
89- ] . join ( "\n" ) ,
90- ) ;
98+ } ) ,
99+ ] . join ( "\n" ) ,
100+ ) ;
91101
92- const logs = await loadSessionLogs ( { sessionFile, limit : 0 } ) ;
93- const series = await loadSessionUsageTimeSeries ( { sessionFile, maxPoints : 0 } ) ;
94- const positiveLogs = await loadSessionLogs ( { sessionFile, limit : 10 } ) ;
95- const positiveSeries = await loadSessionUsageTimeSeries ( { sessionFile, maxPoints : 10 } ) ;
96- assert . equal ( logs ?. length , 0 ) ;
97- assert . equal ( series . points . length , 0 ) ;
98- assert . equal ( positiveLogs ?. length , 3 ) ;
99- assert . equal ( positiveSeries . points . length , 2 ) ;
100- console . log ( "loadSessionLogs({ limit: 0 }).length =" , logs ?. length ) ;
101- console . log (
102- "loadSessionUsageTimeSeries({ maxPoints: 0 }).points.length =" ,
103- series ?. points . length ,
104- ) ;
102+ const logs = await loadSessionLogs ( { sessionFile, limit : 0 } ) ;
103+ const series = await loadSessionUsageTimeSeries ( { sessionFile, maxPoints : 0 } ) ;
104+ const positiveLogs = await loadSessionLogs ( { sessionFile, limit : 10 } ) ;
105+ const positiveSeries = await loadSessionUsageTimeSeries ( { sessionFile, maxPoints : 10 } ) ;
106+ assert . equal ( logs ?. length , 0 ) ;
107+ assert . equal ( series . points . length , 0 ) ;
108+ assert . equal ( positiveLogs ?. length , 3 ) ;
109+ assert . equal ( positiveSeries . points . length , 2 ) ;
110+ console . log ( "loadSessionLogs({ limit: 0 }).length =" , logs ?. length ) ;
111+ console . log (
112+ "loadSessionUsageTimeSeries({ maxPoints: 0 }).points.length =" ,
113+ series ?. points . length ,
114+ ) ;
105115
106- try {
107- voiceCallCliTesting . parseVoiceCallIntOption ( "nope" , "--port" , { min : 1 } ) ;
108- assert . fail ( "expected invalid voicecall --port value to throw" ) ;
109- } catch ( error ) {
110- const message = error instanceof Error ? error . message : String ( error ) ;
111- assert . equal ( message , "Invalid numeric value for --port: nope" ) ;
112- console . log ( "parseVoiceCallIntOption('nope', '--port') error:" , message ) ;
113- }
116+ try {
117+ voiceCallCliTesting . parseVoiceCallIntOption ( "nope" , "--port" , { min : 1 } ) ;
118+ assert . fail ( "expected invalid voicecall --port value to throw" ) ;
119+ } catch ( error ) {
120+ const message = error instanceof Error ? error . message : String ( error ) ;
121+ assert . equal ( message , "Invalid numeric value for --port: nope" ) ;
122+ console . log ( "parseVoiceCallIntOption('nope', '--port') error:" , message ) ;
123+ }
124+ } ) ;
114125}
115126
116- await main ( ) ;
127+ if ( process . argv [ 1 ] && import . meta. url === pathToFileURL ( process . argv [ 1 ] ) . href ) {
128+ await main ( ) ;
129+ }
0 commit comments