124 lines
4.1 KiB
TypeScript
124 lines
4.1 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { createRoot, createSignal } from "solid-js"
|
|
import { createStore } from "solid-js/store"
|
|
import { createServerProjects, migrateCanonicalLocalServerState, resolveServerList, ServerConnection } from "./server"
|
|
import { ServerScope } from "@/utils/server-scope"
|
|
|
|
describe("resolveServerList", () => {
|
|
test("lets startup auth_token credentials override a persisted same-url server", () => {
|
|
const list = resolveServerList({
|
|
stored: [{ url: "https://server.example.test" }],
|
|
props: [
|
|
{
|
|
type: "http",
|
|
authToken: true,
|
|
http: {
|
|
url: "https://server.example.test",
|
|
username: "opencode",
|
|
password: "secret",
|
|
},
|
|
},
|
|
],
|
|
})
|
|
|
|
expect(list).toHaveLength(1)
|
|
expect(list[0]?.type).toBe("http")
|
|
expect(list[0]?.http).toEqual({
|
|
url: "https://server.example.test",
|
|
username: "opencode",
|
|
password: "secret",
|
|
})
|
|
expect(list[0]?.type === "http" ? list[0].authToken : false).toBe(true)
|
|
expect(ServerConnection.key(list[0]!) as string).toBe("https://server.example.test")
|
|
})
|
|
|
|
test("keeps persisted credentials when startup has no auth_token", () => {
|
|
const list = resolveServerList({
|
|
stored: [
|
|
{
|
|
url: "https://server.example.test",
|
|
username: "opencode",
|
|
password: "saved",
|
|
},
|
|
],
|
|
props: [{ type: "http", http: { url: "https://server.example.test" } }],
|
|
})
|
|
|
|
expect(list).toHaveLength(1)
|
|
expect(list[0]?.type).toBe("http")
|
|
expect(list[0]?.http).toEqual({
|
|
url: "https://server.example.test",
|
|
username: "opencode",
|
|
password: "saved",
|
|
})
|
|
expect(list[0]?.type === "http" ? list[0].authToken : true).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe("createServerProjects", () => {
|
|
test("keeps active and explicit server buckets in one reactive store", () => {
|
|
createRoot((dispose) => {
|
|
const [scope] = createSignal(ServerScope.local)
|
|
const [store, setStore] = createStore({ projects: {}, lastProject: {} })
|
|
const active = createServerProjects({ scope, store, setStore })
|
|
const remote = createServerProjects({ scope: () => "https://debian.example" as ServerScope, store, setStore })
|
|
|
|
remote.open("/repo")
|
|
expect(remote.list()).toEqual([{ worktree: "/repo", expanded: true }])
|
|
expect(active.list()).toEqual([])
|
|
|
|
const adopted = createServerProjects({ scope: () => "https://debian.example" as ServerScope, store, setStore })
|
|
expect(adopted.list()).toEqual([{ worktree: "/repo", expanded: true }])
|
|
|
|
adopted.close("/repo")
|
|
expect(remote.list()).toEqual([])
|
|
dispose()
|
|
})
|
|
})
|
|
})
|
|
|
|
describe("migrateCanonicalLocalServerState", () => {
|
|
test("moves an existing canonical web bucket into local scope", () => {
|
|
expect(
|
|
migrateCanonicalLocalServerState(
|
|
{
|
|
list: [],
|
|
projects: { "https://opencode.example.com": [{ worktree: "/remote", expanded: true }] },
|
|
lastProject: { "https://opencode.example.com": "/remote" },
|
|
},
|
|
ServerConnection.Key.make("https://opencode.example.com"),
|
|
),
|
|
).toEqual({
|
|
list: [],
|
|
projects: { local: [{ worktree: "/remote", expanded: true }] },
|
|
lastProject: { local: "/remote" },
|
|
})
|
|
})
|
|
|
|
test("preserves existing local state while merging a canonical web bucket", () => {
|
|
expect(
|
|
migrateCanonicalLocalServerState(
|
|
{
|
|
projects: {
|
|
local: [{ worktree: "/local", expanded: false }],
|
|
"https://opencode.example.com": [
|
|
{ worktree: "/local", expanded: true },
|
|
{ worktree: "/remote", expanded: true },
|
|
],
|
|
},
|
|
lastProject: { local: "/local", "https://opencode.example.com": "/remote" },
|
|
},
|
|
ServerConnection.Key.make("https://opencode.example.com"),
|
|
),
|
|
).toEqual({
|
|
projects: {
|
|
local: [
|
|
{ worktree: "/local", expanded: false },
|
|
{ worktree: "/remote", expanded: true },
|
|
],
|
|
},
|
|
lastProject: { local: "/local" },
|
|
})
|
|
})
|
|
})
|