mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-09 18:29:39 +00:00
106 lines
3.4 KiB
TypeScript
106 lines
3.4 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import type { PermissionRequest, QuestionRequest, Session } from "@opencode-ai/sdk/v2/client"
|
|
import { sessionPermissionRequest, sessionQuestionRequest } from "./session-request-tree"
|
|
|
|
const session = (input: { id: string; parentID?: string }) =>
|
|
({
|
|
id: input.id,
|
|
parentID: input.parentID,
|
|
}) as Session
|
|
|
|
const permission = (id: string, sessionID: string) =>
|
|
({
|
|
id,
|
|
sessionID,
|
|
}) as PermissionRequest
|
|
|
|
const question = (id: string, sessionID: string) =>
|
|
({
|
|
id,
|
|
sessionID,
|
|
questions: [],
|
|
}) as QuestionRequest
|
|
|
|
describe("sessionPermissionRequest", () => {
|
|
test("prefers the current session permission", () => {
|
|
const sessions = [session({ id: "root" }), session({ id: "child", parentID: "root" })]
|
|
const permissions = {
|
|
root: [permission("perm-root", "root")],
|
|
child: [permission("perm-child", "child")],
|
|
}
|
|
|
|
expect(sessionPermissionRequest(sessions, permissions, "root")?.id).toBe("perm-root")
|
|
})
|
|
|
|
test("returns a nested child permission", () => {
|
|
const sessions = [
|
|
session({ id: "root" }),
|
|
session({ id: "child", parentID: "root" }),
|
|
session({ id: "grand", parentID: "child" }),
|
|
session({ id: "other" }),
|
|
]
|
|
const permissions = {
|
|
grand: [permission("perm-grand", "grand")],
|
|
other: [permission("perm-other", "other")],
|
|
}
|
|
|
|
expect(sessionPermissionRequest(sessions, permissions, "root")?.id).toBe("perm-grand")
|
|
})
|
|
|
|
test("returns undefined without a matching tree permission", () => {
|
|
const sessions = [session({ id: "root" }), session({ id: "child", parentID: "root" })]
|
|
const permissions = {
|
|
other: [permission("perm-other", "other")],
|
|
}
|
|
|
|
expect(sessionPermissionRequest(sessions, permissions, "root")).toBeUndefined()
|
|
})
|
|
|
|
test("skips filtered permissions in the current tree", () => {
|
|
const sessions = [session({ id: "root" }), session({ id: "child", parentID: "root" })]
|
|
const permissions = {
|
|
root: [permission("perm-root", "root")],
|
|
child: [permission("perm-child", "child")],
|
|
}
|
|
|
|
expect(sessionPermissionRequest(sessions, permissions, "root", (item) => item.id !== "perm-root"))?.toMatchObject({
|
|
id: "perm-child",
|
|
})
|
|
})
|
|
|
|
test("returns undefined when all tree permissions are filtered out", () => {
|
|
const sessions = [session({ id: "root" }), session({ id: "child", parentID: "root" })]
|
|
const permissions = {
|
|
root: [permission("perm-root", "root")],
|
|
child: [permission("perm-child", "child")],
|
|
}
|
|
|
|
expect(sessionPermissionRequest(sessions, permissions, "root", () => false)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe("sessionQuestionRequest", () => {
|
|
test("prefers the current session question", () => {
|
|
const sessions = [session({ id: "root" }), session({ id: "child", parentID: "root" })]
|
|
const questions = {
|
|
root: [question("q-root", "root")],
|
|
child: [question("q-child", "child")],
|
|
}
|
|
|
|
expect(sessionQuestionRequest(sessions, questions, "root")?.id).toBe("q-root")
|
|
})
|
|
|
|
test("returns a nested child question", () => {
|
|
const sessions = [
|
|
session({ id: "root" }),
|
|
session({ id: "child", parentID: "root" }),
|
|
session({ id: "grand", parentID: "child" }),
|
|
]
|
|
const questions = {
|
|
grand: [question("q-grand", "grand")],
|
|
}
|
|
|
|
expect(sessionQuestionRequest(sessions, questions, "root")?.id).toBe("q-grand")
|
|
})
|
|
})
|