diff --git a/cmd/nextpath/internal/opportunity/store.go b/cmd/nextpath/internal/opportunity/store.go new file mode 100644 index 0000000..7f39c77 --- /dev/null +++ b/cmd/nextpath/internal/opportunity/store.go @@ -0,0 +1,37 @@ +package opportunity + +import ( + "encoding/json" + "fmt" + "os" +) + +type Snapshot struct { + Version string `json:"version"` + GeneratedAt string `json:"generated_at"` + Occupations map[string]Evidence `json:"occupations"` +} + +// Load reads the latest complete snapshot. A missing cache is an expected +// state: NextPath can still provide interest matches without inventing labor +// evidence. +func Load(path string) (Snapshot, error) { + if path == "" { + return Snapshot{}, nil + } + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return Snapshot{}, nil + } + if err != nil { + return Snapshot{}, fmt.Errorf("read opportunity cache: %w", err) + } + var snapshot Snapshot + if err := json.Unmarshal(data, &snapshot); err != nil { + return Snapshot{}, fmt.Errorf("decode opportunity cache: %w", err) + } + if snapshot.Version == "" || len(snapshot.Occupations) == 0 { + return Snapshot{}, fmt.Errorf("opportunity cache is empty") + } + return snapshot, nil +} diff --git a/cmd/nextpath/internal/opportunity/store_test.go b/cmd/nextpath/internal/opportunity/store_test.go new file mode 100644 index 0000000..c16d0f3 --- /dev/null +++ b/cmd/nextpath/internal/opportunity/store_test.go @@ -0,0 +1,24 @@ +package opportunity + +import ( + "os" + "path/filepath" + "testing" +) + +func TestLoadMissingSnapshotIsAvailableState(t *testing.T) { + snapshot, err := Load(filepath.Join(t.TempDir(), "missing.json")) + if err != nil || len(snapshot.Occupations) != 0 { + t.Fatalf("Load missing = %#v, %v", snapshot, err) + } +} + +func TestLoadRejectsMalformedSnapshot(t *testing.T) { + path := filepath.Join(t.TempDir(), "opportunity.json") + if err := os.WriteFile(path, []byte(`{"version":"v1","occupations":{}}`), 0o600); err != nil { + t.Fatal(err) + } + if _, err := Load(path); err == nil { + t.Fatal("Load accepted empty snapshot") + } +} diff --git a/cmd/nextpath/internal/web/server.go b/cmd/nextpath/internal/web/server.go index 0fea2a1..68548f7 100644 --- a/cmd/nextpath/internal/web/server.go +++ b/cmd/nextpath/internal/web/server.go @@ -15,6 +15,7 @@ import ( "my-homelab-configs/cmd/nextpath/internal/account" "my-homelab-configs/cmd/nextpath/internal/assessment" "my-homelab-configs/cmd/nextpath/internal/career" + "my-homelab-configs/cmd/nextpath/internal/opportunity" ) const ( @@ -24,9 +25,15 @@ const ( ) type Config struct { - SecureCookies bool - SessionTTL time.Duration - Logger *slog.Logger + SecureCookies bool + SessionTTL time.Duration + Logger *slog.Logger + OpportunityStorePath string +} + +type careerResult struct { + career.Match + Outlook *opportunity.Outlook } type session struct { @@ -216,11 +223,28 @@ func (server *Server) results(writer http.ResponseWriter, request *http.Request) return } matches := career.Rank(result, catalog, 5) + snapshot, err := opportunity.Load(server.config.OpportunityStorePath) + if err != nil { + server.config.Logger.Warn("load NextPath opportunity evidence", "error", err) + } + careerResults := make([]careerResult, 0, len(matches)) + for _, match := range matches { + view := careerResult{Match: match} + if evidence, exists := snapshot.Occupations[match.Occupation.ONETSOC]; exists { + if outlook, calculateErr := opportunity.Calculate(evidence); calculateErr == nil { + view.Outlook = &outlook + } else { + server.config.Logger.Warn("calculate NextPath employability outlook", "occupation", match.Occupation.ONETSOC, "error", calculateErr) + } + } + careerResults = append(careerResults, view) + } server.mu.Lock() csrf := server.sessions[token].CSRFToken server.mu.Unlock() server.render(writer, http.StatusOK, "Your interest profile", resultsPage, map[string]any{ - "Name": user.DisplayName, "Result": result, "Matches": matches, "CSRF": csrf, + "Name": user.DisplayName, "Result": result, "Matches": careerResults, "CSRF": csrf, + "OpportunityGeneratedAt": snapshot.GeneratedAt, }) } diff --git a/cmd/nextpath/internal/web/server_test.go b/cmd/nextpath/internal/web/server_test.go index 89847c6..d7b05e3 100644 --- a/cmd/nextpath/internal/web/server_test.go +++ b/cmd/nextpath/internal/web/server_test.go @@ -6,6 +6,7 @@ import ( "net/http/cookiejar" "net/http/httptest" "net/url" + "os" "path/filepath" "regexp" "strings" @@ -22,7 +23,17 @@ func TestCompletePrivateAssessmentFlow(t *testing.T) { if err != nil { t.Fatal(err) } - testServer := httptest.NewServer(New(accounts, Config{}).Handler()) + opportunityPath := filepath.Join(t.TempDir(), "opportunity.json") + if err := os.WriteFile(opportunityPath, []byte(`{ + "version":"v1","generated_at":"2026-12-05T12:00:00Z","occupations":{"13-2011.00":{ + "arizona_annual_graduates":100,"arizona_projected_annual_openings":100,"active_entry_level_postings":50, + "graduate_academic_year":"latest","projection_period":"2022-2032","postings_observed_at":"2026-12-05T12:00:00Z", + "geography":"Arizona","soc_to_cip_mapping_coverage":0.9, + "graduate_source_url":"https://example.test/graduates","projection_source_url":"https://example.test/projections","posting_source_url":"https://example.test/jobs" + }}}`), 0o600); err != nil { + t.Fatal(err) + } + testServer := httptest.NewServer(New(accounts, Config{OpportunityStorePath: opportunityPath}).Handler()) defer testServer.Close() jar, _ := cookiejar.New(nil) client := &http.Client{Jar: jar, CheckRedirect: func(_ *http.Request, _ []*http.Request) error { return http.ErrUseLastResponse }} @@ -56,7 +67,7 @@ func TestCompletePrivateAssessmentFlow(t *testing.T) { response = get(t, client, testServer.URL+"/results") body := readBody(t, response) - if response.StatusCode != http.StatusOK || !strings.Contains(body, "interest profile") || !strings.Contains(body, "not ability, identity, or employability") || !strings.Contains(body, "Careers to explore next") { + if response.StatusCode != http.StatusOK || !strings.Contains(body, "interest profile") || !strings.Contains(body, "not ability or identity") || !strings.Contains(body, "Careers to explore next") || !strings.Contains(body, "Arizona employability outlook: 88%") { t.Fatalf("unexpected results response %d: %s", response.StatusCode, body) } response = get(t, client, testServer.URL+"/account/export") diff --git a/cmd/nextpath/internal/web/templates.go b/cmd/nextpath/internal/web/templates.go index dc88b9b..dfabc2a 100644 --- a/cmd/nextpath/internal/web/templates.go +++ b/cmd/nextpath/internal/web/templates.go @@ -24,4 +24,4 @@ const loginPage = `{{define "content"}}

