11import { afterEach , beforeEach , describe , expect , test } from "bun:test"
22import path from "path"
3+ import { Effect } from "effect"
34import { ModelID , ProviderID } from "../../src/provider/schema"
45import { Instruction } from "../../src/session/instruction"
56import type { MessageV2 } from "../../src/session/message-v2"
@@ -8,6 +9,9 @@ import { MessageID, PartID, SessionID } from "../../src/session/schema"
89import { Global } from "../../src/global"
910import { tmpdir } from "../fixture/fixture"
1011
12+ const run = < A > ( effect : Effect . Effect < A , any , Instruction . Service > ) =>
13+ Effect . runPromise ( effect . pipe ( Effect . provide ( Instruction . defaultLayer ) ) )
14+
1115function loaded ( filepath : string ) : MessageV2 . WithParts [ ] {
1216 const sessionID = SessionID . make ( "session-loaded-1" )
1317 const messageID = MessageID . make ( "message-loaded-1" )
@@ -57,17 +61,22 @@ describe("Instruction.resolve", () => {
5761 } )
5862 await Instance . provide ( {
5963 directory : tmp . path ,
60- fn : async ( ) => {
61- const system = await Instruction . systemPaths ( )
62- expect ( system . has ( path . join ( tmp . path , "AGENTS.md" ) ) ) . toBe ( true )
64+ fn : ( ) =>
65+ run (
66+ Instruction . Service . use ( ( svc ) =>
67+ Effect . gen ( function * ( ) {
68+ const system = yield * svc . systemPaths ( )
69+ expect ( system . has ( path . join ( tmp . path , "AGENTS.md" ) ) ) . toBe ( true )
6370
64- const results = await Instruction . resolve (
65- [ ] ,
66- path . join ( tmp . path , "src" , "file.ts" ) ,
67- MessageID . make ( "message-test-1" ) ,
68- )
69- expect ( results ) . toEqual ( [ ] )
70- } ,
71+ const results = yield * svc . resolve (
72+ [ ] ,
73+ path . join ( tmp . path , "src" , "file.ts" ) ,
74+ MessageID . make ( "message-test-1" ) ,
75+ )
76+ expect ( results ) . toEqual ( [ ] )
77+ } ) ,
78+ ) ,
79+ ) ,
7180 } )
7281 } )
7382
@@ -80,18 +89,23 @@ describe("Instruction.resolve", () => {
8089 } )
8190 await Instance . provide ( {
8291 directory : tmp . path ,
83- fn : async ( ) => {
84- const system = await Instruction . systemPaths ( )
85- expect ( system . has ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) ) ) . toBe ( false )
92+ fn : ( ) =>
93+ run (
94+ Instruction . Service . use ( ( svc ) =>
95+ Effect . gen ( function * ( ) {
96+ const system = yield * svc . systemPaths ( )
97+ expect ( system . has ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) ) ) . toBe ( false )
8698
87- const results = await Instruction . resolve (
88- [ ] ,
89- path . join ( tmp . path , "subdir" , "nested" , "file.ts" ) ,
90- MessageID . make ( "message-test-2" ) ,
91- )
92- expect ( results . length ) . toBe ( 1 )
93- expect ( results [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
94- } ,
99+ const results = yield * svc . resolve (
100+ [ ] ,
101+ path . join ( tmp . path , "subdir" , "nested" , "file.ts" ) ,
102+ MessageID . make ( "message-test-2" ) ,
103+ )
104+ expect ( results . length ) . toBe ( 1 )
105+ expect ( results [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
106+ } ) ,
107+ ) ,
108+ ) ,
95109 } )
96110 } )
97111
@@ -104,14 +118,19 @@ describe("Instruction.resolve", () => {
104118 } )
105119 await Instance . provide ( {
106120 directory : tmp . path ,
107- fn : async ( ) => {
108- const filepath = path . join ( tmp . path , "subdir" , "AGENTS.md" )
109- const system = await Instruction . systemPaths ( )
110- expect ( system . has ( filepath ) ) . toBe ( false )
121+ fn : ( ) =>
122+ run (
123+ Instruction . Service . use ( ( svc ) =>
124+ Effect . gen ( function * ( ) {
125+ const filepath = path . join ( tmp . path , "subdir" , "AGENTS.md" )
126+ const system = yield * svc . systemPaths ( )
127+ expect ( system . has ( filepath ) ) . toBe ( false )
111128
112- const results = await Instruction . resolve ( [ ] , filepath , MessageID . make ( "message-test-3" ) )
113- expect ( results ) . toEqual ( [ ] )
114- } ,
129+ const results = yield * svc . resolve ( [ ] , filepath , MessageID . make ( "message-test-3" ) )
130+ expect ( results ) . toEqual ( [ ] )
131+ } ) ,
132+ ) ,
133+ ) ,
115134 } )
116135 } )
117136
@@ -124,17 +143,22 @@ describe("Instruction.resolve", () => {
124143 } )
125144 await Instance . provide ( {
126145 directory : tmp . path ,
127- fn : async ( ) => {
128- const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
129- const id = MessageID . make ( "message-claim-1" )
146+ fn : ( ) =>
147+ run (
148+ Instruction . Service . use ( ( svc ) =>
149+ Effect . gen ( function * ( ) {
150+ const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
151+ const id = MessageID . make ( "message-claim-1" )
130152
131- const first = await Instruction . resolve ( [ ] , filepath , id )
132- const second = await Instruction . resolve ( [ ] , filepath , id )
153+ const first = yield * svc . resolve ( [ ] , filepath , id )
154+ const second = yield * svc . resolve ( [ ] , filepath , id )
133155
134- expect ( first ) . toHaveLength ( 1 )
135- expect ( first [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
136- expect ( second ) . toEqual ( [ ] )
137- } ,
156+ expect ( first ) . toHaveLength ( 1 )
157+ expect ( first [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
158+ expect ( second ) . toEqual ( [ ] )
159+ } ) ,
160+ ) ,
161+ ) ,
138162 } )
139163 } )
140164
@@ -147,18 +171,23 @@ describe("Instruction.resolve", () => {
147171 } )
148172 await Instance . provide ( {
149173 directory : tmp . path ,
150- fn : async ( ) => {
151- const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
152- const id = MessageID . make ( "message-claim-2" )
174+ fn : ( ) =>
175+ run (
176+ Instruction . Service . use ( ( svc ) =>
177+ Effect . gen ( function * ( ) {
178+ const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
179+ const id = MessageID . make ( "message-claim-2" )
153180
154- const first = await Instruction . resolve ( [ ] , filepath , id )
155- await Instruction . clear ( id )
156- const second = await Instruction . resolve ( [ ] , filepath , id )
181+ const first = yield * svc . resolve ( [ ] , filepath , id )
182+ yield * svc . clear ( id )
183+ const second = yield * svc . resolve ( [ ] , filepath , id )
157184
158- expect ( first ) . toHaveLength ( 1 )
159- expect ( second ) . toHaveLength ( 1 )
160- expect ( second [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
161- } ,
185+ expect ( first ) . toHaveLength ( 1 )
186+ expect ( second ) . toHaveLength ( 1 )
187+ expect ( second [ 0 ] . filepath ) . toBe ( path . join ( tmp . path , "subdir" , "AGENTS.md" ) )
188+ } ) ,
189+ ) ,
190+ ) ,
162191 } )
163192 } )
164193
@@ -171,15 +200,19 @@ describe("Instruction.resolve", () => {
171200 } )
172201 await Instance . provide ( {
173202 directory : tmp . path ,
174- fn : async ( ) => {
175- const agents = path . join ( tmp . path , "subdir" , "AGENTS.md" )
176- const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
177- const id = MessageID . make ( "message-claim-3" )
203+ fn : ( ) =>
204+ run (
205+ Instruction . Service . use ( ( svc ) =>
206+ Effect . gen ( function * ( ) {
207+ const agents = path . join ( tmp . path , "subdir" , "AGENTS.md" )
208+ const filepath = path . join ( tmp . path , "subdir" , "nested" , "file.ts" )
209+ const id = MessageID . make ( "message-claim-3" )
178210
179- const results = await Instruction . resolve ( loaded ( agents ) , filepath , id )
180-
181- expect ( results ) . toEqual ( [ ] )
182- } ,
211+ const results = yield * svc . resolve ( loaded ( agents ) , filepath , id )
212+ expect ( results ) . toEqual ( [ ] )
213+ } ) ,
214+ ) ,
215+ ) ,
183216 } )
184217 } )
185218
@@ -221,11 +254,16 @@ describe("Instruction.systemPaths OPENCODE_CONFIG_DIR", () => {
221254 try {
222255 await Instance . provide ( {
223256 directory : projectTmp . path ,
224- fn : async ( ) => {
225- const paths = await Instruction . systemPaths ( )
226- expect ( paths . has ( path . join ( profileTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
227- expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( false )
228- } ,
257+ fn : ( ) =>
258+ run (
259+ Instruction . Service . use ( ( svc ) =>
260+ Effect . gen ( function * ( ) {
261+ const paths = yield * svc . systemPaths ( )
262+ expect ( paths . has ( path . join ( profileTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
263+ expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( false )
264+ } ) ,
265+ ) ,
266+ ) ,
229267 } )
230268 } finally {
231269 ; ( Global . Path as { config : string } ) . config = originalGlobalConfig
@@ -248,11 +286,16 @@ describe("Instruction.systemPaths OPENCODE_CONFIG_DIR", () => {
248286 try {
249287 await Instance . provide ( {
250288 directory : projectTmp . path ,
251- fn : async ( ) => {
252- const paths = await Instruction . systemPaths ( )
253- expect ( paths . has ( path . join ( profileTmp . path , "AGENTS.md" ) ) ) . toBe ( false )
254- expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
255- } ,
289+ fn : ( ) =>
290+ run (
291+ Instruction . Service . use ( ( svc ) =>
292+ Effect . gen ( function * ( ) {
293+ const paths = yield * svc . systemPaths ( )
294+ expect ( paths . has ( path . join ( profileTmp . path , "AGENTS.md" ) ) ) . toBe ( false )
295+ expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
296+ } ) ,
297+ ) ,
298+ ) ,
256299 } )
257300 } finally {
258301 ; ( Global . Path as { config : string } ) . config = originalGlobalConfig
@@ -274,10 +317,15 @@ describe("Instruction.systemPaths OPENCODE_CONFIG_DIR", () => {
274317 try {
275318 await Instance . provide ( {
276319 directory : projectTmp . path ,
277- fn : async ( ) => {
278- const paths = await Instruction . systemPaths ( )
279- expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
280- } ,
320+ fn : ( ) =>
321+ run (
322+ Instruction . Service . use ( ( svc ) =>
323+ Effect . gen ( function * ( ) {
324+ const paths = yield * svc . systemPaths ( )
325+ expect ( paths . has ( path . join ( globalTmp . path , "AGENTS.md" ) ) ) . toBe ( true )
326+ } ) ,
327+ ) ,
328+ ) ,
281329 } )
282330 } finally {
283331 ; ( Global . Path as { config : string } ) . config = originalGlobalConfig
0 commit comments