11import { expect , spyOn , test } from "bun:test"
22import fs from "fs/promises"
33import path from "path"
4+ import { Effect } from "effect"
45import { pathToFileURL } from "url"
56import { tmpdir } from "../../fixture/fixture"
67import { createTuiPluginApi } from "../../fixture/tui-plugin"
7- import { TuiConfig } from "../../../src/config/ tui"
8+ import { mockTuiService } from "../../fixture/ tui-runtime "
89
910const { TuiPluginRuntime } = await import ( "../../../src/cli/cmd/tui/plugin/runtime" )
1011
@@ -31,11 +32,10 @@ test("adds tui plugin at runtime from spec", async () => {
3132 } )
3233
3334 process . env . OPENCODE_PLUGIN_META_FILE = path . join ( tmp . path , "plugin-meta.json" )
34- const get = spyOn ( TuiConfig , "get" ) . mockResolvedValue ( {
35+ const restore = mockTuiService ( {
3536 plugin : [ ] ,
3637 plugin_origins : undefined ,
3738 } )
38- const wait = spyOn ( TuiConfig , "waitForDependencies" ) . mockResolvedValue ( )
3939 const cwd = spyOn ( process , "cwd" ) . mockImplementation ( ( ) => tmp . path )
4040
4141 try {
@@ -54,8 +54,7 @@ test("adds tui plugin at runtime from spec", async () => {
5454 } finally {
5555 await TuiPluginRuntime . dispose ( )
5656 cwd . mockRestore ( )
57- get . mockRestore ( )
58- wait . mockRestore ( )
57+ restore ( )
5958 delete process . env . OPENCODE_PLUGIN_META_FILE
6059 }
6160} )
@@ -72,36 +71,39 @@ test("retries runtime add for file plugins after dependency wait", async () => {
7271 } )
7372
7473 process . env . OPENCODE_PLUGIN_META_FILE = path . join ( tmp . path , "plugin-meta.json" )
75- const get = spyOn ( TuiConfig , "get" ) . mockResolvedValue ( {
76- plugin : [ ] ,
77- plugin_origins : undefined ,
78- } )
79- const wait = spyOn ( TuiConfig , "waitForDependencies" ) . mockImplementation ( async ( ) => {
80- await Bun . write (
81- path . join ( tmp . extra . mod , "index.ts" ) ,
82- `export default {
74+ const restore = mockTuiService (
75+ {
76+ plugin : [ ] ,
77+ plugin_origins : undefined ,
78+ } ,
79+ {
80+ wait : ( ) =>
81+ Effect . promise ( async ( ) => {
82+ await Bun . write (
83+ path . join ( tmp . extra . mod , "index.ts" ) ,
84+ `export default {
8385 id: "demo.add.retry",
8486 tui: async () => {
8587 await Bun.write(${ JSON . stringify ( tmp . extra . marker ) } , "called")
8688 },
8789}
8890` ,
89- )
90- } )
91+ )
92+ } ) ,
93+ } ,
94+ )
9195 const cwd = spyOn ( process , "cwd" ) . mockImplementation ( ( ) => tmp . path )
9296
9397 try {
9498 await TuiPluginRuntime . init ( createTuiPluginApi ( ) )
9599
96100 await expect ( TuiPluginRuntime . addPlugin ( tmp . extra . spec ) ) . resolves . toBe ( true )
97101 await expect ( fs . readFile ( tmp . extra . marker , "utf8" ) ) . resolves . toBe ( "called" )
98- expect ( wait ) . toHaveBeenCalledTimes ( 1 )
99102 expect ( TuiPluginRuntime . list ( ) . find ( ( item ) => item . id === "demo.add.retry" ) ?. active ) . toBe ( true )
100103 } finally {
101104 await TuiPluginRuntime . dispose ( )
102105 cwd . mockRestore ( )
103- get . mockRestore ( )
104- wait . mockRestore ( )
106+ restore ( )
105107 delete process . env . OPENCODE_PLUGIN_META_FILE
106108 }
107109} )
0 commit comments