Files
schpet__linear-cli/test/utils/graphql.test.ts
Peter Schilling 908bae467f fix: error when --workspace flag specifies unknown workspace
Previously, --workspace would silently fall back to other credential
sources when the specified workspace wasn't found. Now it errors with
a helpful message suggesting `linear auth login` or `linear auth list`.

Also errors when both LINEAR_API_KEY env var and --workspace are set,
since these are conflicting ways to specify credentials.

Added error handling guidelines to CLAUDE.md to prevent silent failures.
2026-01-27 13:02:46 -08:00

56 lines
1.5 KiB
TypeScript

import { assertEquals, assertStringIncludes, assertThrows } from "@std/assert"
import { setCliWorkspace } from "../../src/config.ts"
import { getResolvedApiKey } from "../../src/utils/graphql.ts"
Deno.test("getResolvedApiKey - errors when --workspace not found in credentials", () => {
// Setup - use a workspace name that definitely doesn't exist
Deno.env.delete("LINEAR_API_KEY")
setCliWorkspace("nonexistent-workspace-xyz-123")
try {
const error = assertThrows(
() => getResolvedApiKey(),
Error,
)
assertStringIncludes(
error.message,
'Workspace "nonexistent-workspace-xyz-123" not found in credentials',
)
} finally {
// Cleanup
setCliWorkspace(undefined)
}
})
Deno.test("getResolvedApiKey - errors when LINEAR_API_KEY and --workspace both set", () => {
// Setup
Deno.env.set("LINEAR_API_KEY", "test-api-key")
setCliWorkspace("test-workspace")
try {
assertThrows(
() => getResolvedApiKey(),
Error,
"Cannot use --workspace flag when LINEAR_API_KEY environment variable is set",
)
} finally {
// Cleanup
Deno.env.delete("LINEAR_API_KEY")
setCliWorkspace(undefined)
}
})
Deno.test("getResolvedApiKey - returns LINEAR_API_KEY when set without --workspace", () => {
// Setup
Deno.env.set("LINEAR_API_KEY", "test-api-key")
setCliWorkspace(undefined)
try {
const result = getResolvedApiKey()
assertEquals(result, "test-api-key")
} finally {
// Cleanup
Deno.env.delete("LINEAR_API_KEY")
}
})