Files
llmgw/internal/router/train_test.go
T
Ilker ManapandClaude Opus 5.5 a53b2f4be9 Customise a model from the portal: a file, a model, a button
A page for anyone in a project, built for the one who knows least: an
Excel, CSV or JSONL file of questions and answers, previewed; a model; a
name; who may use it; and a button. The weights to train on are found
from where the model's came (a GGUF repository's base model), the card's
memory from their size, the node as the one with the most free. When it
is done the adapter is published by itself (a llama.cpp model's replicas
replaced to load it) and is a model of its own in the chat and the API.

The project's by default; its members share it wider (confirmed, and
never wider than the knowledge bases its pairs came from) or delete it.
One trains at a time per project. ctl train start needs only --model and
--data now, and --publish publishes when done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-19 11:31:12 +01:00

166 lines
5.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package router
import (
"bytes"
"context"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"time"
"gitea.mynodes.xyz/ilker/llmgw/internal/control/adminapi"
"gitea.mynodes.xyz/ilker/llmgw/internal/store"
)
type fakeTraining struct {
started []adminapi.TrainRequest
jobs map[string]store.TrainJob
shared map[string]string
deleted []string
}
func (f *fakeTraining) TrainableModels() []string { return []string{"qwen"} }
func (f *fakeTraining) StartTraining(_ context.Context, in adminapi.TrainRequest, actor string) (store.TrainJob, error) {
f.started = append(f.started, in)
j := store.TrainJob{ID: "train-" + in.Name, Name: in.Name, Model: in.Model, Owner: in.Owner, Project: in.Project,
CreatedBy: actor, State: store.TrainQueued, Pairs: strings.Count(in.Data, "\n"), CreatedAt: time.Now()}
f.jobs[j.ID] = j
return j, nil
}
func (f *fakeTraining) Trainings(_ context.Context, projects []string, by string) ([]store.TrainJob, error) {
var out []store.TrainJob
for _, j := range f.jobs {
for _, p := range projects {
if j.Project == p {
out = append(out, j)
}
}
}
return out, nil
}
func (f *fakeTraining) Training(_ context.Context, id string) (store.TrainJob, error) {
j, ok := f.jobs[id]
if !ok {
return j, store.ErrNotFound
}
return j, nil
}
func (f *fakeTraining) Published(store.TrainJob) bool { return false }
func (f *fakeTraining) ShareTraining(_ context.Context, id, owner, _ string) error {
f.shared[id] = owner
return nil
}
func (f *fakeTraining) DeleteTraining(_ context.Context, id, _ string) error {
f.deleted = append(f.deleted, id)
return nil
}
func trainForm(t *testing.T, fields map[string]string, file string) *http.Request {
t.Helper()
var b bytes.Buffer
mw := multipart.NewWriter(&b)
for k, v := range fields {
_ = mw.WriteField(k, v)
}
if file != "" {
w, _ := mw.CreateFormFile("file", "ornekler.csv")
_, _ = w.Write([]byte(file))
}
_ = mw.Close()
req := httptest.NewRequest("POST", "/train/start", &b)
req.Header.Set("Content-Type", mw.FormDataContentType())
return req
}
func TestThePortalTrainsForAProjectItsMembersManage(t *testing.T) {
cfg := baseConfig()
cfg.Auth.Enabled = true
cfg.Auth.Store = filepath.Join(t.TempDir(), "store.db")
rt := newTestRouter(t, cfg)
ft := &fakeTraining{jobs: map[string]store.TrainJob{}, shared: map[string]string{}}
rt.SetTraining(ft)
ctx := t.Context()
org, _ := rt.store.CreateOrg(ctx, "acme")
team, _ := rt.store.CreateTeam(ctx, org, "support")
project, _ := rt.store.CreateProject(ctx, team, "bot")
ayse := store.User{ID: "usr-a", Email: "ayse@acme.example", State: store.UserActive, CreatedAt: time.Now()}
bob := store.User{ID: "usr-b", Email: "bob@else.example", State: store.UserActive, CreatedAt: time.Now()}
for _, u := range []store.User{ayse, bob} {
if err := rt.store.CreateUser(ctx, u); err != nil {
t.Fatal(err)
}
}
_ = rt.store.AddMembership(ctx, ayse.ID, store.ScopeProject, project, "member", "test", time.Now())
csv := "Soru,Cevap\nİzin kaç gün?,Yirmi gün.\nMasraf nasıl?,Formla.\n"
start := func(u store.User, fields map[string]string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
rt.trainStart(rec, trainForm(t, fields, csv), u)
return rec
}
good := map[string]string{"name": "destek", "model": "qwen", "project": project, "share": "project"}
if rec := start(bob, good); rec.Code != http.StatusForbidden {
t.Errorf("someone outside the project started one: %d %s", rec.Code, rec.Body.String())
}
wide := map[string]string{"name": "destek", "model": "qwen", "project": project, "share": "org"}
if rec := start(ayse, wide); rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "confirm") {
t.Errorf("shared with the organization unconfirmed: %d %s", rec.Code, rec.Body.String())
}
other := map[string]string{"name": "destek", "model": "llama-70b", "project": project, "share": "project"}
if rec := start(ayse, other); rec.Code != http.StatusBadRequest {
t.Errorf("a model not offered: %d", rec.Code)
}
if rec := start(ayse, good); rec.Code != http.StatusOK {
t.Fatalf("%d %s", rec.Code, rec.Body.String())
}
in := ft.started[0]
if in.Owner != "project:"+project || in.Project != project || !in.Publish || in.Public ||
in.Data != `{"q":"İzin kaç gün?","a":"Yirmi gün."}`+"\n"+`{"q":"Masraf nasıl?","a":"Formla."}`+"\n" {
t.Errorf("asked for %+v", in)
}
list := func(u store.User) []trainRow {
rec := httptest.NewRecorder()
rt.trainJobs(rec, httptest.NewRequest("GET", "/train/jobs", nil), u)
var rows []trainRow
_ = json.Unmarshal(rec.Body.Bytes(), &rows)
return rows
}
if rows := list(ayse); len(rows) != 1 || rows[0].Share != "project" || rows[0].Project != "acme / support / bot" ||
rows[0].CreatedBy != ayse.Email {
t.Errorf("ayse sees %+v", rows)
}
if rows := list(bob); len(rows) != 0 {
t.Errorf("bob sees %+v", rows)
}
act := func(u store.User, what string, fields map[string]string) int {
req := trainForm(t, fields, "")
req.SetPathValue("id", "train-destek")
rec := httptest.NewRecorder()
if what == "share" {
rt.trainShare(rec, req, u)
} else {
rt.trainDelete(rec, req, u)
}
return rec.Code
}
if code := act(bob, "delete", nil); code != http.StatusNotFound || len(ft.deleted) != 0 {
t.Errorf("bob deleted it: %d", code)
}
if code := act(ayse, "share", map[string]string{"share": "team", "confirm": "yes"}); code != http.StatusOK ||
ft.shared["train-destek"] != "team:"+team {
t.Errorf("shared with the team: %d %v", code, ft.shared)
}
if code := act(ayse, "share", map[string]string{"share": "all"}); code != http.StatusBadRequest {
t.Errorf("shared with everyone unconfirmed: %d", code)
}
if code := act(ayse, "delete", nil); code != http.StatusOK || len(ft.deleted) != 1 {
t.Errorf("ayse deleting: %d", code)
}
}