61 lines
2.2 KiB
Go
61 lines
2.2 KiB
Go
package disabled_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/adapter/secondary/code_runner/disabled"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const secretSource = "print('secret-source-marker')"
|
|
|
|
func validRequest() outbound.CodeRunRequest {
|
|
return outbound.CodeRunRequest{
|
|
Key: outbound.CodeRunKey{AttemptItemID: uuid.New(), SubmissionID: uuid.New()},
|
|
Language: outbound.CodeLanguagePython3,
|
|
Source: secretSource,
|
|
Limits: outbound.CodeRunLimits{TimeLimitMs: 1000, MemoryLimitMB: 128, OutputLimitKB: 64},
|
|
Groups: []outbound.CodeTestGroup{{
|
|
ID: "public", Visibility: outbound.CodeTestPublic,
|
|
Checker: outbound.CodeChecker{Kind: outbound.CodeCheckerExact},
|
|
Tests: []outbound.CodeTest{{ID: "1", Input: "1\n", ExpectedOutput: "1\n"}},
|
|
}},
|
|
}
|
|
}
|
|
|
|
func TestRunnerReportsDisabledAndNeverAVerdict(t *testing.T) {
|
|
canceled, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
runner := disabled.NewRunner()
|
|
|
|
for name, ctx := range map[string]context.Context{"active context": context.Background(), "canceled context": canceled} {
|
|
for method, call := range map[string]func(context.Context, outbound.CodeRunRequest) (outbound.CodeRunStatus, error){
|
|
"Submit": runner.Submit, "Poll": runner.Poll,
|
|
} {
|
|
status, err := call(ctx, validRequest())
|
|
if !errors.Is(err, outbound.ErrCodeRunnerDisabled) || !errors.Is(err, outbound.ErrCodeRunnerUnavailable) {
|
|
t.Fatalf("%s %s error = %v, want ErrCodeRunnerDisabled under ErrCodeRunnerUnavailable", name, method, err)
|
|
}
|
|
if status != (outbound.CodeRunStatus{}) || status.Result != nil {
|
|
t.Fatalf("%s %s status = %+v, want the zero status", name, method, status)
|
|
}
|
|
if strings.Contains(err.Error(), "secret-source-marker") {
|
|
t.Fatalf("%s %s error exposes the source: %v", name, method, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRunnerRejectsInvalidRequestsBeforeReportingDisabled(t *testing.T) {
|
|
_, err := disabled.NewRunner().Submit(context.Background(), outbound.CodeRunRequest{})
|
|
|
|
if !errors.Is(err, outbound.ErrInvalidCodeRunRequest) || errors.Is(err, outbound.ErrCodeRunnerDisabled) {
|
|
t.Fatalf("Submit(empty) error = %v, want ErrInvalidCodeRunRequest only", err)
|
|
}
|
|
}
|