1+ // @effect -diagnostics nodeBuiltinImport:off - directory aliases use junctions on Windows,
2+ // which Effect FileSystem.symlink does not expose.
3+ import * as NodeFSP from "node:fs/promises" ;
4+
15import * as NodeServices from "@effect/platform-node/NodeServices" ;
26import { assert , describe , it } from "@effect/vitest" ;
7+ import { DEFAULT_SERVER_SETTINGS , ProviderInstanceId } from "@t3tools/contracts" ;
8+ import type { ProviderUsageInstance } from "@t3tools/provider-core/server/usage" ;
9+ import * as HostProcess from "@t3tools/shared/HostProcess" ;
310import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient" ;
411import * as Context from "effect/Context" ;
512import * as Effect from "effect/Effect" ;
@@ -8,14 +15,51 @@ import * as Layer from "effect/Layer";
815import * as Path from "effect/Path" ;
916import * as SqlClient from "effect/sql/SqlClient" ;
1017
11- import { readOpenCodeUsage } from "./usage.ts" ;
18+ import type { OpenCodeSettings } from "../settings.ts" ;
19+ import { openCodeUsageReader , readOpenCodeUsage } from "./usage.ts" ;
1220
1321/** A writable connection that stays open until the test's scope closes. */
1422const openDatabase = ( filename : string ) =>
1523 Layer . build ( NodeSqliteClient . layer ( { filename } ) ) . pipe (
1624 Effect . map ( Context . get ( SqlClient . SqlClient ) ) ,
1725 ) ;
1826
27+ const seedHistory = Effect . fn ( "seedHistory" ) ( function * ( root : string , id : string ) {
28+ const fileSystem = yield * FileSystem . FileSystem ;
29+ const path = yield * Path . Path ;
30+ yield * fileSystem . makeDirectory ( root , { recursive : true } ) ;
31+ const db = yield * openDatabase ( path . join ( root , "opencode.db" ) ) ;
32+ yield * db . unsafe ( "CREATE TABLE message (id TEXT, session_id TEXT, data TEXT)" ) ;
33+ yield * db . unsafe ( "INSERT INTO message VALUES (?, ?, ?)" , [
34+ id ,
35+ "session-1" ,
36+ JSON . stringify ( {
37+ role : "assistant" ,
38+ modelID : "claude-sonnet-4-5" ,
39+ time : { created : 1780000000000 } ,
40+ tokens : { input : 100 , output : 20 } ,
41+ } ) ,
42+ ] ) ;
43+ } ) ;
44+
45+ const usageInstance = ( id : string , environment : NodeJS . ProcessEnv , configured = true ) => ( {
46+ instanceId : ProviderInstanceId . make ( id ) ,
47+ config : undefined ,
48+ environment,
49+ configured,
50+ } ) ;
51+
52+ const scanUsage = ( instances : readonly ProviderUsageInstance < OpenCodeSettings > [ ] ) => {
53+ if ( openCodeUsageReader . kind !== "scan" ) throw new Error ( "Expected an OpenCode scan reader" ) ;
54+ return openCodeUsageReader . scan ( {
55+ instances,
56+ settings : DEFAULT_SERVER_SETTINGS ,
57+ windowStartMs : 0 ,
58+ retentionCutoffMs : 0 ,
59+ awaitRefresh : true ,
60+ } ) ;
61+ } ;
62+
1963describe ( "readOpenCodeUsage" , ( ) => {
2064 it . effect ( "counts migrated OpenCode messages once and sees subsequent WAL writes" , ( ) =>
2165 Effect . gen ( function * ( ) {
@@ -68,3 +112,125 @@ describe("readOpenCodeUsage", () => {
68112 } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
69113 ) ;
70114} ) ;
115+
116+ describe ( "openCodeUsageReader" , ( ) => {
117+ it . effect ( "reads histories from each instance's environment" , ( ) =>
118+ Effect . gen ( function * ( ) {
119+ const fileSystem = yield * FileSystem . FileSystem ;
120+ const path = yield * Path . Path ;
121+ const temp = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "usage-instances-test-" } ) ;
122+ const home = yield * fileSystem . realPath ( temp ) ;
123+ const first = path . join ( home , "first" ) ;
124+ const dataHome = path . join ( home , "second" ) ;
125+ const second = path . join ( dataHome , "opencode" ) ;
126+ yield * seedHistory ( first , "msg-first" ) ;
127+ yield * seedHistory ( second , "msg-second" ) ;
128+
129+ const sources = yield * scanUsage ( [
130+ usageInstance ( "opencode-first" , {
131+ OPENCODE_DATA_DIR : " ~/first, , " ,
132+ XDG_DATA_HOME : path . join ( home , "unused" ) ,
133+ } ) ,
134+ usageInstance ( "opencode-second" , { XDG_DATA_HOME : ` ${ dataHome } ` } ) ,
135+ ] ) . pipe (
136+ Effect . provideService ( HostProcess . Environment , {
137+ OPENCODE_DATA_DIR : path . join ( home , "host" ) ,
138+ } ) ,
139+ Effect . provideService ( HostProcess . HomeDirectory , home ) ,
140+ ) ;
141+
142+ assert . deepStrictEqual (
143+ sources . map ( ( source ) => source . dir ) ,
144+ [ first , second ] ,
145+ ) ;
146+ assert . deepStrictEqual (
147+ sources . map ( ( source ) => source . status ) ,
148+ [ "ok" , "ok" ] ,
149+ ) ;
150+ const records = sources . flatMap (
151+ ( source ) => source . files ?. flatMap ( ( file ) => file . records ) ?? [ ] ,
152+ ) ;
153+ assert . deepStrictEqual (
154+ records . map ( ( record ) => record . dedupeKey ) ,
155+ [ "opencode:msg-first" , "opencode:msg-second" ] ,
156+ ) ;
157+ assert . strictEqual (
158+ records . reduce ( ( sum , record ) => sum + record . totals . uncachedInputTokens , 0 ) ,
159+ 200 ,
160+ ) ;
161+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
162+ ) ;
163+
164+ it . effect ( "scans canonical aliases shared by instances only once" , ( ) =>
165+ Effect . gen ( function * ( ) {
166+ const fileSystem = yield * FileSystem . FileSystem ;
167+ const path = yield * Path . Path ;
168+ const temp = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "usage-aliases-test-" } ) ;
169+ const home = yield * fileSystem . realPath ( temp ) ;
170+ const root = path . join ( home , "store" ) ;
171+ const alias = path . join ( home , "alias" ) ;
172+ yield * seedHistory ( root , "msg-shared" ) ;
173+ yield * Effect . tryPromise ( ( ) => NodeFSP . symlink ( root , alias , "junction" ) ) ;
174+ const sources = yield * scanUsage ( [
175+ usageInstance ( "opencode-first" , { OPENCODE_DATA_DIR : ` ${ root } , ${ alias } ` } ) ,
176+ usageInstance ( "opencode-second" , { OPENCODE_DATA_DIR : alias } ) ,
177+ ] ) . pipe (
178+ Effect . provideService ( HostProcess . Environment , {
179+ OPENCODE_DATA_DIR : path . join ( home , "host" ) ,
180+ } ) ,
181+ ) ;
182+
183+ assert . deepStrictEqual (
184+ sources . map ( ( source ) => source . dir ) ,
185+ [ root ] ,
186+ ) ;
187+ assert . deepStrictEqual (
188+ sources . flatMap (
189+ ( source ) =>
190+ source . files ?. flatMap ( ( file ) => file . records . map ( ( record ) => record . dedupeKey ) ) ?? [ ] ,
191+ ) ,
192+ [ "opencode:msg-shared" ] ,
193+ ) ;
194+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
195+ ) ;
196+
197+ it . effect . each ( [
198+ { dataHome : "absolute" , dataDirs : undefined } ,
199+ { dataHome : "relative" , dataDirs : " , " } ,
200+ { dataHome : "absent" , dataDirs : undefined } ,
201+ ] ) ( "retains the implicit host default for $dataHome XDG_DATA_HOME" , ( { dataHome, dataDirs } ) =>
202+ Effect . gen ( function * ( ) {
203+ const fileSystem = yield * FileSystem . FileSystem ;
204+ const path = yield * Path . Path ;
205+ const temp = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "usage-default-test-" } ) ;
206+ const home = yield * fileSystem . realPath ( temp ) ;
207+ const root = path . join ( home , dataHome === "absolute" ? "xdg" : ".local/share" , "opencode" ) ;
208+ const environment = {
209+ OPENCODE_DATA_DIR : dataDirs ,
210+ XDG_DATA_HOME :
211+ dataHome === "absolute"
212+ ? path . join ( home , "xdg" )
213+ : dataHome === "relative"
214+ ? "relative"
215+ : undefined ,
216+ } ;
217+ yield * seedHistory ( root , "msg-default" ) ;
218+
219+ const sources = yield * scanUsage ( [ usageInstance ( "opencode" , environment , false ) ] ) . pipe (
220+ Effect . provideService ( HostProcess . Environment , environment ) ,
221+ Effect . provideService ( HostProcess . HomeDirectory , home ) ,
222+ ) ;
223+ assert . deepStrictEqual (
224+ sources . map ( ( source ) => source . dir ) ,
225+ [ root ] ,
226+ ) ;
227+ assert . deepStrictEqual (
228+ sources . flatMap (
229+ ( source ) =>
230+ source . files ?. flatMap ( ( file ) => file . records . map ( ( record ) => record . dedupeKey ) ) ?? [ ] ,
231+ ) ,
232+ [ "opencode:msg-default" ] ,
233+ ) ;
234+ } ) . pipe ( Effect . provide ( NodeServices . layer ) ) ,
235+ ) ;
236+ } ) ;
0 commit comments