generated from nhcarrigan/template
feat: start setting up CI
This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
lint-and-test:
|
||||
name: Lint & Test
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install
|
||||
|
||||
- name: Run ESLint
|
||||
run: pnpm lint
|
||||
|
||||
- name: Run Prettier check
|
||||
run: pnpm format:check
|
||||
|
||||
- name: Run Svelte Check
|
||||
run: pnpm check
|
||||
|
||||
- name: Run frontend tests
|
||||
run: pnpm test
|
||||
|
||||
- name: Setup Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
components: clippy
|
||||
|
||||
- name: Cache Rust dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/bin/
|
||||
~/.cargo/registry/index/
|
||||
~/.cargo/registry/cache/
|
||||
~/.cargo/git/db/
|
||||
src-tauri/target/
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||
|
||||
- name: Run Clippy
|
||||
working-directory: src-tauri
|
||||
run: cargo clippy --all-targets --all-features -- -D warnings
|
||||
|
||||
- name: Run Rust tests
|
||||
working-directory: src-tauri
|
||||
run: cargo test
|
||||
|
||||
build-linux:
|
||||
name: Build Linux
|
||||
runs-on: ubuntu-latest
|
||||
needs: lint-and-test
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Linux dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
libwebkit2gtk-4.1-dev \
|
||||
libappindicator3-dev \
|
||||
librsvg2-dev \
|
||||
patchelf \
|
||||
libgtk-3-dev \
|
||||
libayatana-appindicator3-dev
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install
|
||||
|
||||
- name: Setup Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
|
||||
- name: Cache Rust dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/bin/
|
||||
~/.cargo/registry/index/
|
||||
~/.cargo/registry/cache/
|
||||
~/.cargo/git/db/
|
||||
src-tauri/target/
|
||||
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
|
||||
|
||||
- name: Build Linux
|
||||
run: pnpm build:linux
|
||||
|
||||
build-windows:
|
||||
name: Build Windows (cross-compile)
|
||||
runs-on: ubuntu-latest
|
||||
needs: lint-and-test
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Install Linux dependencies for cross-compilation
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y \
|
||||
libwebkit2gtk-4.1-dev \
|
||||
libappindicator3-dev \
|
||||
librsvg2-dev \
|
||||
patchelf \
|
||||
libgtk-3-dev \
|
||||
libayatana-appindicator3-dev \
|
||||
clang \
|
||||
lld \
|
||||
llvm
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
with:
|
||||
version: 9
|
||||
|
||||
- name: Setup Node.js
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: pnpm
|
||||
|
||||
- name: Install frontend dependencies
|
||||
run: pnpm install
|
||||
|
||||
- name: Setup Rust
|
||||
uses: dtolnay/rust-toolchain@stable
|
||||
with:
|
||||
targets: x86_64-pc-windows-msvc
|
||||
|
||||
- name: Install cargo-xwin
|
||||
run: cargo install cargo-xwin
|
||||
|
||||
- name: Cache Rust dependencies
|
||||
uses: actions/cache@v4
|
||||
with:
|
||||
path: |
|
||||
~/.cargo/bin/
|
||||
~/.cargo/registry/index/
|
||||
~/.cargo/registry/cache/
|
||||
~/.cargo/git/db/
|
||||
src-tauri/target/
|
||||
key: ${{ runner.os }}-cargo-windows-${{ hashFiles('**/Cargo.lock') }}
|
||||
|
||||
- name: Build Windows
|
||||
run: pnpm build:windows
|
||||
@@ -0,0 +1,6 @@
|
||||
build/
|
||||
.svelte-kit/
|
||||
dist/
|
||||
src-tauri/target/
|
||||
node_modules/
|
||||
pnpm-lock.yaml
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"semi": true,
|
||||
"singleQuote": false,
|
||||
"tabWidth": 2,
|
||||
"trailingComma": "es5",
|
||||
"printWidth": 100,
|
||||
"plugins": ["prettier-plugin-svelte"],
|
||||
"overrides": [
|
||||
{
|
||||
"files": "*.svelte",
|
||||
"options": {
|
||||
"parser": "svelte"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import js from "@eslint/js";
|
||||
import tseslint from "typescript-eslint";
|
||||
import svelte from "eslint-plugin-svelte";
|
||||
import prettier from "eslint-config-prettier";
|
||||
import globals from "globals";
|
||||
|
||||
export default tseslint.config(
|
||||
js.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
...svelte.configs["flat/recommended"],
|
||||
prettier,
|
||||
...svelte.configs["flat/prettier"],
|
||||
{
|
||||
languageOptions: {
|
||||
globals: {
|
||||
...globals.browser,
|
||||
...globals.node,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
files: ["**/*.svelte", "**/*.svelte.ts", "**/*.svelte.js"],
|
||||
languageOptions: {
|
||||
parserOptions: {
|
||||
parser: tseslint.parser,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ignores: [
|
||||
"build/",
|
||||
".svelte-kit/",
|
||||
"dist/",
|
||||
"src-tauri/target/",
|
||||
"node_modules/",
|
||||
],
|
||||
}
|
||||
);
|
||||
+21
-2
@@ -12,7 +12,14 @@
|
||||
"tauri": "tauri",
|
||||
"build:linux": "tauri build",
|
||||
"build:windows": "tauri build --runner cargo-xwin --target x86_64-pc-windows-msvc",
|
||||
"build:all": "pnpm build:linux && pnpm build:windows"
|
||||
"build:all": "pnpm build:linux && pnpm build:windows",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"test:coverage": "vitest run --coverage",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint . --fix",
|
||||
"format": "prettier --write .",
|
||||
"format:check": "prettier --check ."
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
@@ -22,15 +29,27 @@
|
||||
"@tauri-apps/plugin-shell": "^2.3.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.39.2",
|
||||
"@sveltejs/adapter-static": "^3.0.6",
|
||||
"@sveltejs/kit": "^2.9.0",
|
||||
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
||||
"@tailwindcss/vite": "^4.1.18",
|
||||
"@tauri-apps/cli": "^2",
|
||||
"@testing-library/jest-dom": "^6.9.1",
|
||||
"@testing-library/svelte": "^5.3.1",
|
||||
"eslint": "^9.39.2",
|
||||
"eslint-config-prettier": "^10.1.8",
|
||||
"eslint-plugin-svelte": "^3.14.0",
|
||||
"globals": "^17.0.0",
|
||||
"jsdom": "^27.4.0",
|
||||
"prettier": "^3.8.0",
|
||||
"prettier-plugin-svelte": "^3.4.1",
|
||||
"svelte": "^5.0.0",
|
||||
"svelte-check": "^4.0.0",
|
||||
"tailwindcss": "^4.1.18",
|
||||
"typescript": "~5.6.2",
|
||||
"vite": "^6.0.3"
|
||||
"typescript-eslint": "^8.53.0",
|
||||
"vite": "^6.0.3",
|
||||
"vitest": "^4.0.17"
|
||||
}
|
||||
}
|
||||
|
||||
Generated
+1798
File diff suppressed because it is too large
Load Diff
+132
-14
@@ -1,8 +1,9 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum CharacterState {
|
||||
#[default]
|
||||
Idle,
|
||||
Thinking,
|
||||
Typing,
|
||||
@@ -14,27 +15,17 @@ pub enum CharacterState {
|
||||
Error,
|
||||
}
|
||||
|
||||
impl Default for CharacterState {
|
||||
fn default() -> Self {
|
||||
CharacterState::Idle
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ConnectionStatus {
|
||||
#[default]
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl Default for ConnectionStatus {
|
||||
fn default() -> Self {
|
||||
ConnectionStatus::Disconnected
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct TerminalLine {
|
||||
pub id: String,
|
||||
@@ -46,6 +37,7 @@ pub struct TerminalLine {
|
||||
pub tool_name: Option<String>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PermissionRequest {
|
||||
pub id: String,
|
||||
@@ -186,3 +178,129 @@ pub struct PermissionPromptEvent {
|
||||
pub tool_input: serde_json::Value,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_character_state_default() {
|
||||
let state = CharacterState::default();
|
||||
assert_eq!(state, CharacterState::Idle);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_connection_status_default() {
|
||||
let status = ConnectionStatus::default();
|
||||
matches!(status, ConnectionStatus::Disconnected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_character_state_serialization() {
|
||||
let state = CharacterState::Thinking;
|
||||
let serialized = serde_json::to_string(&state).unwrap();
|
||||
assert_eq!(serialized, "\"thinking\"");
|
||||
|
||||
let deserialized: CharacterState = serde_json::from_str(&serialized).unwrap();
|
||||
assert_eq!(deserialized, CharacterState::Thinking);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_character_states_serialize() {
|
||||
let states = vec![
|
||||
(CharacterState::Idle, "\"idle\""),
|
||||
(CharacterState::Thinking, "\"thinking\""),
|
||||
(CharacterState::Typing, "\"typing\""),
|
||||
(CharacterState::Searching, "\"searching\""),
|
||||
(CharacterState::Coding, "\"coding\""),
|
||||
(CharacterState::Mcp, "\"mcp\""),
|
||||
(CharacterState::Permission, "\"permission\""),
|
||||
(CharacterState::Success, "\"success\""),
|
||||
(CharacterState::Error, "\"error\""),
|
||||
];
|
||||
|
||||
for (state, expected) in states {
|
||||
let serialized = serde_json::to_string(&state).unwrap();
|
||||
assert_eq!(serialized, expected, "Failed for state: {:?}", state);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_terminal_line_serialization() {
|
||||
let line = TerminalLine {
|
||||
id: "test-123".to_string(),
|
||||
line_type: "assistant".to_string(),
|
||||
content: "Hello, world!".to_string(),
|
||||
timestamp: "2024-01-01T00:00:00Z".to_string(),
|
||||
tool_name: None,
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&line).unwrap();
|
||||
assert!(serialized.contains("\"type\":\"assistant\""));
|
||||
assert!(serialized.contains("\"content\":\"Hello, world!\""));
|
||||
assert!(!serialized.contains("tool_name"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_terminal_line_with_tool_name() {
|
||||
let line = TerminalLine {
|
||||
id: "test-456".to_string(),
|
||||
line_type: "tool".to_string(),
|
||||
content: "Reading file...".to_string(),
|
||||
timestamp: "2024-01-01T00:00:00Z".to_string(),
|
||||
tool_name: Some("Read".to_string()),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&line).unwrap();
|
||||
assert!(serialized.contains("\"tool_name\":\"Read\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_text() {
|
||||
let block = ContentBlock::Text {
|
||||
text: "Hello!".to_string(),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&block).unwrap();
|
||||
assert!(serialized.contains("\"type\":\"text\""));
|
||||
assert!(serialized.contains("\"text\":\"Hello!\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_content_block_tool_use() {
|
||||
let block = ContentBlock::ToolUse {
|
||||
id: "tool-123".to_string(),
|
||||
name: "Read".to_string(),
|
||||
input: serde_json::json!({"file_path": "/test.txt"}),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&block).unwrap();
|
||||
assert!(serialized.contains("\"type\":\"tool_use\""));
|
||||
assert!(serialized.contains("\"name\":\"Read\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_state_change_event() {
|
||||
let event = StateChangeEvent {
|
||||
state: CharacterState::Coding,
|
||||
tool_name: Some("Edit".to_string()),
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&event).unwrap();
|
||||
assert!(serialized.contains("\"state\":\"coding\""));
|
||||
assert!(serialized.contains("\"tool_name\":\"Edit\""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_output_event() {
|
||||
let event = OutputEvent {
|
||||
line_type: "assistant".to_string(),
|
||||
content: "Test output".to_string(),
|
||||
tool_name: None,
|
||||
};
|
||||
|
||||
let serialized = serde_json::to_string(&event).unwrap();
|
||||
assert!(serialized.contains("\"line_type\":\"assistant\""));
|
||||
assert!(serialized.contains("\"content\":\"Test output\""));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -472,3 +472,127 @@ pub type SharedBridge = Arc<Mutex<WslBridge>>;
|
||||
pub fn create_shared_bridge() -> SharedBridge {
|
||||
Arc::new(Mutex::new(WslBridge::new()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_get_tool_state_search_tools() {
|
||||
assert!(matches!(get_tool_state("Read"), CharacterState::Searching));
|
||||
assert!(matches!(get_tool_state("Glob"), CharacterState::Searching));
|
||||
assert!(matches!(get_tool_state("Grep"), CharacterState::Searching));
|
||||
assert!(matches!(get_tool_state("WebSearch"), CharacterState::Searching));
|
||||
assert!(matches!(get_tool_state("WebFetch"), CharacterState::Searching));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tool_state_coding_tools() {
|
||||
assert!(matches!(get_tool_state("Edit"), CharacterState::Coding));
|
||||
assert!(matches!(get_tool_state("Write"), CharacterState::Coding));
|
||||
assert!(matches!(get_tool_state("NotebookEdit"), CharacterState::Coding));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tool_state_mcp_tools() {
|
||||
assert!(matches!(get_tool_state("mcp__github__create_issue"), CharacterState::Mcp));
|
||||
assert!(matches!(get_tool_state("mcp__notion__search"), CharacterState::Mcp));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tool_state_task() {
|
||||
assert!(matches!(get_tool_state("Task"), CharacterState::Thinking));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tool_state_unknown() {
|
||||
assert!(matches!(get_tool_state("SomeUnknownTool"), CharacterState::Typing));
|
||||
assert!(matches!(get_tool_state("Bash"), CharacterState::Typing));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_read() {
|
||||
let input = serde_json::json!({"file_path": "/home/test/file.txt"});
|
||||
let desc = format_tool_description("Read", &input);
|
||||
assert_eq!(desc, "Reading file: /home/test/file.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_read_no_path() {
|
||||
let input = serde_json::json!({});
|
||||
let desc = format_tool_description("Read", &input);
|
||||
assert_eq!(desc, "Reading file...");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_glob() {
|
||||
let input = serde_json::json!({"pattern": "**/*.rs"});
|
||||
let desc = format_tool_description("Glob", &input);
|
||||
assert_eq!(desc, "Searching for files: **/*.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_grep() {
|
||||
let input = serde_json::json!({"pattern": "TODO"});
|
||||
let desc = format_tool_description("Grep", &input);
|
||||
assert_eq!(desc, "Searching for: TODO");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_edit() {
|
||||
let input = serde_json::json!({"file_path": "/home/test/main.rs"});
|
||||
let desc = format_tool_description("Edit", &input);
|
||||
assert_eq!(desc, "Editing: /home/test/main.rs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_write() {
|
||||
let input = serde_json::json!({"file_path": "/home/test/new.txt"});
|
||||
let desc = format_tool_description("Write", &input);
|
||||
assert_eq!(desc, "Editing: /home/test/new.txt");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_bash_short() {
|
||||
let input = serde_json::json!({"command": "ls -la"});
|
||||
let desc = format_tool_description("Bash", &input);
|
||||
assert_eq!(desc, "Running: ls -la");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_bash_long() {
|
||||
let long_cmd = "a".repeat(100);
|
||||
let input = serde_json::json!({"command": long_cmd});
|
||||
let desc = format_tool_description("Bash", &input);
|
||||
assert!(desc.starts_with("Running: "));
|
||||
assert!(desc.ends_with("..."));
|
||||
assert!(desc.len() < 70);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_tool_description_unknown() {
|
||||
let input = serde_json::json!({"some": "data"});
|
||||
let desc = format_tool_description("CustomTool", &input);
|
||||
assert_eq!(desc, "Using tool: CustomTool");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wsl_bridge_new() {
|
||||
let bridge = WslBridge::new();
|
||||
assert!(!bridge.is_running());
|
||||
assert_eq!(bridge.get_working_directory(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_wsl_bridge_default() {
|
||||
let bridge = WslBridge::default();
|
||||
assert!(!bridge.is_running());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_shared_bridge() {
|
||||
let shared = create_shared_bridge();
|
||||
let bridge = shared.lock();
|
||||
assert!(!bridge.is_running());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { mapMessageToState, extractTextFromMessage, extractToolInfo } from "./stateMapper";
|
||||
import type { ClaudeStreamMessage, AssistantMessage } from "$lib/types/messages";
|
||||
|
||||
describe("stateMapper", () => {
|
||||
describe("mapMessageToState", () => {
|
||||
it("returns idle for system init message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "system",
|
||||
subtype: "init",
|
||||
session_id: "test-session",
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("idle");
|
||||
});
|
||||
|
||||
it("returns null for non-init system messages", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "system",
|
||||
subtype: "other",
|
||||
session_id: "test-session",
|
||||
};
|
||||
expect(mapMessageToState(message)).toBeNull();
|
||||
});
|
||||
|
||||
it("returns searching for Read tool", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "Read",
|
||||
input: { file_path: "/test/file.txt" },
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("searching");
|
||||
});
|
||||
|
||||
it("returns coding for Edit tool", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "Edit",
|
||||
input: { file_path: "/test/file.txt", old_string: "foo", new_string: "bar" },
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("coding");
|
||||
});
|
||||
|
||||
it("returns mcp for mcp__ prefixed tools", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "mcp__github__list_repos",
|
||||
input: {},
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("mcp");
|
||||
});
|
||||
|
||||
it("returns thinking for Task tool", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "Task",
|
||||
input: { prompt: "test task" },
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("thinking");
|
||||
});
|
||||
|
||||
it("returns typing for text content", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [{ type: "text", text: "Hello, Naomi!" }],
|
||||
model: "claude-3",
|
||||
stop_reason: "end_turn",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("typing");
|
||||
});
|
||||
|
||||
it("returns success for result success message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "result",
|
||||
subtype: "success",
|
||||
result: "Task completed",
|
||||
session_id: "test-session",
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("success");
|
||||
});
|
||||
|
||||
it("returns error for result error message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "result",
|
||||
subtype: "error_max_turns",
|
||||
error: "Max turns exceeded",
|
||||
session_id: "test-session",
|
||||
};
|
||||
expect(mapMessageToState(message)).toBe("error");
|
||||
});
|
||||
|
||||
it("returns null for user messages", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "user",
|
||||
message: { role: "user", content: "Hello" },
|
||||
};
|
||||
expect(mapMessageToState(message)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractTextFromMessage", () => {
|
||||
it("extracts text from assistant message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{ type: "text", text: "Hello!" },
|
||||
{ type: "text", text: "How are you?" },
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "end_turn",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(extractTextFromMessage(message)).toBe("Hello!\nHow are you?");
|
||||
});
|
||||
|
||||
it("returns null for assistant message without text", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "Read",
|
||||
input: { file_path: "/test/file.txt" },
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
expect(extractTextFromMessage(message)).toBeNull();
|
||||
});
|
||||
|
||||
it("extracts text from stream_event delta", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "stream_event",
|
||||
event: {
|
||||
type: "content_block_delta",
|
||||
index: 0,
|
||||
delta: { type: "text_delta", text: "streaming text" },
|
||||
},
|
||||
};
|
||||
expect(extractTextFromMessage(message)).toBe("streaming text");
|
||||
});
|
||||
|
||||
it("extracts result from result message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "result",
|
||||
subtype: "success",
|
||||
result: "Completed successfully",
|
||||
session_id: "test-session",
|
||||
};
|
||||
expect(extractTextFromMessage(message)).toBe("Completed successfully");
|
||||
});
|
||||
});
|
||||
|
||||
describe("extractToolInfo", () => {
|
||||
it("extracts tool info from assistant message", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "assistant",
|
||||
message: {
|
||||
id: "test",
|
||||
type: "message",
|
||||
role: "assistant",
|
||||
content: [
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-1",
|
||||
name: "Read",
|
||||
input: { file_path: "/test/file.txt" },
|
||||
},
|
||||
{
|
||||
type: "tool_use",
|
||||
id: "tool-2",
|
||||
name: "Edit",
|
||||
input: { file_path: "/test/file.txt", old_string: "a", new_string: "b" },
|
||||
},
|
||||
],
|
||||
model: "claude-3",
|
||||
stop_reason: "tool_use",
|
||||
usage: { input_tokens: 100, output_tokens: 50 },
|
||||
},
|
||||
};
|
||||
const tools = extractToolInfo(message);
|
||||
expect(tools).toHaveLength(2);
|
||||
expect(tools[0]).toEqual({
|
||||
name: "Read",
|
||||
input: { file_path: "/test/file.txt" },
|
||||
});
|
||||
expect(tools[1]).toEqual({
|
||||
name: "Edit",
|
||||
input: { file_path: "/test/file.txt", old_string: "a", new_string: "b" },
|
||||
});
|
||||
});
|
||||
|
||||
it("returns empty array for non-assistant messages", () => {
|
||||
const message: ClaudeStreamMessage = {
|
||||
type: "user",
|
||||
message: { role: "user", content: "Hello" },
|
||||
};
|
||||
expect(extractToolInfo(message)).toEqual([]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
import { sveltekit } from "@sveltejs/kit/vite";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [sveltekit()],
|
||||
test: {
|
||||
include: ["src/**/*.{test,spec}.{js,ts}"],
|
||||
environment: "jsdom",
|
||||
setupFiles: ["./vitest.setup.ts"],
|
||||
globals: true,
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
import "@testing-library/jest-dom/vitest";
|
||||
Reference in New Issue
Block a user