Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
5.8 KB
Raw Blame
package handler

import (
	"context"
	"errors"
	"net/http"
	"net/http/httptest"
	"sync"
	"testing"
	"time"

	"github.com/jackc/pgx/v5"
	"github.com/multica-ai/multica/server/internal/events"
	"github.com/multica-ai/multica/server/internal/testutil"
	db "github.com/multica-ai/multica/server/pkg/db/generated"
	"github.com/multica-ai/multica/server/pkg/protocol"
)

// A supporting-file write is a change to the skill: it must bump the skill's
// updated_at and announce skill:updated with the full file set, exactly like
// PUT /api/skills/{id} does. Before this, clients holding the skill never
// learned that a file had changed.
func TestSkillFileWritesPublishSkillUpdated(t *testing.T) {
	skillID := insertHandlerTestSkill(t, "skill-file-events", "body")

	var mu sync.Mutex
	var updates []SkillWithFilesResponse
	testHandler.Bus.Subscribe(protocol.EventSkillUpdated, func(e events.Event) {
		payload, ok := e.Payload.(map[string]any)
		if !ok {
			return
		}
		skill, ok := payload["skill"].(SkillWithFilesResponse)
		if !ok || skill.ID != skillID {
			return
		}
		mu.Lock()
		updates = append(updates, skill)
		mu.Unlock()
	})
	lastUpdate := func() (SkillWithFilesResponse, int) {
		mu.Lock()
		defer mu.Unlock()
		if len(updates) == 0 {
			return SkillWithFilesResponse{}, 0
		}
		return updates[len(updates)-1], len(updates)
	}
	updatedAt := func() time.Time {
		var ts time.Time
		if err := testPool.QueryRow(context.Background(),
			`SELECT updated_at FROM skill WHERE id = $1`, skillID).Scan(&ts); err != nil {
			t.Fatalf("read updated_at: %v", err)
		}
		return ts
	}

	before := updatedAt()

	w := httptest.NewRecorder()
	req := newRequest("PUT", "/api/skills/"+skillID+"/files", map[string]any{
		"path":    "notes/a.md",
		"content": "alpha",
	})
	req = withURLParam(req, "id", skillID)
	testHandler.UpsertSkillFile(w, req)
	if w.Code != http.StatusOK {
		t.Fatalf("UpsertSkillFile: expected 200, got %d: %s", w.Code, w.Body.String())
	}

	got, n := lastUpdate()
	if n != 1 {
		t.Fatalf("expected 1 skill:updated after upsert, got %d", n)
	}
	if len(got.Files) != 1 || got.Files[0].Path != "notes/a.md" || got.Files[0].Content != "alpha" {
		t.Fatalf("upsert event files = %+v, want the one upserted file", got.Files)
	}
	afterUpsert := updatedAt()
	if !afterUpsert.After(before) {
		t.Fatalf("upsert did not bump skill.updated_at: before %v, after %v", before, afterUpsert)
	}

	fileID := got.Files[0].ID
	w = httptest.NewRecorder()
	req = newRequest("DELETE", "/api/skills/"+skillID+"/files/"+fileID, nil)
	req = withURLParams(req, "id", skillID, "fileId", fileID)
	testHandler.DeleteSkillFile(w, req)
	if w.Code != http.StatusNoContent {
		t.Fatalf("DeleteSkillFile: expected 204, got %d: %s", w.Code, w.Body.String())
	}

	got, n = lastUpdate()
	if n != 2 {
		t.Fatalf("expected 2 skill:updated after delete, got %d", n)
	}
	if len(got.Files) != 0 {
		t.Fatalf("delete event files = %+v, want none", got.Files)
	}
	if !updatedAt().After(afterUpsert) {
		t.Fatal("delete did not bump skill.updated_at")
	}
}

func TestTouchSkillWorkspaceScope(t *testing.T) {
	ctx := context.Background()
	skillID := dbfx.Insert(t, "skill", testutil.Cols{
		"workspace_id": testWorkspaceID,
		"created_by":   testUserID,
		"name":         "touch-skill-workspace-scope",
		"updated_at":   testutil.Raw("now() - interval '1 hour'"),
	})
	otherWorkspaceID := dbfx.Workspace(t, "Other skill workspace", "touch-skill-other-"+skillID)
	original, err := testHandler.Queries.GetSkill(ctx, parseUUID(skillID))
	if err != nil {
		t.Fatal(err)
	}

	_, err = testHandler.Queries.TouchSkill(ctx, db.TouchSkillParams{
		ID:          original.ID,
		WorkspaceID: parseUUID(otherWorkspaceID),
	})
	if !errors.Is(err, pgx.ErrNoRows) {
		t.Fatalf("touch from another workspace: got %v, want no rows", err)
	}
	unchanged, err := testHandler.Queries.GetSkill(ctx, original.ID)
	if err != nil {
		t.Fatal(err)
	}
	if !unchanged.UpdatedAt.Time.Equal(original.UpdatedAt.Time) {
		t.Fatal("touch from another workspace changed the skill timestamp")
	}

	updated, err := testHandler.Queries.TouchSkill(ctx, db.TouchSkillParams{
		ID:          original.ID,
		WorkspaceID: original.WorkspaceID,
	})
	if err != nil {
		t.Fatal(err)
	}
	if !updated.UpdatedAt.Time.After(original.UpdatedAt.Time) {
		t.Fatal("touch from the owning workspace did not update the skill timestamp")
	}
}

func TestSkillFileWritesReturnNotFoundAfterSkillDeleted(t *testing.T) {
	for _, method := range []string{http.MethodPut, http.MethodDelete} {
		t.Run(method, func(t *testing.T) {
			skillID := dbfx.Insert(t, "skill", testutil.Cols{
				"workspace_id": testWorkspaceID,
				"created_by":   testUserID,
				"name":         "deleted-before-file-write-" + method,
			})
			fileID := dbfx.Insert(t, "skill_file", testutil.Cols{
				"skill_id": skillID,
				"path":     "notes.md",
				"content":  "original",
			})

			h := *testHandler
			h.Bus = events.New()
			updates := 0
			h.Bus.Subscribe(protocol.EventSkillUpdated, func(events.Event) { updates++ })
			deleted := false
			h.TxStarter = hookedTxStarter{base: testHandler.TxStarter, hook: func() error {
				dbfx.Exec(t, `DELETE FROM skill_file WHERE skill_id = $1`, skillID)
				dbfx.Exec(t, `DELETE FROM skill WHERE id = $1 AND workspace_id = $2`, skillID, testWorkspaceID)
				deleted = true
				return nil
			}}

			handler := h.UpsertSkillFile
			path := "/api/skills/" + skillID + "/files"
			if method == http.MethodDelete {
				handler = h.DeleteSkillFile
				path += "/" + fileID
			}
			req := newRequest(method, path, map[string]any{"path": "notes.md", "content": "changed"})
			req = testutil.WithURLParams(req, "id", skillID, "fileId", fileID)
			testutil.Call(t, handler, req).Want(http.StatusNotFound)
			if !deleted {
				t.Fatal("request did not reach the post-authorization deletion hook")
			}
			if updates != 0 {
				t.Fatalf("deleted skill published %d updates, want none", updates)
			}
		})
	}
}