Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

5.2 KB
Raw Blame
import { readFileSync } from "node:fs"
import { resolve } from "node:path"
import { describe, expect, it } from "vitest"
const root = resolve(import.meta.dirname, "../../src/desktop/src-tauri")
const read = (file: string) => readFileSync(resolve(root, file), "utf8")
type Capability = { identifier: string; local?: boolean; remote?: { urls: string[] }; windows: string[]; webviews?: string[]; platforms?: string[]; permissions: (string | { identifier: string })[] }
type Config = { app: { security: { capabilities: (string | Capability)[] } }; build: { frontendDist: string }; plugins?: { "deep-link"?: unknown } }
const production: Config = JSON.parse(read("tauri.conf.json"))
const development: Config = JSON.parse(read("tauri.dev.conf.json"))
const capabilities = (config: Config): Capability[] => config.app.security.capabilities.map(c => typeof c === "string" ? JSON.parse(read(`capabilities/${c === "desktop-capability" ? "desktop" : c}.json`)) : c)

describe("desktop native OAuth security configuration", () => {
  it.each([production, development])("keeps desktop raw IPC disabled and scopes mobile event listeners to the trusted main window", config => {
    const merged = capabilities(config)
    const isProduction = config === production
    const lifecycleIdentifier = isProduction ? "window-lifecycle-mobile" : "window-lifecycle-dev"
    const lifecycle = merged.filter(c => c.identifier === lifecycleIdentifier)
    expect(lifecycle).toHaveLength(1)
    expect(lifecycle[0]).toMatchObject({
      windows: ["main"],
      platforms: ["android", "iOS"],
      local: !isProduction,
      remote: { urls: [isProduction ? "https://alook.ai" : "http://localhost:3000"] },
      permissions: ["core:event:allow-listen", "core:event:allow-unlisten"],
    })
    expect(lifecycle[0]!.webviews ?? []).toEqual([])
    for (const capability of merged.filter(c => c.identifier !== lifecycleIdentifier)) {
      for (const permission of capability.permissions.map(p => typeof p === "string" ? p : p.identifier)) {
        expect(permission).not.toMatch(/^(?:core:default$|core:event:|deep-link:|store:)/)
      }
    }
    const oauth = merged.find(c => c.permissions.includes("native-oauth"))!
    expect(config.build.frontendDist).toBe(isProduction ? "alook-recovery://localhost/bootstrap" : "http://localhost:3000")
    expect(oauth.windows).toEqual(["main"])
    expect(oauth.platforms).toEqual(expect.arrayContaining(["linux", "macOS", "windows"]))
    expect(oauth.remote?.urls).toEqual(isProduction ? ["https://alook.ai"] : ["http://localhost:3000"])
    expect(oauth.local).toBe(true)
  })
  it("requires explicit command ACL while preserving preexisting desktop custom commands", () => {
    const build = read("build.rs")
    const native = read("permissions/native-oauth.toml")
    const existing = read("permissions/desktop-commands.toml")
    const commands = ["snapshot", "listen", "unlisten", "prepare", "open_start", "pending_exchange", "reject_candidate", "finish", "cancel"]
    for (const command of commands) {
      expect(build).toContain(`"native_oauth_${command}"`)
      expect(native).toContain(`"native_oauth_${command}"`)
    }
    expect(native).toContain(`commands.allow = [${commands.map(command => `"native_oauth_${command}"`).join(", ")}]`)
    for (const command of ["daemon_runtime_capability", "daemon_pair", "set_window_theme", "close_splashscreen", "desktop_zoom_shortcut"]) {
      expect(build).toContain(`"${command}"`)
      expect(existing).toContain(`"${command}"`)
    }
    expect(capabilities(production).find(c => c.identifier === "desktop-capability")?.permissions).toContain("desktop-commands")
  })
  it("preserves the desktop-only scheme and ordered single-instance intake", () => {
    expect(production.plugins?.["deep-link"]).toMatchObject({ desktop: { schemes: ["ai.alook.desktop"] } })
    const source = read("src/lib.rs")
    expect(source.indexOf("tauri_plugin_single_instance::init")).toBeLessThan(source.indexOf("tauri_plugin_deep_link::init"))
    expect(source.indexOf("tauri_plugin_deep_link::init")).toBeLessThan(source.indexOf("tauri_plugin_opener::init"))
    expect(read("Cargo.toml")).toContain('tauri-plugin-single-instance = { version = "2", features = ["deep-link"] }')
    const runtime = read("src/native_oauth_runtime.rs")
    expect(runtime.indexOf(".on_open_url(")).toBeLessThan(runtime.indexOf(".get_current()"))
    expect(runtime).toContain('#[cfg(target_os = "linux")]')
    expect(runtime).toContain(".register_all()")
    expect(runtime).toContain("channel.send(())")
    expect(source).toContain("PageLoadEvent::Started")
    expect(source).toContain("WindowEvent::Destroyed")
    expect(source).toContain("tauri::WindowEvent::Focused(true)")
    expect(source).not.toContain("tauri::RunEvent::Resumed")
    expect(runtime).toContain("use crate::native_command_guard::guard;")
    expect(runtime).not.toContain('label == "main"')
    const guard = read("src/native_command_guard.rs")
    expect(guard).toContain('label == "main"')
    expect(guard).toContain("url.origin().ascii_serialization() == expected")
    expect(guard).toContain("url.username().is_empty()")
    expect(guard).toContain("url.password().is_none()")
    expect(guard).toContain("accepts_only_the_main_window_and_exact_origin")
  })
})