8c94e9005f
Co-authored-by: Brendan Allan <14191578+Brendonovich@users.noreply.github.com> Co-authored-by: Kit Langton <kit.langton@gmail.com> Co-authored-by: opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com> Co-authored-by: Affan Ali <93028901+affanali2k3@users.noreply.github.com> Co-authored-by: affanali2k3 <affanalikhanxx@gmail.com> Co-authored-by: Frank <frank@anoma.ly> Co-authored-by: opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com> Co-authored-by: 𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴 <little-frank@opencord.local> Co-authored-by: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Co-authored-by: Jay V <air@live.ca> Co-authored-by: Dax Raad <d@ironbay.co> Co-authored-by: Aarav Sareen <96787824+arvsrn@users.noreply.github.com> Co-authored-by: OpeOginni <107570612+OpeOginni@users.noreply.github.com> Co-authored-by: Luke Parker <10430890+Hona@users.noreply.github.com> Co-authored-by: Ben Guthrie <benjee.012@gmail.com> Co-authored-by: Dax <mail@thdxr.com> Co-authored-by: Filip <34747899+neriousy@users.noreply.github.com> Co-authored-by: Max Anderson <max.a.anderson95@gmail.com> Co-authored-by: Brendan Allan <git@brendonovich.dev> Co-authored-by: Jack <jack@anoma.ly> Co-authored-by: Shoubhit Dash <shoubhit2005@gmail.com> Co-authored-by: Dustin Deus <deusdustin@gmail.com> Co-authored-by: starptech <starptech@starptechs-MBP.fritz.box> Co-authored-by: Aiden Cline <aidenpcline@gmail.com> Co-authored-by: usrnk1 <7547651+usrnk1@users.noreply.github.com> Co-authored-by: Jay <53023+jayair@users.noreply.github.com> Co-authored-by: runvip <164729189+runvip@users.noreply.github.com> Co-authored-by: opencode <opencode@sst.dev> Co-authored-by: Julian Coy <julian@ex-machina.co> Co-authored-by: Vladimir Glafirov <vglafirov@gitlab.com>
79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
import { expect, test } from "bun:test"
|
|
import { setTimeout as sleep } from "node:timers/promises"
|
|
import { AppNodeBuilder } from "@opencode-ai/core/effect/app-node-builder"
|
|
import { Effect, Layer } from "effect"
|
|
import { FSUtil } from "@opencode-ai/core/fs-util"
|
|
import { McpAuth } from "../../src/mcp/auth"
|
|
|
|
function authFile() {
|
|
let raw = ""
|
|
let activeWrites = 0
|
|
let sawOverlap = false
|
|
|
|
const fsLayer = Layer.effect(
|
|
FSUtil.Service,
|
|
Effect.gen(function* () {
|
|
const fs = yield* FSUtil.Service
|
|
|
|
return FSUtil.Service.of({
|
|
...fs,
|
|
readJson: (file) =>
|
|
file.endsWith("mcp-auth.json")
|
|
? Effect.try({
|
|
try: () => {
|
|
if (!raw) throw new Error("mcp-auth.json missing")
|
|
return JSON.parse(raw)
|
|
},
|
|
catch: (cause) => new FSUtil.FileSystemError({ method: "readJson", cause }),
|
|
})
|
|
: fs.readJson(file),
|
|
writeJson: (file, value, mode) =>
|
|
file.endsWith("mcp-auth.json")
|
|
? Effect.promise(async () => {
|
|
activeWrites++
|
|
sawOverlap = sawOverlap || activeWrites > 1
|
|
raw = ""
|
|
await sleep(10)
|
|
const next = JSON.stringify(value, null, 2)
|
|
raw = sawOverlap ? `${next}\n}` : next
|
|
activeWrites--
|
|
})
|
|
: fs.writeJson(file, value, mode),
|
|
})
|
|
}),
|
|
).pipe(Layer.provide(AppNodeBuilder.build(FSUtil.node)))
|
|
|
|
return { fsLayer, raw: () => raw }
|
|
}
|
|
|
|
function authService(fsLayer: Layer.Layer<FSUtil.Service>) {
|
|
return McpAuth.Service.use((auth) => Effect.succeed(auth)).pipe(
|
|
Effect.provide(AppNodeBuilder.build(McpAuth.node, [[FSUtil.node, fsLayer]])),
|
|
)
|
|
}
|
|
|
|
test("serializes concurrent auth file updates across service instances", async () => {
|
|
const file = authFile()
|
|
|
|
await Effect.runPromise(
|
|
Effect.gen(function* () {
|
|
const first = yield* authService(file.fsLayer)
|
|
const second = yield* authService(file.fsLayer)
|
|
|
|
yield* Effect.all(
|
|
[
|
|
first.updateTokens("posthog", { accessToken: "access-token" }, "https://mcp.posthog.com/mcp"),
|
|
second.updateClientInfo("posthog", { clientId: "client-id" }, "https://mcp.posthog.com/mcp"),
|
|
],
|
|
{ concurrency: "unbounded" },
|
|
)
|
|
|
|
const entry = yield* first.get("posthog")
|
|
expect(entry?.tokens?.accessToken).toBe("access-token")
|
|
expect(entry?.clientInfo?.clientId).toBe("client-id")
|
|
expect(entry?.serverUrl).toBe("https://mcp.posthog.com/mcp")
|
|
expect(() => JSON.parse(file.raw())).not.toThrow()
|
|
}),
|
|
)
|
|
})
|