Continue exploring.

What sounds interesting, {{.Name}}?

Rate the activity itself—not whether you are already good at it.

{{.Answered}} of {{.Total}} answered

{{.Question.Prompt}}

{{range $value := seq}}{{end}}
Not interestedUnsureVery interested
{{end}}` -const resultsPage = `{{define "content"}}

Your current interest profile

Hello {{.Name}}. Your three-letter exploration code is {{.Result.Code}}.

{{range .Result.Scores}}
{{.Dimension}}
{{.Percent}}%
{{end}}

Careers to explore next

These are personal-interest matches, not employability rankings. Arizona market and education evidence will be scored separately.

{{range .Matches}}
O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%

{{.Occupation.Title}}

{{.Occupation.Summary}}

{{.Reason}}

Try it first: {{.Occupation.TryFirst}}

Review the O*NET occupation profile
{{end}}

How to use this

{{.Result.InstrumentNotice}}

Confidence: {{.Result.Confidence}}. This describes interests, not ability, identity, or employability.

Export my data

Delete account

{{end}}` +const resultsPage = `{{define "content"}}

Your current interest profile

Hello {{.Name}}. Your three-letter exploration code is {{.Result.Code}}.

{{range .Result.Scores}}
{{.Dimension}}
{{.Percent}}%
{{end}}

Careers to explore next

Interest fit and Arizona market outlook are separate signals. The market percentage is a rough demand-to-completions comparison, not your probability of being hired.

{{range .Matches}}
O*NET-SOC {{.Occupation.ONETSOC}} · interest fit {{.FitScore}}%

{{.Occupation.Title}}

{{.Occupation.Summary}}

{{.Reason}}

{{if .Outlook}}
Arizona employability outlook: {{.Outlook.EmployabilityOutlookPercent}}%

{{.Outlook.Label}} · {{.Outlook.Confidence}} evidence confidence

{{.Outlook.Evidence.ArizonaProjectedOpenings}} projected openings/year and {{.Outlook.Evidence.ActiveEntryLevelPostings}} postings observed, compared with {{.Outlook.Evidence.ArizonaAnnualGraduates}} recent Arizona completions. Demand-to-completions ratio: {{.Outlook.AnnualDemandToGraduateRatio}}.

Formula: 75% projected annual demand coverage + 25% current posting coverage. Posting snapshot {{.Outlook.Evidence.PostingsObservedAt.Format "Jan 2, 2006"}}.

{{else}}

Arizona employability outlook unavailable. Complete source evidence has not been refreshed for this occupation; no percentage is estimated.

{{end}}

Try it first: {{.Occupation.TryFirst}}

Review the O*NET occupation profile
{{end}}

How to use this

{{.Result.InstrumentNotice}}

Confidence: {{.Result.Confidence}}. This describes interests, not ability or identity. Market outlook is contextual evidence, not a hiring promise.

Export my data

Delete account

{{end}}` diff --git a/cmd/nextpath/main.go b/cmd/nextpath/main.go index 088b0b7..d6b1726 100644 --- a/cmd/nextpath/main.go +++ b/cmd/nextpath/main.go @@ -21,8 +21,9 @@ func main() { os.Exit(1) } server := web.New(accounts, web.Config{ - SecureCookies: strings.EqualFold(os.Getenv("NEXTPATH_SECURE_COOKIES"), "true"), - SessionTTL: 12 * time.Hour, + SecureCookies: strings.EqualFold(os.Getenv("NEXTPATH_SECURE_COOKIES"), "true"), + SessionTTL: 12 * time.Hour, + OpportunityStorePath: envOr("NEXTPATH_OPPORTUNITY_STORE", "./data/opportunity.json"), }) slog.Info("NextPath listening", "address", address) if err := http.ListenAndServe(address, server.Handler()); err != nil && !errors.Is(err, http.ErrServerClosed) {