Files
wehub-resource-sync f99010fae1
CI / lint (push) Failing after 1s
CI / frontend (push) Failing after 1s
CI / scripts (push) Failing after 1s
CI / Go Test (ubuntu-latest) (push) Failing after 0s
CI / frontend-node-25 (push) Failing after 1s
CI / docs (push) Failing after 0s
CI / coverage (push) Failing after 0s
CI / e2e (push) Failing after 0s
Docker / build-and-push (push) Failing after 1s
CI / integration (push) Failing after 4m43s
CI / Go Test (windows-latest) (push) Has been cancelled
CI / Desktop Unit Tests (Windows) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux (arm64)) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Linux) (push) Has been cancelled
Desktop Artifacts / Desktop Build (Windows) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (aarch64)) (push) Has been cancelled
Desktop Artifacts (macOS) / Desktop Build (macOS (x86_64)) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:36 +08:00

169 lines
4.1 KiB
Go

package server
import (
"bytes"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/agentsview/internal/importer"
)
func TestHandleImportClaudeAI(t *testing.T) {
srv := testServer(t, 5*time.Second)
conversations := `[
{
"uuid": "api-test-001",
"name": "API Test",
"summary": "",
"created_at": "2026-03-01T10:00:00.000000Z",
"updated_at": "2026-03-01T10:05:00.000000Z",
"account": {"uuid": "acct-1"},
"chat_messages": [
{
"uuid": "m1",
"text": "Test message",
"content": [{"type":"text","text":"Test message"}],
"sender": "human",
"created_at": "2026-03-01T10:00:00.000000Z",
"updated_at": "2026-03-01T10:00:00.000000Z",
"attachments": [],
"files": []
}
]
}
]`
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", "conversations.json")
require.NoError(t, err)
_, _ = part.Write([]byte(conversations))
writer.Close()
req := httptest.NewRequest(
http.MethodPost,
"/api/v1/import/claude-ai",
&body,
)
req.Header.Set("Content-Type", writer.FormDataContentType())
rec := httptest.NewRecorder()
srv.mux.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
var stats importer.ImportStats
require.NoError(t, json.NewDecoder(rec.Body).Decode(&stats))
assert.Equal(t, 1, stats.Imported)
assert.Zero(t, stats.Updated)
}
func TestHandleImportChatGPT_RequiresZip(t *testing.T) {
srv := testServer(t, 5*time.Second)
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", "data.json")
require.NoError(t, err)
_, _ = part.Write([]byte("[]"))
writer.Close()
req := httptest.NewRequest(
http.MethodPost,
"/api/v1/import/chatgpt",
&body,
)
req.Header.Set(
"Content-Type", writer.FormDataContentType(),
)
rec := httptest.NewRecorder()
srv.mux.ServeHTTP(rec, req)
require.Equal(t, http.StatusBadRequest, rec.Code, "body: %s", rec.Body.String())
}
func TestHandleImportClaudeAI_SSE(t *testing.T) {
srv := testServer(t, 5*time.Second)
conversations := `[{
"uuid": "sse-test-001",
"name": "SSE Test",
"created_at": "2026-03-01T10:00:00.000000Z",
"updated_at": "2026-03-01T10:05:00.000000Z",
"chat_messages": [{
"uuid": "m1", "text": "hello", "sender": "human",
"content": [{"type":"text","text":"hello"}],
"created_at": "2026-03-01T10:00:00.000000Z"
}]
}]`
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile(
"file", "conversations.json",
)
require.NoError(t, err)
_, _ = part.Write([]byte(conversations))
writer.Close()
req := httptest.NewRequest(
http.MethodPost,
"/api/v1/import/claude-ai",
&body,
)
req.Header.Set(
"Content-Type", writer.FormDataContentType(),
)
req.Header.Set("Accept", "text/event-stream")
rec := httptest.NewRecorder()
srv.mux.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code, "body: %s", rec.Body.String())
require.Contains(t, rec.Header().Get("Content-Type"), "text/event-stream")
// Parse the done event from the SSE body.
var stats importer.ImportStats
lines := strings.Split(rec.Body.String(), "\n")
for i, line := range lines {
if line == "event: done" && i+1 < len(lines) {
data := strings.TrimPrefix(
lines[i+1], "data: ",
)
require.NoError(t, json.Unmarshal([]byte(data), &stats))
}
}
assert.Equal(t, 1, stats.Imported)
}
func TestHandleImportClaudeAI_NoFile(t *testing.T) {
srv := testServer(t, 5*time.Second)
var body bytes.Buffer
writer := multipart.NewWriter(&body)
writer.Close()
req := httptest.NewRequest(
http.MethodPost,
"/api/v1/import/claude-ai",
&body,
)
req.Header.Set("Content-Type", writer.FormDataContentType())
rec := httptest.NewRecorder()
srv.mux.ServeHTTP(rec, req)
require.Equal(t, http.StatusBadRequest, rec.Code, "body: %s", rec.Body.String())
}