feat(assessment): report planned bank coverage
This commit is contained in:
1 parent
76d32bb79f
commit
a69b1a79bb
10 files changed
+1211
-288
No files matched your search
@@ -0,0 +1,225 @@
|
||||
// Package bankcoverage implements the local assessment bank coverage command.
|
||||
package bankcoverage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
assessment "lincodex/backend/internal/usecase/assessment"
|
||||
)
|
||||
|
||||
// Run reports how valid bundle drafts and implementation paths match the bank manifest.
|
||||
func Run(arguments []string, stdout, stderr io.Writer) error {
|
||||
const location = "bankcoverage.Run"
|
||||
flags := flag.NewFlagSet("assessment-bank-coverage", flag.ContinueOnError)
|
||||
flags.SetOutput(stderr)
|
||||
manifestPath := flags.String("manifest", "testdata/assessment/bank_manifest.json", "path to the planned bank manifest")
|
||||
bundleDir := flags.String("bundles", "testdata/assessment", "directory containing bank bundle JSON files and solution files")
|
||||
strict := flags.Bool("strict", false, "fail when planned families, review declarations, or implementation paths are missing")
|
||||
if err := flags.Parse(arguments); err != nil {
|
||||
return fmt.Errorf("%s - parse arguments: %w", location, err)
|
||||
}
|
||||
if flags.NArg() != 0 {
|
||||
return fmt.Errorf("%s - unexpected positional arguments", location)
|
||||
}
|
||||
content, err := os.ReadFile(*manifestPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read manifest: %w", location, err)
|
||||
}
|
||||
manifest, err := assessment.ParseBankCoverageManifest(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
inventory, err := loadBankCoverageInventory(*bundleDir, *manifestPath, manifest)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - load bundle inventory: %w", location, err)
|
||||
}
|
||||
report, err := assessment.BuildBankCoverageReport(manifest, inventory)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - build report: %w", location, err)
|
||||
}
|
||||
if err := printBankCoverageReport(stdout, report); err != nil {
|
||||
return fmt.Errorf("%s - write report: %w", location, err)
|
||||
}
|
||||
if *strict {
|
||||
return report.StrictError()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadBankCoverageInventory(bundleDir, manifestPath string, manifest assessment.BankCoverageManifest) (result assessment.BankCoverageInventory, returnErr error) {
|
||||
const location = "bankcoverage.loadBankCoverageInventory"
|
||||
manifestPath, err := canonicalPath(manifestPath)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - resolve manifest path: %w", location, err)
|
||||
}
|
||||
bundleRoot, err := os.OpenRoot(bundleDir)
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - open bundle directory: %w", location, err)
|
||||
}
|
||||
defer func() {
|
||||
if err := bundleRoot.Close(); err != nil {
|
||||
returnErr = errors.Join(returnErr, fmt.Errorf("%s - close bundle directory: %w", location, err))
|
||||
}
|
||||
}()
|
||||
inventory := assessment.BankCoverageInventory{}
|
||||
err = fs.WalkDir(bundleRoot.FS(), ".", func(path string, entry fs.DirEntry, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return fmt.Errorf("%s - scan bundle directory: %w", location, walkErr)
|
||||
}
|
||||
if entry.IsDir() || filepath.Ext(path) != ".json" {
|
||||
return nil
|
||||
}
|
||||
candidatePath, err := canonicalPath(filepath.Join(bundleDir, filepath.FromSlash(path)))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - resolve candidate path %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
if candidatePath == manifestPath {
|
||||
return nil
|
||||
}
|
||||
content, err := bundleRoot.ReadFile(filepath.FromSlash(path))
|
||||
if err != nil {
|
||||
return fmt.Errorf("%s - read bundle %s: %w", location, filepath.ToSlash(path), err)
|
||||
}
|
||||
bundle, err := assessment.ParseBundle(bytes.NewReader(content))
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
drafts, err := bundle.Drafts()
|
||||
if err != nil {
|
||||
inventory.InvalidBundleFiles = append(inventory.InvalidBundleFiles, fmt.Sprintf("%s: %v", filepath.ToSlash(path), err))
|
||||
return nil
|
||||
}
|
||||
for _, draft := range drafts {
|
||||
core := draft.Core()
|
||||
inventory.Families = append(inventory.Families, assessment.BankCoverageFamilyRecord{
|
||||
Key: string(core.Key), Category: string(core.Category), Version: core.Version,
|
||||
Author: string(core.Author), Reviewer: string(core.Reviewer),
|
||||
ReviewStatus: string(core.ReviewStatus), Source: filepath.ToSlash(path),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return assessment.BankCoverageInventory{}, err
|
||||
}
|
||||
if err := collectImplementationPaths(bundleRoot, manifest, &inventory); err != nil {
|
||||
return assessment.BankCoverageInventory{}, fmt.Errorf("%s - collect implementation paths: %w", location, err)
|
||||
}
|
||||
return inventory, nil
|
||||
}
|
||||
|
||||
func collectImplementationPaths(root *os.Root, manifest assessment.BankCoverageManifest, inventory *assessment.BankCoverageInventory) error {
|
||||
const location = "bankcoverage.collectImplementationPaths"
|
||||
for _, category := range manifest.Categories {
|
||||
for _, topic := range category.Topics {
|
||||
for _, key := range topic.FamilyKeys {
|
||||
for _, language := range category.RequiredLanguages {
|
||||
relative := filepath.Join(category.ImplementationsDir, key+implementationExtension(language))
|
||||
info, err := root.Stat(relative)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("%s - inspect %s: %w", location, filepath.ToSlash(relative), err)
|
||||
}
|
||||
if info.Mode().IsRegular() && info.Size() > 0 {
|
||||
inventory.ImplementationFiles = append(inventory.ImplementationFiles, filepath.ToSlash(relative))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func canonicalPath(path string) (string, error) {
|
||||
absPath, err := filepath.Abs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resolvedPath, err := filepath.EvalSymlinks(absPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Clean(resolvedPath), nil
|
||||
}
|
||||
|
||||
func implementationExtension(language string) string {
|
||||
if language == "python" {
|
||||
return ".py"
|
||||
}
|
||||
return ".go"
|
||||
}
|
||||
|
||||
func printBankCoverageReport(writer io.Writer, report assessment.BankCoverageReport) error {
|
||||
const location = "bankcoverage.printBankCoverageReport"
|
||||
write := func(format string, values ...any) error {
|
||||
if _, err := fmt.Fprintf(writer, format, values...); err != nil {
|
||||
return fmt.Errorf("%s - write output: %w", location, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := write("Assessment bank coverage inventory\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := write("Family records are valid drafts from bundle files; this report does not publish or calibrate them.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, category := range report.Categories {
|
||||
if err := write("%s: %d/%d planned family keys present; %d keys inventoried; %d/%d implementation paths present\n",
|
||||
category.Code, category.PresentFamilies, category.PlannedFamilies, category.InventoriedFamilies,
|
||||
category.PresentImplementationFiles, category.RequiredImplementationFiles); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := write("Review declarations: %d approved, %d pending, %d rejected; %d distinct family keys inventoried\n",
|
||||
report.DeclaredApprovedReviews, report.PendingReviewDeclarations, report.RejectedReviewDeclarations, report.InventoriedFamilies); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, names := range []struct {
|
||||
label string
|
||||
names []string
|
||||
}{
|
||||
{"Missing family keys", report.MissingFamilyKeys},
|
||||
{"Families without an approved independent reviewer declaration", report.MissingIndependentReviews},
|
||||
{"Missing Go/Python implementation paths", report.MissingImplementationFiles},
|
||||
{"Unassigned family keys", report.UnassignedFamilyKeys},
|
||||
{"Unassigned categories", report.UnassignedCategories},
|
||||
{"Category mismatches", report.CategoryMismatches},
|
||||
{"Invalid bundle records", report.InvalidBundleFiles},
|
||||
{"Evidence not verifiable by this check", report.UnverifiableEvidence},
|
||||
} {
|
||||
if err := printNames(writer, names.label, names.names); err != nil {
|
||||
return fmt.Errorf("%s - print %s: %w", location, names.label, err)
|
||||
}
|
||||
}
|
||||
if report.CalibrationMeasurementRequired {
|
||||
if err := write("Calibration thresholds remain external measurement gates; no quality result is inferred.\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printNames(writer io.Writer, label string, names []string) error {
|
||||
if len(names) == 0 {
|
||||
return nil
|
||||
}
|
||||
if _, err := fmt.Fprintf(writer, "%s (%d):\n", label, len(names)); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, err := fmt.Fprintf(writer, " %s\n", name); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package bankcoverage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const assessmentFixturePath = "../../../../../testdata/assessment"
|
||||
|
||||
func TestRunReportsIncompleteInventoryWithoutFailing(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want informational success", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "backend: 0/25 planned family keys present") ||
|
||||
!strings.Contains(output.String(), "network: 0/15 planned family keys present") {
|
||||
t.Fatalf("report omitted incomplete family counts:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunStrictRefusesIncompleteInventory(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
var output bytes.Buffer
|
||||
err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output)
|
||||
if err == nil || !strings.Contains(err.Error(), "assessment.BankCoverageReport.StrictError") {
|
||||
t.Fatalf("run(--strict) error = %v, want strict incomplete-inventory error", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "Missing family keys (40)") {
|
||||
t.Fatalf("strict report omitted missing keys:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunReportsMalformedBundleAndStrictModeRejectsIt(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
malformedPath := filepath.Join(bundleDir, "malformed.json")
|
||||
if err := os.WriteFile(malformedPath, []byte("not a bundle"), 0o600); err != nil {
|
||||
t.Fatalf("write malformed bundle: %v", err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want informational success", err)
|
||||
}
|
||||
if !strings.Contains(output.String(), "Invalid bundle records (1)") || !strings.Contains(output.String(), "malformed.json") {
|
||||
t.Fatalf("report omitted malformed bundle diagnostic:\n%s", output.String())
|
||||
}
|
||||
if err := Run([]string{"--strict", "--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err == nil {
|
||||
t.Fatal("run(--strict) error = nil, want malformed bundle refusal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunSkipsTheCustomManifestInsideBundleDirectory(t *testing.T) {
|
||||
_, bundleDir := prepareCoverageFixtures(t)
|
||||
manifestPath := filepath.Join(bundleDir, "plan-custom.json")
|
||||
content, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read manifest fixture: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(manifestPath, content, 0o600); err != nil {
|
||||
t.Fatalf("write custom manifest: %v", err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
if err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, &output, &output); err != nil {
|
||||
t.Fatalf("run() error = %v, want report success", err)
|
||||
}
|
||||
if strings.Contains(output.String(), "plan-custom.json") {
|
||||
t.Fatalf("custom manifest was parsed as a bundle:\n%s", output.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPropagatesOutputWriteFailure(t *testing.T) {
|
||||
manifestPath, bundleDir := prepareCoverageFixtures(t)
|
||||
writeErr := errors.New("closed output")
|
||||
err := Run([]string{"--manifest", manifestPath, "--bundles", bundleDir}, failingCoverageWriter{err: writeErr}, &bytes.Buffer{})
|
||||
if !errors.Is(err, writeErr) {
|
||||
t.Fatalf("run() error = %v, want wrapped output error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func prepareCoverageFixtures(t *testing.T) (string, string) {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
bundleDir := filepath.Join(root, "bundles")
|
||||
if err := os.Mkdir(bundleDir, 0o700); err != nil {
|
||||
t.Fatalf("create bundle directory: %v", err)
|
||||
}
|
||||
manifestContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "bank_manifest.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read manifest fixture: %v", err)
|
||||
}
|
||||
manifestPath := filepath.Join(root, "bank-plan.json")
|
||||
if err := os.WriteFile(manifestPath, manifestContent, 0o600); err != nil {
|
||||
t.Fatalf("write manifest fixture: %v", err)
|
||||
}
|
||||
bundleContent, err := os.ReadFile(filepath.Join(assessmentFixturePath, "backend_demo_bundle.json"))
|
||||
if err != nil {
|
||||
t.Fatalf("read demo bundle fixture: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(bundleDir, "backend_demo_bundle.json"), bundleContent, 0o600); err != nil {
|
||||
t.Fatalf("write demo bundle fixture: %v", err)
|
||||
}
|
||||
return manifestPath, bundleDir
|
||||
}
|
||||
|
||||
type failingCoverageWriter struct {
|
||||
err error
|
||||
}
|
||||
|
||||
func (writer failingCoverageWriter) Write([]byte) (int, error) {
|
||||
return 0, writer.err
|
||||
}
|
||||
@@ -72,9 +72,9 @@ func NewStorage(settings config.Config) (*Storage, func(), error) {
|
||||
CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse },
|
||||
},
|
||||
// A non-seekable upload cannot be replayed safely.
|
||||
Retryer: aws.NopRetryer{},
|
||||
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
|
||||
ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
|
||||
Retryer: aws.NopRetryer{},
|
||||
RequestChecksumCalculation: aws.RequestChecksumCalculationWhenRequired,
|
||||
ResponseChecksumValidation: aws.ResponseChecksumValidationWhenRequired,
|
||||
DisableLogOutputChecksumValidationSkipped: true,
|
||||
})
|
||||
return &Storage{client: client, bucket: settings.SeaweedFSBucket, timeout: settings.SeaweedFSTimeout}, transport.CloseIdleConnections, nil
|
||||
|
||||
@@ -0,0 +1,535 @@
|
||||
package assessment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
maxBankManifestBytes = 1 << 20
|
||||
bankCoverageManifestVersion = 1
|
||||
maximumDifficultySpreadLogits = 0.3
|
||||
minimumItemDiscrimination = 0.5
|
||||
minimumPointBiserial = 0.2
|
||||
)
|
||||
|
||||
var requiredBankPlan = map[string]map[string]int{
|
||||
"backend": {
|
||||
"algorithms": 5, "data-structures": 4, "http-api": 4,
|
||||
"sql-transactions": 4, "concurrency": 4, "debugging-testing": 4,
|
||||
},
|
||||
"network": {
|
||||
"addressing-subnetting": 3, "ethernet-vlan": 3, "routing-ospf": 3,
|
||||
"dns-dhcp-tcp": 3, "configuration-diagnostics": 3,
|
||||
},
|
||||
}
|
||||
|
||||
var requiredBankLanguages = map[string][]string{
|
||||
"backend": {"go", "python"},
|
||||
"network": nil,
|
||||
}
|
||||
|
||||
var requiredBankContent = []string{"answer_key", "rubric", "seed_generator"}
|
||||
var requiredBankEvidence = []string{"grade_coverage", "independent_review", "reference_seeds"}
|
||||
|
||||
// BankCoverageManifest describes the planned bank inventory. It does not mark
|
||||
// any family as reviewed, calibrated, published, or available to candidates.
|
||||
type BankCoverageManifest struct {
|
||||
Version int `json:"version"`
|
||||
Categories []BankCoverageCategoryPlan `json:"categories"`
|
||||
RequiredContent []string `json:"required_content"`
|
||||
RequiredEvidence []string `json:"required_evidence"`
|
||||
CalibrationGates BankCalibrationGatePlan `json:"calibration_gates"`
|
||||
}
|
||||
|
||||
type BankCoverageCategoryPlan struct {
|
||||
Code string `json:"code"`
|
||||
RequiredFamilyCount int `json:"required_family_count"`
|
||||
RequiredLanguages []string `json:"required_languages"`
|
||||
ImplementationsDir string `json:"implementations_dir"`
|
||||
Topics []BankCoverageTopicPlan `json:"topics"`
|
||||
}
|
||||
|
||||
type BankCoverageTopicPlan struct {
|
||||
Name string `json:"name"`
|
||||
RequiredFamilyCount int `json:"required_family_count"`
|
||||
FamilyKeys []string `json:"family_keys"`
|
||||
}
|
||||
|
||||
type BankCalibrationGatePlan struct {
|
||||
ExternalMeasurementRequired bool `json:"external_measurement_required"`
|
||||
MaximumDifficultySpreadLogits float64 `json:"maximum_difficulty_spread_logits"`
|
||||
MinimumDiscrimination float64 `json:"minimum_discrimination"`
|
||||
MinimumPointBiserial float64 `json:"minimum_point_biserial"`
|
||||
}
|
||||
|
||||
// BankCoverageInventory contains validated family drafts and artifact paths
|
||||
// collected by a caller such as the assessment operator command.
|
||||
type BankCoverageInventory struct {
|
||||
Families []BankCoverageFamilyRecord
|
||||
ImplementationFiles []string
|
||||
InvalidBundleFiles []string
|
||||
}
|
||||
|
||||
type BankCoverageFamilyRecord struct {
|
||||
Key string
|
||||
Category string
|
||||
Version int32
|
||||
Author string
|
||||
Reviewer string
|
||||
ReviewStatus string
|
||||
Source string
|
||||
}
|
||||
|
||||
type BankCoverageCategoryReport struct {
|
||||
Code string
|
||||
PlannedFamilies int
|
||||
PresentFamilies int
|
||||
InventoriedFamilies int
|
||||
RequiredImplementationFiles int
|
||||
PresentImplementationFiles int
|
||||
}
|
||||
|
||||
// BankCoverageReport is a snapshot of planned family keys and artifact paths.
|
||||
// Review status is reported as bundle metadata only; this report never
|
||||
// authorizes publication or candidate availability.
|
||||
type BankCoverageReport struct {
|
||||
Categories []BankCoverageCategoryReport
|
||||
InventoriedFamilies int
|
||||
DeclaredApprovedReviews int
|
||||
PendingReviewDeclarations int
|
||||
RejectedReviewDeclarations int
|
||||
MissingFamilyKeys []string
|
||||
MissingIndependentReviews []string
|
||||
CategoryMismatches []string
|
||||
UnassignedFamilyKeys []string
|
||||
UnassignedCategories []string
|
||||
MissingImplementationFiles []string
|
||||
InvalidBundleFiles []string
|
||||
UnverifiableEvidence []string
|
||||
CalibrationMeasurementRequired bool
|
||||
}
|
||||
|
||||
type bankManifestFamily struct {
|
||||
category string
|
||||
implementationsDir string
|
||||
languages []string
|
||||
}
|
||||
|
||||
// ParseBankCoverageManifest strictly decodes and validates the planned bank
|
||||
// inventory. Its 25 Backend and 15 Network family counts are the initial
|
||||
// authoring floor within the PRD's wider target ranges.
|
||||
func ParseBankCoverageManifest(content []byte) (BankCoverageManifest, error) {
|
||||
const location = "assessment.ParseBankCoverageManifest"
|
||||
if len(content) > maxBankManifestBytes {
|
||||
return BankCoverageManifest{}, fmt.Errorf("%s - manifest exceeds %d bytes: %w", location, maxBankManifestBytes, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(content))
|
||||
decoder.DisallowUnknownFields()
|
||||
var manifest BankCoverageManifest
|
||||
if err := decoder.Decode(&manifest); err != nil {
|
||||
return BankCoverageManifest{}, fmt.Errorf("%s - decode manifest: %w: %w", location, domain.ErrInvalidAssessmentFamily, err)
|
||||
}
|
||||
var trailing any
|
||||
if err := decoder.Decode(&trailing); !errors.Is(err, io.EOF) {
|
||||
if err == nil {
|
||||
return BankCoverageManifest{}, fmt.Errorf("%s - trailing data after manifest: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
return BankCoverageManifest{}, fmt.Errorf("%s - read after manifest: %w: %w", location, domain.ErrInvalidAssessmentFamily, err)
|
||||
}
|
||||
if err := validateBankCoverageManifest(manifest); err != nil {
|
||||
return BankCoverageManifest{}, fmt.Errorf("%s - validate manifest: %w", location, err)
|
||||
}
|
||||
return manifest, nil
|
||||
}
|
||||
|
||||
// BuildBankCoverageReport matches validated bundle inventory against the
|
||||
// planned family keys. Missing content and artifact paths are report data, not
|
||||
// errors, so incomplete authoring does not fail ordinary backend checks.
|
||||
func BuildBankCoverageReport(manifest BankCoverageManifest, inventory BankCoverageInventory) (BankCoverageReport, error) {
|
||||
const location = "assessment.BuildBankCoverageReport"
|
||||
if err := validateBankCoverageManifest(manifest); err != nil {
|
||||
return BankCoverageReport{}, fmt.Errorf("%s - validate manifest: %w", location, err)
|
||||
}
|
||||
build := newBankCoverageBuild(manifest, inventory)
|
||||
build.addFamilyRecords(inventory.Families)
|
||||
build.countObservedFamilies()
|
||||
build.countMissingRequirements(inventory.ImplementationFiles)
|
||||
return build.finish(), nil
|
||||
}
|
||||
|
||||
type bankCoverageBuild struct {
|
||||
report BankCoverageReport
|
||||
planned map[string]bankManifestFamily
|
||||
observed map[string]BankCoverageFamilyRecord
|
||||
unassignedKeys map[string]struct{}
|
||||
unassignedCategory map[string]struct{}
|
||||
categoryReports map[string]*BankCoverageCategoryReport
|
||||
}
|
||||
|
||||
func newBankCoverageBuild(manifest BankCoverageManifest, inventory BankCoverageInventory) bankCoverageBuild {
|
||||
build := bankCoverageBuild{
|
||||
report: newBankCoverageReport(manifest),
|
||||
planned: indexBankManifestFamilies(manifest),
|
||||
observed: make(map[string]BankCoverageFamilyRecord, len(inventory.Families)),
|
||||
unassignedKeys: make(map[string]struct{}),
|
||||
unassignedCategory: make(map[string]struct{}),
|
||||
categoryReports: make(map[string]*BankCoverageCategoryReport, len(manifest.Categories)),
|
||||
}
|
||||
build.report.InvalidBundleFiles = slices.Clone(inventory.InvalidBundleFiles)
|
||||
for index := range build.report.Categories {
|
||||
build.categoryReports[build.report.Categories[index].Code] = &build.report.Categories[index]
|
||||
}
|
||||
return build
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) addFamilyRecords(families []BankCoverageFamilyRecord) {
|
||||
for _, family := range families {
|
||||
if !validBankCoverageFamilyRecord(family) {
|
||||
build.report.InvalidBundleFiles = append(build.report.InvalidBundleFiles,
|
||||
fmt.Sprintf("%s: invalid family record %q", family.Source, family.Key))
|
||||
continue
|
||||
}
|
||||
build.classifyFamily(family)
|
||||
build.recordFamilyVersion(family)
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) classifyFamily(family BankCoverageFamilyRecord) {
|
||||
plannedFamily, isPlanned := build.planned[family.Key]
|
||||
if !isPlanned {
|
||||
build.unassignedKeys[family.Key] = struct{}{}
|
||||
}
|
||||
if build.categoryReports[family.Category] == nil {
|
||||
build.unassignedCategory[family.Category] = struct{}{}
|
||||
}
|
||||
if isPlanned && family.Category != plannedFamily.category {
|
||||
build.report.CategoryMismatches = append(build.report.CategoryMismatches,
|
||||
fmt.Sprintf("%s: expected %s, found %s", family.Key, plannedFamily.category, family.Category))
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) recordFamilyVersion(family BankCoverageFamilyRecord) {
|
||||
current, exists := build.observed[family.Key]
|
||||
if exists && current.Category != family.Category {
|
||||
build.report.InvalidBundleFiles = append(build.report.InvalidBundleFiles,
|
||||
fmt.Sprintf("%s: family %s appears under categories %s and %s", family.Source, family.Key, current.Category, family.Category))
|
||||
return
|
||||
}
|
||||
if !exists || family.Version > current.Version {
|
||||
build.observed[family.Key] = family
|
||||
return
|
||||
}
|
||||
if family.Version == current.Version {
|
||||
build.report.InvalidBundleFiles = append(build.report.InvalidBundleFiles,
|
||||
fmt.Sprintf("%s: family %s version %d appears in more than one bundle", family.Source, family.Key, family.Version))
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) countObservedFamilies() {
|
||||
for key, family := range build.observed {
|
||||
build.report.InventoriedFamilies++
|
||||
if categoryReport := build.categoryReports[family.Category]; categoryReport != nil {
|
||||
categoryReport.InventoriedFamilies++
|
||||
}
|
||||
build.countReviewDeclaration(family)
|
||||
plannedFamily, isPlanned := build.planned[key]
|
||||
if !isPlanned || family.Category != plannedFamily.category {
|
||||
continue
|
||||
}
|
||||
build.categoryReports[plannedFamily.category].PresentFamilies++
|
||||
if family.ReviewStatus != string(domain.AssessmentReviewApproved) || family.Reviewer == "" || family.Reviewer == family.Author {
|
||||
build.report.MissingIndependentReviews = append(build.report.MissingIndependentReviews, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) countReviewDeclaration(family BankCoverageFamilyRecord) {
|
||||
switch family.ReviewStatus {
|
||||
case string(domain.AssessmentReviewApproved):
|
||||
if family.Reviewer != "" && family.Reviewer != family.Author {
|
||||
build.report.DeclaredApprovedReviews++
|
||||
}
|
||||
case string(domain.AssessmentReviewRejected):
|
||||
build.report.RejectedReviewDeclarations++
|
||||
default:
|
||||
build.report.PendingReviewDeclarations++
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) countMissingRequirements(paths []string) {
|
||||
implementationFiles := make(map[string]struct{}, len(paths))
|
||||
for _, path := range paths {
|
||||
implementationFiles[filepath.ToSlash(filepath.Clean(path))] = struct{}{}
|
||||
}
|
||||
for key, family := range build.planned {
|
||||
observedFamily, exists := build.observed[key]
|
||||
if !exists || observedFamily.Category != family.category {
|
||||
build.report.MissingFamilyKeys = append(build.report.MissingFamilyKeys, key)
|
||||
}
|
||||
build.countMissingImplementations(key, family, implementationFiles)
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) countMissingImplementations(key string, family bankManifestFamily, present map[string]struct{}) {
|
||||
for _, language := range family.languages {
|
||||
relative := filepath.ToSlash(filepath.Join(family.implementationsDir, key+implementationExtension(language)))
|
||||
if _, exists := present[relative]; !exists {
|
||||
build.report.MissingImplementationFiles = append(build.report.MissingImplementationFiles, relative)
|
||||
continue
|
||||
}
|
||||
build.categoryReports[family.category].PresentImplementationFiles++
|
||||
}
|
||||
}
|
||||
|
||||
func (build *bankCoverageBuild) finish() BankCoverageReport {
|
||||
for key := range build.unassignedKeys {
|
||||
build.report.UnassignedFamilyKeys = append(build.report.UnassignedFamilyKeys, key)
|
||||
}
|
||||
for category := range build.unassignedCategory {
|
||||
build.report.UnassignedCategories = append(build.report.UnassignedCategories, category)
|
||||
}
|
||||
slices.Sort(build.report.MissingFamilyKeys)
|
||||
slices.Sort(build.report.MissingIndependentReviews)
|
||||
slices.Sort(build.report.CategoryMismatches)
|
||||
slices.Sort(build.report.UnassignedFamilyKeys)
|
||||
slices.Sort(build.report.UnassignedCategories)
|
||||
slices.Sort(build.report.MissingImplementationFiles)
|
||||
slices.Sort(build.report.InvalidBundleFiles)
|
||||
return build.report
|
||||
}
|
||||
|
||||
// StrictError reports missing inventory requirements that the checker can
|
||||
// verify. It does not certify the external review, calibration, or semantic
|
||||
// equivalence of reference implementations.
|
||||
func (report BankCoverageReport) StrictError() error {
|
||||
const location = "assessment.BankCoverageReport.StrictError"
|
||||
if len(report.MissingFamilyKeys) == 0 && len(report.MissingIndependentReviews) == 0 &&
|
||||
len(report.MissingImplementationFiles) == 0 && len(report.InvalidBundleFiles) == 0 &&
|
||||
len(report.CategoryMismatches) == 0 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s - planned bank inventory has %d missing families, %d missing review declarations, %d missing implementation paths, and %d invalid bundle records", location,
|
||||
len(report.MissingFamilyKeys), len(report.MissingIndependentReviews), len(report.MissingImplementationFiles), len(report.InvalidBundleFiles)+len(report.CategoryMismatches))
|
||||
}
|
||||
|
||||
func validateBankCoverageManifest(manifest BankCoverageManifest) error {
|
||||
const location = "assessment.validateBankCoverageManifest"
|
||||
if manifest.Version != bankCoverageManifestVersion {
|
||||
return fmt.Errorf("%s - version must be %d: %w", location, bankCoverageManifestVersion, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if len(manifest.Categories) != len(requiredBankPlan) {
|
||||
return fmt.Errorf("%s - exactly backend and network categories are required: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if err := validateBankCoverageCategories(manifest.Categories); err != nil {
|
||||
return fmt.Errorf("%s - validate category plans: %w", location, err)
|
||||
}
|
||||
if !sameStringSet(manifest.RequiredContent, requiredBankContent) {
|
||||
return fmt.Errorf("%s - required content must include seed_generator, answer_key, and rubric: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if !sameStringSet(manifest.RequiredEvidence, requiredBankEvidence) {
|
||||
return fmt.Errorf("%s - required evidence must include grade_coverage, reference_seeds, and independent_review: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
gates := manifest.CalibrationGates
|
||||
if !gates.ExternalMeasurementRequired || gates.MaximumDifficultySpreadLogits != maximumDifficultySpreadLogits ||
|
||||
gates.MinimumDiscrimination != minimumItemDiscrimination || gates.MinimumPointBiserial != minimumPointBiserial {
|
||||
return fmt.Errorf("%s - calibration thresholds must remain external gates at 0.3, 0.5, and 0.2: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBankCoverageCategories(categories []BankCoverageCategoryPlan) error {
|
||||
const location = "assessment.validateBankCoverageCategories"
|
||||
seenCategories := make(map[string]struct{}, len(categories))
|
||||
seenFamilyKeys := make(map[string]struct{})
|
||||
for _, category := range categories {
|
||||
expectedTopics, ok := requiredBankPlan[category.Code]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s - unknown category %q: %w", location, category.Code, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if _, exists := seenCategories[category.Code]; exists {
|
||||
return fmt.Errorf("%s - category %q appears twice: %w", location, category.Code, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
seenCategories[category.Code] = struct{}{}
|
||||
if err := validateBankCoverageCategory(category, expectedTopics, seenFamilyKeys); err != nil {
|
||||
return fmt.Errorf("%s - category %s: %w", location, category.Code, err)
|
||||
}
|
||||
}
|
||||
for category := range requiredBankPlan {
|
||||
if _, ok := seenCategories[category]; !ok {
|
||||
return fmt.Errorf("%s - category %s is missing: %w", location, category, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBankCoverageCategory(category BankCoverageCategoryPlan, expectedTopics map[string]int, seenFamilyKeys map[string]struct{}) error {
|
||||
const location = "assessment.validateBankCoverageCategory"
|
||||
if category.RequiredFamilyCount != sumTopicCounts(expectedTopics) {
|
||||
return fmt.Errorf("%s - requires %d planned families: %w", location, sumTopicCounts(expectedTopics), domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if !sameStringSet(category.RequiredLanguages, requiredBankLanguages[category.Code]) {
|
||||
return fmt.Errorf("%s - has unsupported required languages: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if len(category.RequiredLanguages) > 0 {
|
||||
if err := validateImplementationDir(category.ImplementationsDir); err != nil {
|
||||
return fmt.Errorf("%s - implementation directory: %w", location, err)
|
||||
}
|
||||
} else if category.ImplementationsDir != "" {
|
||||
return fmt.Errorf("%s - must not declare implementation files: %w", location, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
return validateBankCoverageTopics(category.Code, category.Topics, expectedTopics, seenFamilyKeys)
|
||||
}
|
||||
|
||||
func validateBankCoverageTopics(category string, topics []BankCoverageTopicPlan, expectedTopics map[string]int, seenFamilyKeys map[string]struct{}) error {
|
||||
const location = "assessment.validateBankCoverageTopics"
|
||||
if len(topics) != len(expectedTopics) {
|
||||
return fmt.Errorf("%s - category %s has an unexpected topic set: %w", location, category, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
seenTopics := make(map[string]struct{}, len(topics))
|
||||
for _, topic := range topics {
|
||||
requiredCount, ok := expectedTopics[topic.Name]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s - category %s has unknown topic %q: %w", location, category, topic.Name, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
if _, exists := seenTopics[topic.Name]; exists {
|
||||
return fmt.Errorf("%s - category %s topic %q appears twice: %w", location, category, topic.Name, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
seenTopics[topic.Name] = struct{}{}
|
||||
if err := validateBankCoverageTopic(category, topic, requiredCount, seenFamilyKeys); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for name := range expectedTopics {
|
||||
if _, ok := seenTopics[name]; !ok {
|
||||
return fmt.Errorf("%s - category %s is missing topic %q: %w", location, category, name, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateBankCoverageTopic(category string, topic BankCoverageTopicPlan, requiredCount int, seenFamilyKeys map[string]struct{}) error {
|
||||
const location = "assessment.validateBankCoverageTopic"
|
||||
if topic.RequiredFamilyCount != requiredCount || len(topic.FamilyKeys) != requiredCount {
|
||||
return fmt.Errorf("%s - category %s topic %q requires %d family keys: %w", location, category, topic.Name, requiredCount, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
for _, key := range topic.FamilyKeys {
|
||||
if _, err := domain.ParseAssessmentFamilyKey(key); err != nil {
|
||||
return fmt.Errorf("%s - category %s topic %s has an invalid family key %q: %w", location, category, topic.Name, key, err)
|
||||
}
|
||||
if _, exists := seenFamilyKeys[key]; exists {
|
||||
return fmt.Errorf("%s - family key %q appears more than once: %w", location, key, domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
seenFamilyKeys[key] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateImplementationDir(value string) error {
|
||||
if value == "" || filepath.IsAbs(value) {
|
||||
return fmt.Errorf("must be a non-empty relative path: %w", domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
for _, part := range strings.Split(filepath.Clean(value), string(filepath.Separator)) {
|
||||
if part == ".." || part == "." || part == "" {
|
||||
return fmt.Errorf("must not escape the bundle directory: %w", domain.ErrInvalidAssessmentFamily)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validBankCoverageFamilyRecord(family BankCoverageFamilyRecord) bool {
|
||||
if _, err := domain.ParseAssessmentFamilyKey(family.Key); err != nil || family.Version < 1 {
|
||||
return false
|
||||
}
|
||||
if _, err := domain.ParseAssessmentCategory(family.Category); err != nil {
|
||||
return false
|
||||
}
|
||||
if _, err := domain.ParseAssessmentAuthor(family.Author); err != nil {
|
||||
return false
|
||||
}
|
||||
status, err := domain.ParseAssessmentReviewStatus(family.ReviewStatus)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if status == domain.AssessmentReviewPending {
|
||||
return family.Reviewer == ""
|
||||
}
|
||||
if _, err := domain.ParseAssessmentOperator(family.Reviewer); err != nil {
|
||||
return false
|
||||
}
|
||||
return family.Reviewer != family.Author
|
||||
}
|
||||
|
||||
func implementationExtension(language string) string {
|
||||
if language == "python" {
|
||||
return ".py"
|
||||
}
|
||||
return ".go"
|
||||
}
|
||||
|
||||
func sameStringSet(actual, expected []string) bool {
|
||||
if len(actual) != len(expected) {
|
||||
return false
|
||||
}
|
||||
left := slices.Clone(actual)
|
||||
right := slices.Clone(expected)
|
||||
slices.Sort(left)
|
||||
slices.Sort(right)
|
||||
return slices.Equal(left, right)
|
||||
}
|
||||
|
||||
func sumTopicCounts(topics map[string]int) int {
|
||||
total := 0
|
||||
for _, count := range topics {
|
||||
total += count
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func indexBankManifestFamilies(manifest BankCoverageManifest) map[string]bankManifestFamily {
|
||||
families := make(map[string]bankManifestFamily)
|
||||
for _, category := range manifest.Categories {
|
||||
for _, topic := range category.Topics {
|
||||
for _, key := range topic.FamilyKeys {
|
||||
families[key] = bankManifestFamily{
|
||||
category: category.Code,
|
||||
implementationsDir: category.ImplementationsDir,
|
||||
languages: slices.Clone(category.RequiredLanguages),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return families
|
||||
}
|
||||
|
||||
func newBankCoverageReport(manifest BankCoverageManifest) BankCoverageReport {
|
||||
report := BankCoverageReport{
|
||||
Categories: make([]BankCoverageCategoryReport, 0, len(manifest.Categories)),
|
||||
CalibrationMeasurementRequired: manifest.CalibrationGates.ExternalMeasurementRequired,
|
||||
UnverifiableEvidence: []string{
|
||||
"grade_coverage is not stored in the bundle format",
|
||||
"reference_seeds are not stored in the bundle format",
|
||||
"independent_review is declared by the bundle and is not authenticated here",
|
||||
"implementation files are checked for presence only; semantic parity is not tested here",
|
||||
},
|
||||
}
|
||||
for _, category := range manifest.Categories {
|
||||
requiredImplementationFiles := 0
|
||||
for _, topic := range category.Topics {
|
||||
requiredImplementationFiles += len(topic.FamilyKeys) * len(category.RequiredLanguages)
|
||||
}
|
||||
report.Categories = append(report.Categories, BankCoverageCategoryReport{
|
||||
Code: category.Code, PlannedFamilies: category.RequiredFamilyCount,
|
||||
RequiredImplementationFiles: requiredImplementationFiles,
|
||||
})
|
||||
}
|
||||
return report
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package assessment
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
)
|
||||
|
||||
const bankCoverageManifestPath = "../../../testdata/assessment/bank_manifest.json"
|
||||
|
||||
func loadBankCoverageManifest(t *testing.T) BankCoverageManifest {
|
||||
t.Helper()
|
||||
content, err := os.ReadFile(filepath.FromSlash(bankCoverageManifestPath))
|
||||
if err != nil {
|
||||
t.Fatalf("read bank coverage manifest: %v", err)
|
||||
}
|
||||
manifest, err := ParseBankCoverageManifest(content)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseBankCoverageManifest() error = %v", err)
|
||||
}
|
||||
return manifest
|
||||
}
|
||||
|
||||
func TestBankCoverageManifestMatchesThePlannedTopicCounts(t *testing.T) {
|
||||
manifest := loadBankCoverageManifest(t)
|
||||
if len(manifest.Categories) != 2 {
|
||||
t.Fatalf("manifest has %d categories, want Backend and Network", len(manifest.Categories))
|
||||
}
|
||||
if manifest.Categories[0].Code != "backend" || manifest.Categories[0].RequiredFamilyCount != 25 {
|
||||
t.Fatalf("Backend plan = %+v, want 25 families", manifest.Categories[0])
|
||||
}
|
||||
if !sameStringSet(manifest.Categories[0].RequiredLanguages, []string{"go", "python"}) {
|
||||
t.Fatalf("Backend languages = %v, want Go and Python", manifest.Categories[0].RequiredLanguages)
|
||||
}
|
||||
if manifest.Categories[1].Code != "network" || manifest.Categories[1].RequiredFamilyCount != 15 {
|
||||
t.Fatalf("Network plan = %+v, want 15 families", manifest.Categories[1])
|
||||
}
|
||||
if len(manifest.Categories[1].RequiredLanguages) != 0 {
|
||||
t.Fatalf("Network languages = %v, want no language-specific implementation requirement", manifest.Categories[1].RequiredLanguages)
|
||||
}
|
||||
if got := sumTopicCounts(requiredBankPlan["backend"]); got != 25 {
|
||||
t.Fatalf("Backend topic count = %d, want 25", got)
|
||||
}
|
||||
if got := sumTopicCounts(requiredBankPlan["network"]); got != 15 {
|
||||
t.Fatalf("Network topic count = %d, want 15", got)
|
||||
}
|
||||
if !manifest.CalibrationGates.ExternalMeasurementRequired {
|
||||
t.Fatal("calibration gates must require external measurements")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBankCoverageRejectsAnIncompleteManifest(t *testing.T) {
|
||||
manifest := loadBankCoverageManifest(t)
|
||||
manifest.Categories[0].RequiredLanguages = []string{"python"}
|
||||
content, err := json.Marshal(manifest)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal manifest: %v", err)
|
||||
}
|
||||
if _, err := ParseBankCoverageManifest(content); !errors.Is(err, domain.ErrInvalidAssessmentFamily) {
|
||||
t.Fatalf("ParseBankCoverageManifest(missing Go requirement) error = %v, want invalid manifest", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBankCoverageReportsTheExistingUnreviewedDemoInventory(t *testing.T) {
|
||||
bundle := loadDemoBundle(t)
|
||||
inventory := bankCoverageInventoryFromBundle(t, bundle)
|
||||
report, err := BuildBankCoverageReport(loadBankCoverageManifest(t), inventory)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildBankCoverageReport() error = %v", err)
|
||||
}
|
||||
if report.InventoriedFamilies != 4 || report.PendingReviewDeclarations != 4 || report.DeclaredApprovedReviews != 0 {
|
||||
t.Fatalf("demo inventory/review counts = %d/%d/%d, want 4 inventoried, 4 pending, 0 approved declarations",
|
||||
report.InventoriedFamilies, report.PendingReviewDeclarations, report.DeclaredApprovedReviews)
|
||||
}
|
||||
if len(report.UnassignedFamilyKeys) != 4 || len(report.MissingFamilyKeys) != 40 {
|
||||
t.Fatalf("demo coverage = %d unassigned and %d missing keys, want 4 and 40", len(report.UnassignedFamilyKeys), len(report.MissingFamilyKeys))
|
||||
}
|
||||
if report.Categories[0].PresentFamilies != 0 || report.Categories[0].InventoriedFamilies != 4 {
|
||||
t.Fatalf("Backend coverage = %+v, want 0 planned keys matched and 4 inventoried demo families", report.Categories[0])
|
||||
}
|
||||
if report.Categories[0].RequiredImplementationFiles != 50 || report.Categories[1].RequiredImplementationFiles != 0 {
|
||||
t.Fatalf("implementation requirements = backend %d, network %d, want 50 and 0", report.Categories[0].RequiredImplementationFiles, report.Categories[1].RequiredImplementationFiles)
|
||||
}
|
||||
if !report.CalibrationMeasurementRequired || len(report.UnverifiableEvidence) == 0 {
|
||||
t.Fatalf("report did not preserve external gates and unsupported evidence: %+v", report)
|
||||
}
|
||||
if err := report.StrictError(); err == nil {
|
||||
t.Fatal("StrictError() = nil, want incomplete inventory error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBankCoverageReportsThe24And14FamilyGaps(t *testing.T) {
|
||||
manifest := loadBankCoverageManifest(t)
|
||||
inventory := BankCoverageInventory{}
|
||||
for _, category := range manifest.Categories {
|
||||
remaining := category.RequiredFamilyCount - 1
|
||||
for _, topic := range category.Topics {
|
||||
for _, key := range topic.FamilyKeys {
|
||||
if remaining == 0 {
|
||||
break
|
||||
}
|
||||
inventory.Families = append(inventory.Families, pendingBankFamily(key, category.Code))
|
||||
remaining--
|
||||
}
|
||||
}
|
||||
}
|
||||
report, err := BuildBankCoverageReport(manifest, inventory)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildBankCoverageReport() error = %v", err)
|
||||
}
|
||||
if report.Categories[0].PresentFamilies != 24 || report.Categories[1].PresentFamilies != 14 {
|
||||
t.Fatalf("coverage = %d/%d and %d/%d, want 24/25 and 14/15",
|
||||
report.Categories[0].PresentFamilies, report.Categories[0].PlannedFamilies,
|
||||
report.Categories[1].PresentFamilies, report.Categories[1].PlannedFamilies)
|
||||
}
|
||||
if len(report.MissingFamilyKeys) != 2 {
|
||||
t.Fatalf("missing family keys = %v, want one gap per category", report.MissingFamilyKeys)
|
||||
}
|
||||
if len(report.MissingIndependentReviews) != 38 {
|
||||
t.Fatalf("missing review declarations = %d, want 38 present families with pending review", len(report.MissingIndependentReviews))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBankCoverageNamesMissingGoVariantAndReviewer(t *testing.T) {
|
||||
manifest := loadBankCoverageManifest(t)
|
||||
key := manifest.Categories[0].Topics[0].FamilyKeys[0]
|
||||
inventory := BankCoverageInventory{
|
||||
Families: []BankCoverageFamilyRecord{pendingBankFamily(key, "backend")},
|
||||
ImplementationFiles: []string{filepath.ToSlash(filepath.Join("solutions", key+".py"))},
|
||||
}
|
||||
report, err := BuildBankCoverageReport(manifest, inventory)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildBankCoverageReport() error = %v", err)
|
||||
}
|
||||
goPath := filepath.ToSlash(filepath.Join("solutions", key+".go"))
|
||||
pythonPath := filepath.ToSlash(filepath.Join("solutions", key+".py"))
|
||||
if !slices.Contains(report.MissingImplementationFiles, goPath) {
|
||||
t.Fatalf("missing implementations = %v, want %s", report.MissingImplementationFiles, goPath)
|
||||
}
|
||||
if slices.Contains(report.MissingImplementationFiles, pythonPath) {
|
||||
t.Fatalf("missing implementations = %v, Python path %s is present", report.MissingImplementationFiles, pythonPath)
|
||||
}
|
||||
if !slices.Contains(report.MissingIndependentReviews, key) {
|
||||
t.Fatalf("missing review declarations = %v, want %s", report.MissingIndependentReviews, key)
|
||||
}
|
||||
if err := report.StrictError(); err == nil {
|
||||
t.Fatal("StrictError() = nil, want missing variant and reviewer declaration to fail strict checks")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBankCoverageReportsFamiliesFromUnplannedCategories(t *testing.T) {
|
||||
family := pendingBankFamily("other-family", "security")
|
||||
report, err := BuildBankCoverageReport(loadBankCoverageManifest(t), BankCoverageInventory{
|
||||
Families: []BankCoverageFamilyRecord{family},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("BuildBankCoverageReport() error = %v", err)
|
||||
}
|
||||
if report.InventoriedFamilies != 1 || !slices.Contains(report.UnassignedCategories, "security") ||
|
||||
!slices.Contains(report.UnassignedFamilyKeys, "other-family") {
|
||||
t.Fatalf("unplanned category inventory = %+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func bankCoverageInventoryFromBundle(t *testing.T, bundle Bundle) BankCoverageInventory {
|
||||
t.Helper()
|
||||
drafts, err := bundle.Drafts()
|
||||
if err != nil {
|
||||
t.Fatalf("Bundle.Drafts() error = %v", err)
|
||||
}
|
||||
inventory := BankCoverageInventory{}
|
||||
for _, draft := range drafts {
|
||||
core := draft.Core()
|
||||
inventory.Families = append(inventory.Families, BankCoverageFamilyRecord{
|
||||
Key: string(core.Key), Category: string(core.Category), Version: core.Version,
|
||||
Author: string(core.Author), Reviewer: string(core.Reviewer),
|
||||
ReviewStatus: string(core.ReviewStatus), Source: "demo bundle",
|
||||
})
|
||||
}
|
||||
return inventory
|
||||
}
|
||||
|
||||
func pendingBankFamily(key, category string) BankCoverageFamilyRecord {
|
||||
return BankCoverageFamilyRecord{
|
||||
Key: key, Category: category, Version: 1,
|
||||
Author: "operator:author", ReviewStatus: string(domain.AssessmentReviewPending),
|
||||
Source: "unit bundle",
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user