+21

![opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



![opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>](/assets/img/avatar_default.png)



James Long
Brendan Allan
Kit Langton
opencode-agent[bot] <opencode-agent[bot]@users.noreply.github.com>
Affan Ali
affanali2k3
Frank
opencode-agent[bot] <219766164+opencode-agent[bot]@users.noreply.github.com>
𝓛𝓲𝓽𝓽𝓵𝓮 𝓕𝓻𝓪𝓷𝓴
Aiden Cline
Jay V
Dax Raad
Aarav Sareen
OpeOginni
Luke Parker
Ben Guthrie
Dax
Filip
Max Anderson
Brendan Allan
Jack
Shoubhit Dash
Dustin Deus
starptech
Aiden Cline
usrnk1
Jay
runvip
opencode
Julian Coy
Vladimir Glafirov
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>
236 lines
7.3 KiB
TypeScript
236 lines
7.3 KiB
TypeScript
import { NodeFileSystem } from "@effect/platform-node"
|
|
import { describe, expect } from "bun:test"
|
|
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
|
|
import { Effect, Layer } from "effect"
|
|
import { provideTmpdirInstance, testInstanceStoreLayer, TestInstance } from "../fixture/fixture"
|
|
import { testEffect } from "../lib/effect"
|
|
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
|
|
import { Format } from "../../src/format"
|
|
import * as Formatter from "../../src/format/formatter"
|
|
|
|
const it = testEffect(
|
|
Layer.mergeAll(LayerNode.compile(LayerNode.group([Format.node, CrossSpawnSpawner.node])), NodeFileSystem.layer),
|
|
)
|
|
|
|
describe("Format", () => {
|
|
it.instance("status() returns empty list when no formatters are configured", () =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
expect(yield* fmt.status()).toEqual([])
|
|
}),
|
|
),
|
|
)
|
|
|
|
it.instance(
|
|
"status() returns built-in formatters when formatter is true",
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
const statuses = yield* fmt.status()
|
|
const gofmt = statuses.find((item) => item.name === "gofmt")
|
|
expect(gofmt).toBeDefined()
|
|
expect(gofmt!.extensions).toContain(".go")
|
|
}),
|
|
),
|
|
{ config: { formatter: true } },
|
|
)
|
|
|
|
it.instance(
|
|
"status() keeps built-in formatters when config object is provided",
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
const statuses = yield* fmt.status()
|
|
const gofmt = statuses.find((item) => item.name === "gofmt")
|
|
const mix = statuses.find((item) => item.name === "mix")
|
|
expect(gofmt).toBeDefined()
|
|
expect(gofmt!.extensions).toContain(".go")
|
|
expect(mix).toBeDefined()
|
|
}),
|
|
),
|
|
{ config: { formatter: { gofmt: {} } } },
|
|
)
|
|
|
|
it.instance(
|
|
"status() excludes formatters marked as disabled in config",
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
const statuses = yield* fmt.status()
|
|
const gofmt = statuses.find((item) => item.name === "gofmt")
|
|
const mix = statuses.find((item) => item.name === "mix")
|
|
expect(gofmt).toBeUndefined()
|
|
expect(mix).toBeDefined()
|
|
}),
|
|
),
|
|
{ config: { formatter: { gofmt: { disabled: true } } } },
|
|
)
|
|
|
|
it.instance(
|
|
"status() excludes uv when ruff is disabled",
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
const statuses = yield* fmt.status()
|
|
expect(statuses.find((item) => item.name === "ruff")).toBeUndefined()
|
|
expect(statuses.find((item) => item.name === "uv")).toBeUndefined()
|
|
}),
|
|
),
|
|
{ config: { formatter: { ruff: { disabled: true } } } },
|
|
)
|
|
|
|
it.instance(
|
|
"status() excludes ruff when uv is disabled",
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
const statuses = yield* fmt.status()
|
|
expect(statuses.find((item) => item.name === "ruff")).toBeUndefined()
|
|
expect(statuses.find((item) => item.name === "uv")).toBeUndefined()
|
|
}),
|
|
),
|
|
{ config: { formatter: { uv: { disabled: true } } } },
|
|
)
|
|
|
|
it.instance("service initializes without error", () => Format.Service.use(() => Effect.void))
|
|
|
|
it.instance(
|
|
"file() returns false when no formatter runs",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const file = `${test.directory}/test.txt`
|
|
yield* Effect.promise(() => Bun.write(file, "x"))
|
|
|
|
const formatted = yield* Format.use.file(file)
|
|
expect(formatted).toBe(false)
|
|
}),
|
|
{ config: { formatter: false } },
|
|
)
|
|
|
|
testEffect(
|
|
Layer.mergeAll(
|
|
LayerNode.compile(LayerNode.group([Format.node, CrossSpawnSpawner.node])),
|
|
NodeFileSystem.layer,
|
|
testInstanceStoreLayer,
|
|
),
|
|
).live("status() initializes formatter state per directory", () =>
|
|
Effect.gen(function* () {
|
|
const a = yield* provideTmpdirInstance(() => Format.use.status(), {
|
|
config: { formatter: false },
|
|
})
|
|
const b = yield* provideTmpdirInstance(() => Format.use.status(), {
|
|
config: {
|
|
formatter: true,
|
|
},
|
|
})
|
|
|
|
expect(a).toEqual([])
|
|
expect(b.find((item) => item.name === "gofmt")).toBeDefined()
|
|
}),
|
|
)
|
|
|
|
it.instance(
|
|
"runs enabled checks for matching formatters in parallel",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const file = `${test.directory}/test.parallel`
|
|
yield* Effect.promise(() => Bun.write(file, "x"))
|
|
|
|
const one = {
|
|
extensions: Formatter.gofmt.extensions,
|
|
enabled: Formatter.gofmt.enabled,
|
|
}
|
|
const two = {
|
|
extensions: Formatter.mix.extensions,
|
|
enabled: Formatter.mix.enabled,
|
|
}
|
|
|
|
let active = 0
|
|
let max = 0
|
|
|
|
yield* Effect.acquireUseRelease(
|
|
Effect.sync(() => {
|
|
Formatter.gofmt.extensions = [".parallel"]
|
|
Formatter.mix.extensions = [".parallel"]
|
|
Formatter.gofmt.enabled = async () => {
|
|
active++
|
|
max = Math.max(max, active)
|
|
await Promise.resolve()
|
|
active--
|
|
return ["sh", "-c", "true"]
|
|
}
|
|
Formatter.mix.enabled = async () => {
|
|
active++
|
|
max = Math.max(max, active)
|
|
await Promise.resolve()
|
|
active--
|
|
return ["sh", "-c", "true"]
|
|
}
|
|
}),
|
|
() =>
|
|
Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
yield* fmt.init()
|
|
yield* fmt.file(file)
|
|
}),
|
|
),
|
|
() =>
|
|
Effect.sync(() => {
|
|
Formatter.gofmt.extensions = one.extensions
|
|
Formatter.gofmt.enabled = one.enabled
|
|
Formatter.mix.extensions = two.extensions
|
|
Formatter.mix.enabled = two.enabled
|
|
}),
|
|
)
|
|
|
|
expect(max).toBe(2)
|
|
}),
|
|
{ config: { formatter: { gofmt: {}, mix: {} } } },
|
|
)
|
|
|
|
it.instance(
|
|
"runs matching formatters sequentially for the same file",
|
|
() =>
|
|
Effect.gen(function* () {
|
|
const test = yield* TestInstance
|
|
const file = `${test.directory}/test.seq`
|
|
yield* Effect.promise(() => Bun.write(file, "x"))
|
|
|
|
yield* Format.Service.use((fmt) =>
|
|
Effect.gen(function* () {
|
|
yield* fmt.init()
|
|
expect(yield* fmt.file(file)).toBe(true)
|
|
}),
|
|
)
|
|
|
|
expect(yield* Effect.promise(() => Bun.file(file).text())).toBe("xAB")
|
|
}),
|
|
{
|
|
config: {
|
|
formatter: {
|
|
first: {
|
|
command: [
|
|
"node",
|
|
"-e",
|
|
"const fs = require('fs'); const file = process.argv[1]; fs.writeFileSync(file, fs.readFileSync(file, 'utf8') + 'A')",
|
|
"$FILE",
|
|
],
|
|
extensions: [".seq"],
|
|
},
|
|
second: {
|
|
command: [
|
|
"node",
|
|
"-e",
|
|
"const fs = require('fs'); const file = process.argv[1]; fs.writeFileSync(file, fs.readFileSync(file, 'utf8') + 'B')",
|
|
"$FILE",
|
|
],
|
|
extensions: [".seq"],
|
|
},
|
|
},
|
|
},
|
|
},
|
|
)
|
|
})
|