feat: add diagnostic interview loop
This commit is contained in:
80
internal/httpapi/diagnostic.go
Normal file
80
internal/httpapi/diagnostic.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"tutor/internal/interview"
|
||||
)
|
||||
|
||||
type createDiagnosticSessionRequest struct {
|
||||
UserID string `json:"user_id"`
|
||||
TargetRole string `json:"target_role"`
|
||||
Stack []string `json:"stack"`
|
||||
InterviewTimeline string `json:"interview_timeline"`
|
||||
}
|
||||
|
||||
type submitDiagnosticAnswerRequest struct {
|
||||
QuestionID string `json:"question_id"`
|
||||
AnswerText string `json:"answer_text"`
|
||||
}
|
||||
|
||||
func (h Handler) createDiagnosticSession(w http.ResponseWriter, r *http.Request) {
|
||||
var req createDiagnosticSessionRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
|
||||
session, err := h.diagnostic.CreateSession(r.Context(), interview.CreateSessionInput{
|
||||
UserID: req.UserID,
|
||||
TargetRole: req.TargetRole,
|
||||
Stack: req.Stack,
|
||||
InterviewTimeline: req.InterviewTimeline,
|
||||
})
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, session)
|
||||
}
|
||||
|
||||
func (h Handler) getDiagnosticSession(w http.ResponseWriter, r *http.Request) {
|
||||
session, err := h.diagnostic.GetSession(r.PathValue("id"))
|
||||
if errors.Is(err, interview.ErrSessionNotFound) {
|
||||
writeError(w, http.StatusNotFound, "diagnostic session not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, "could not load diagnostic session")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, session)
|
||||
}
|
||||
|
||||
func (h Handler) submitDiagnosticAnswer(w http.ResponseWriter, r *http.Request) {
|
||||
var req submitDiagnosticAnswerRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
|
||||
answer, err := h.diagnostic.SubmitAnswer(r.Context(), interview.SubmitAnswerInput{
|
||||
SessionID: r.PathValue("id"),
|
||||
QuestionID: req.QuestionID,
|
||||
AnswerText: req.AnswerText,
|
||||
})
|
||||
if errors.Is(err, interview.ErrSessionNotFound) || errors.Is(err, interview.ErrQuestionNotFound) {
|
||||
writeError(w, http.StatusNotFound, err.Error())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, answer)
|
||||
}
|
||||
76
internal/httpapi/diagnostic_test.go
Normal file
76
internal/httpapi/diagnostic_test.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"tutor/internal/config"
|
||||
"tutor/internal/interview"
|
||||
"tutor/internal/workflows"
|
||||
)
|
||||
|
||||
func TestDiagnosticHTTPFlow(t *testing.T) {
|
||||
service := interview.NewService(interview.NewMemoryStore(), workflows.NewStubRunner())
|
||||
handler := NewHandler(config.Config{Environment: "test", ModelKey: "deepseek-v4-flash"}, service)
|
||||
routes := handler.Routes()
|
||||
|
||||
createBody := bytes.NewBufferString(`{
|
||||
"user_id":"user-1",
|
||||
"target_role":"junior backend developer",
|
||||
"stack":["go","postgres"],
|
||||
"interview_timeline":"30 days"
|
||||
}`)
|
||||
createReq := httptest.NewRequest(http.MethodPost, "/api/v1/diagnostic-sessions", createBody)
|
||||
createRec := httptest.NewRecorder()
|
||||
routes.ServeHTTP(createRec, createReq)
|
||||
|
||||
if createRec.Code != http.StatusCreated {
|
||||
t.Fatalf("create status = %d, body = %s", createRec.Code, createRec.Body.String())
|
||||
}
|
||||
|
||||
var session interview.Session
|
||||
if err := json.NewDecoder(createRec.Body).Decode(&session); err != nil {
|
||||
t.Fatalf("decode create response: %v", err)
|
||||
}
|
||||
if len(session.Questions) == 0 {
|
||||
t.Fatal("expected questions")
|
||||
}
|
||||
|
||||
answerBody := bytes.NewBufferString(`{
|
||||
"question_id":"` + session.Questions[0].ID + `",
|
||||
"answer_text":"Idempotent requests can be retried safely because repeated calls have the same intended effect."
|
||||
}`)
|
||||
answerReq := httptest.NewRequest(http.MethodPost, "/api/v1/diagnostic-sessions/"+session.ID+"/answers", answerBody)
|
||||
answerRec := httptest.NewRecorder()
|
||||
routes.ServeHTTP(answerRec, answerReq)
|
||||
|
||||
if answerRec.Code != http.StatusCreated {
|
||||
t.Fatalf("answer status = %d, body = %s", answerRec.Code, answerRec.Body.String())
|
||||
}
|
||||
|
||||
var answer interview.Answer
|
||||
if err := json.NewDecoder(answerRec.Body).Decode(&answer); err != nil {
|
||||
t.Fatalf("decode answer response: %v", err)
|
||||
}
|
||||
if len(answer.Grade.Evidence) == 0 {
|
||||
t.Fatal("expected grade evidence")
|
||||
}
|
||||
|
||||
getReq := httptest.NewRequest(http.MethodGet, "/api/v1/diagnostic-sessions/"+session.ID, nil)
|
||||
getRec := httptest.NewRecorder()
|
||||
routes.ServeHTTP(getRec, getReq)
|
||||
|
||||
if getRec.Code != http.StatusOK {
|
||||
t.Fatalf("get status = %d, body = %s", getRec.Code, getRec.Body.String())
|
||||
}
|
||||
var loaded interview.Session
|
||||
if err := json.NewDecoder(getRec.Body).Decode(&loaded); err != nil {
|
||||
t.Fatalf("decode get response: %v", err)
|
||||
}
|
||||
if len(loaded.Answers) != 1 {
|
||||
t.Fatalf("answers = %d, want 1", len(loaded.Answers))
|
||||
}
|
||||
}
|
||||
@@ -5,24 +5,27 @@ import (
|
||||
"net/http"
|
||||
|
||||
"tutor/internal/config"
|
||||
"tutor/internal/workflows"
|
||||
"tutor/internal/interview"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
cfg config.Config
|
||||
runner workflows.Runner
|
||||
cfg config.Config
|
||||
diagnostic *interview.Service
|
||||
}
|
||||
|
||||
func NewHandler(cfg config.Config, runner workflows.Runner) Handler {
|
||||
func NewHandler(cfg config.Config, diagnostic *interview.Service) Handler {
|
||||
return Handler{
|
||||
cfg: cfg,
|
||||
runner: runner,
|
||||
cfg: cfg,
|
||||
diagnostic: diagnostic,
|
||||
}
|
||||
}
|
||||
|
||||
func (h Handler) Routes() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", h.health)
|
||||
mux.HandleFunc("POST /api/v1/diagnostic-sessions", h.createDiagnosticSession)
|
||||
mux.HandleFunc("GET /api/v1/diagnostic-sessions/{id}", h.getDiagnosticSession)
|
||||
mux.HandleFunc("POST /api/v1/diagnostic-sessions/{id}/answers", h.submitDiagnosticAnswer)
|
||||
return mux
|
||||
}
|
||||
|
||||
@@ -45,3 +48,11 @@ func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
writeJSON(w, status, errorResponse{Error: message})
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"tutor/internal/config"
|
||||
"tutor/internal/interview"
|
||||
"tutor/internal/workflows"
|
||||
)
|
||||
|
||||
@@ -15,7 +16,8 @@ func TestHealth(t *testing.T) {
|
||||
Environment: "test",
|
||||
ModelKey: "deepseek-v4-flash",
|
||||
}
|
||||
handler := NewHandler(cfg, workflows.NewStubRunner())
|
||||
service := interview.NewService(interview.NewMemoryStore(), workflows.NewStubRunner())
|
||||
handler := NewHandler(cfg, service)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
Reference in New Issue
Block a user