Organize Seal modules and add guarded rule recoding
This commit is contained in:
1 parent
f788f9c6db
commit
8c7cb219a0
65 files changed
+837
-131
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client"
|
||||
import { randomBytes } from "node:crypto"
|
||||
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
||||
import { tmpdir } from "node:os"
|
||||
import { join } from "node:path"
|
||||
import { createServer, SEAL_USE_MCP_TOOL_NAME, SEAL_USE_MCP_WRITE_TOOL_NAME } from "../../server.js"
|
||||
|
||||
test("separate CLI processes share one-use tickets and MCP retains the first-phase JSON preview", async () => {
|
||||
const dir = mkdtempSync(join(tmpdir(), "seal-recode-cli-"))
|
||||
const corpId = `recode-${randomBytes(4).toString("hex")}`
|
||||
let posts = 0
|
||||
const rules = [
|
||||
{ id: "a", tenantId: "tenant", code: 0, description: "First", scope: "ALL", strictness: "MUST_FOLLOW", status: "ACTIVE" },
|
||||
{ id: "b", tenantId: "tenant", code: 2, description: "Second", scope: "ALL", strictness: "MUST_FOLLOW", status: "ACTIVE" }
|
||||
]
|
||||
const upstream = Bun.serve({
|
||||
hostname: "127.0.0.1", port: 0,
|
||||
fetch(request) {
|
||||
const path = new URL(request.url).pathname
|
||||
if (path === "/api/v1/rules" && request.method === "GET") {
|
||||
return Response.json({ data: { rules, hasPendingDeletes: false } })
|
||||
}
|
||||
if (path === "/api/v1/auth/me") {
|
||||
return Response.json({ data: { user: { id: "operator", tenantId: "tenant", name: "Operator" }, tenant: { id: "tenant", name: "Test" } } })
|
||||
}
|
||||
if (path === "/api/v1/rules/batch-recode" && request.method === "POST") {
|
||||
posts++
|
||||
rules.forEach((rule, index) => { rule.code = index })
|
||||
return Response.json({ data: { activeRules: 2, pendingDeleteRules: 0, nextCode: 2 } })
|
||||
}
|
||||
return Response.json({ error: "Unexpected request" }, { status: 404 })
|
||||
}
|
||||
})
|
||||
const enterprises = join(dir, "enterprises")
|
||||
const store = join(dir, "tickets.sqlite")
|
||||
mkdirSync(enterprises)
|
||||
const url = `http://127.0.0.1:${upstream.port}`
|
||||
writeFileSync(join(enterprises, "corp.json"), JSON.stringify({
|
||||
id: corpId, name: "CLI Test", environment: "test",
|
||||
seal: { url }, source: { type: "seal", token: "test-token", sealUrl: url }
|
||||
}))
|
||||
writeFileSync(join(dir, "config.json"), JSON.stringify({ stateful: false }))
|
||||
const env = {
|
||||
SEAL_USE_ENTERPRISES_DIR: enterprises,
|
||||
SEAL_USE_CONFIG_FILE: join(dir, "config.json"),
|
||||
SEAL_USE_TOKEN_STORE: join(dir, "tokens.json"),
|
||||
SEAL_USE_USAGE_FILE: join(dir, "usage.json"),
|
||||
SEAL_USE_RECODE_STORE: store,
|
||||
SEAL_DISABLE_BG_UPDATE: "1"
|
||||
}
|
||||
const previous = Object.fromEntries(Object.keys(env).map(key => [key, process.env[key]]))
|
||||
const server = createServer({ enableCli: true })
|
||||
const client = new Client({ name: "recode-preview-test", version: "1.0.0" })
|
||||
const run = async (args: string[]) => {
|
||||
const child = Bun.spawn([process.execPath, join(import.meta.dirname, "../../cli.ts"), "rules", "recode", "--corp", corpId, ...args], {
|
||||
env: { ...process.env, ...env }, stdout: "pipe", stderr: "pipe"
|
||||
})
|
||||
const [stdout, stderr, exitCode] = await Promise.all([
|
||||
new Response(child.stdout).text(), new Response(child.stderr).text(), child.exited
|
||||
])
|
||||
return { stdout, stderr, exitCode }
|
||||
}
|
||||
try {
|
||||
const dry = await run(["--dry-run"])
|
||||
expect(dry.exitCode).toBe(0)
|
||||
expect(JSON.parse(dry.stdout).executed).toBe(false)
|
||||
expect(existsSync(store)).toBe(false)
|
||||
const first = await run(["--yes"])
|
||||
expect(first.exitCode).toBe(10)
|
||||
const preview = JSON.parse(first.stdout)
|
||||
expect(preview).toMatchObject({ phase: "confirmation_required", executed: false, impactCount: 2, mappingAvailable: false })
|
||||
expect(first.stderr).toContain(preview.nextCommand)
|
||||
expect(posts).toBe(0)
|
||||
await Bun.sleep(510)
|
||||
const competed = await Promise.all([run(["--confirm-code", preview.confirmCode]), run(["--confirmCode", preview.confirmCode])])
|
||||
expect(competed.map(result => result.exitCode).sort()).toEqual([0, 10])
|
||||
const executed = JSON.parse(competed.find(result => result.exitCode === 0)!.stdout)
|
||||
expect(executed).toMatchObject({ executed: true, nextCode: 2 })
|
||||
expect(executed.changes).toEqual([{ beforeCode: "0000", afterCode: "0000" }, { beforeCode: "0002", afterCode: "0001" }])
|
||||
expect(posts).toBe(1)
|
||||
|
||||
for (const [key, value] of Object.entries(env)) process.env[key] = value
|
||||
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair()
|
||||
await Promise.all([server.connect(serverTransport), client.connect(clientTransport)])
|
||||
const denied = await client.callTool({ name: SEAL_USE_MCP_TOOL_NAME, arguments: { command: `rules recode --corp ${corpId}` } })
|
||||
expect(denied.isError).toBe(true)
|
||||
const result = await client.callTool({ name: SEAL_USE_MCP_WRITE_TOOL_NAME, arguments: { command: `rules recode --corp ${corpId}` } })
|
||||
expect(result.isError).toBe(true)
|
||||
const content = result.content as Array<{ type: "text"; text: string }>
|
||||
expect(JSON.parse(content[0]!.text)).toMatchObject({ executed: false, phase: "confirmation_required" })
|
||||
expect(content[1]!.text).toContain("nextCommand")
|
||||
expect(posts).toBe(1)
|
||||
} finally {
|
||||
await Promise.allSettled([client.close(), server.close()])
|
||||
upstream.stop(true)
|
||||
for (const [key, value] of Object.entries(previous)) {
|
||||
if (value === undefined) delete process.env[key]
|
||||
else process.env[key] = value
|
||||
}
|
||||
rmSync(dir, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user