|
| 1 | +package clientvalidators |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +// displayRunnerRel is the node harness that exercises the client-only |
| 15 | +// altitude display + locale helpers in app.js (see run_js_display.js). |
| 16 | +const displayRunnerRel = "testdata/run_js_display.js" |
| 17 | + |
| 18 | +type localeCase struct { |
| 19 | + Languages []string |
| 20 | + Imperial bool |
| 21 | +} |
| 22 | + |
| 23 | +type altDisplayCase struct { |
| 24 | + Metres string |
| 25 | + Imperial bool |
| 26 | + Output string |
| 27 | +} |
| 28 | + |
| 29 | +// runJSDisplay invokes the display harness with the given cases and returns |
| 30 | +// the JS results keyed for lookup. Mirrors runJSValidators' node-subprocess |
| 31 | +// shape; kept separate so the validator-parity flow stays untouched. |
| 32 | +func runJSDisplay(t *testing.T, locales []localeCase, alts []altDisplayCase) (map[string]bool, map[string]string) { |
| 33 | + t.Helper() |
| 34 | + |
| 35 | + type reqLocale struct { |
| 36 | + Languages []string `json:"languages"` |
| 37 | + } |
| 38 | + type reqAlt struct { |
| 39 | + Metres string `json:"metres"` |
| 40 | + Imperial bool `json:"imperial"` |
| 41 | + } |
| 42 | + req := struct { |
| 43 | + LocaleCases []reqLocale `json:"localeCases"` |
| 44 | + AltDisplayCases []reqAlt `json:"altDisplayCases"` |
| 45 | + }{} |
| 46 | + for _, c := range locales { |
| 47 | + req.LocaleCases = append(req.LocaleCases, reqLocale{Languages: c.Languages}) |
| 48 | + } |
| 49 | + for _, c := range alts { |
| 50 | + req.AltDisplayCases = append(req.AltDisplayCases, reqAlt{Metres: c.Metres, Imperial: c.Imperial}) |
| 51 | + } |
| 52 | + body, err := json.Marshal(req) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("marshal display request: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + appJSPath, err := filepath.Abs(appJSRel) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("abs app.js: %v", err) |
| 60 | + } |
| 61 | + runnerPath, err := filepath.Abs(displayRunnerRel) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("abs runner: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 67 | + defer cancel() |
| 68 | + cmd := exec.CommandContext(ctx, "node", runnerPath, appJSPath) |
| 69 | + cmd.Stdin = strings.NewReader(string(body)) |
| 70 | + var stdout, stderr strings.Builder |
| 71 | + cmd.Stdout = &stdout |
| 72 | + cmd.Stderr = &stderr |
| 73 | + if err := cmd.Run(); err != nil { |
| 74 | + t.Fatalf("node display runner failed: %v; stderr: %s", err, stderr.String()) |
| 75 | + } |
| 76 | + |
| 77 | + var resp struct { |
| 78 | + LocaleResults []struct { |
| 79 | + Languages []string `json:"languages"` |
| 80 | + Imperial bool `json:"imperial"` |
| 81 | + } `json:"localeResults"` |
| 82 | + AltDisplayResults []struct { |
| 83 | + Metres string `json:"metres"` |
| 84 | + Imperial bool `json:"imperial"` |
| 85 | + Output string `json:"output"` |
| 86 | + } `json:"altDisplayResults"` |
| 87 | + Errors []string `json:"errors"` |
| 88 | + } |
| 89 | + if err := json.Unmarshal([]byte(stdout.String()), &resp); err != nil { |
| 90 | + t.Fatalf("decode node output: %v; raw: %q", err, stdout.String()) |
| 91 | + } |
| 92 | + if len(resp.Errors) > 0 { |
| 93 | + t.Fatalf("display runner reported errors: %v", resp.Errors) |
| 94 | + } |
| 95 | + |
| 96 | + localeOut := make(map[string]bool, len(resp.LocaleResults)) |
| 97 | + for _, r := range resp.LocaleResults { |
| 98 | + localeOut[strings.Join(r.Languages, ",")] = r.Imperial |
| 99 | + } |
| 100 | + altOut := make(map[string]string, len(resp.AltDisplayResults)) |
| 101 | + for _, r := range resp.AltDisplayResults { |
| 102 | + altOut[altKey(r.Metres, r.Imperial)] = r.Output |
| 103 | + } |
| 104 | + return localeOut, altOut |
| 105 | +} |
| 106 | + |
| 107 | +func altKey(metres string, imperial bool) string { |
| 108 | + return fmt.Sprintf("%s|%t", metres, imperial) |
| 109 | +} |
| 110 | + |
| 111 | +// TestImperialLengthFromLanguages pins the *-us region rule the altitude |
| 112 | +// field uses to pick its display unit, mirroring the server-side |
| 113 | +// tempUnitFromAcceptLanguage contract. |
| 114 | +func TestImperialLengthFromLanguages(t *testing.T) { |
| 115 | + cases := []localeCase{ |
| 116 | + {Languages: []string{"en-US", "en"}, Imperial: true}, |
| 117 | + {Languages: []string{"es-US"}, Imperial: true}, |
| 118 | + {Languages: []string{"en-GB", "en"}, Imperial: false}, |
| 119 | + {Languages: []string{"de-DE"}, Imperial: false}, |
| 120 | + {Languages: []string{"en", "en-US"}, Imperial: true}, // bare tag skipped, then -us wins |
| 121 | + {Languages: []string{"en", "de-DE"}, Imperial: false}, // bare skipped, first regional is metric |
| 122 | + {Languages: []string{"en"}, Imperial: false}, // bare only → default metric |
| 123 | + {Languages: []string{"*"}, Imperial: false}, // wildcard ignored → default metric |
| 124 | + {Languages: []string{}, Imperial: false}, // empty → default metric |
| 125 | + {Languages: []string{"fr-FR", "en-US"}, Imperial: false}, // first regional (metric) wins |
| 126 | + } |
| 127 | + gotLocale, _ := runJSDisplay(t, cases, nil) |
| 128 | + for _, c := range cases { |
| 129 | + key := strings.Join(c.Languages, ",") |
| 130 | + if got := gotLocale[key]; got != c.Imperial { |
| 131 | + t.Errorf("imperialLengthFromLanguages(%v) = %t, want %t", c.Languages, got, c.Imperial) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// TestAltitudeDisplayValue pins how a canonical bare-metres value renders in |
| 137 | +// the altitude field: metric always "<m>m"; imperial shows "<ft>ft" only |
| 138 | +// when it round-trips exactly back to the stored metres, otherwise "<m>m". |
| 139 | +func TestAltitudeDisplayValue(t *testing.T) { |
| 140 | + cases := []altDisplayCase{ |
| 141 | + // Metric viewer: always metres, unit suffixed. |
| 142 | + {Metres: "3.6576", Imperial: false, Output: "3.6576m"}, |
| 143 | + {Metres: "20", Imperial: false, Output: "20m"}, |
| 144 | + {Metres: "", Imperial: false, Output: ""}, |
| 145 | + // Imperial viewer, exact whole-feet conversions → feet. |
| 146 | + {Metres: "3.6576", Imperial: true, Output: "12ft"}, // 12 ft |
| 147 | + {Metres: "19.812", Imperial: true, Output: "65ft"}, // 65 ft |
| 148 | + {Metres: "121.92", Imperial: true, Output: "400ft"}, // 400 ft |
| 149 | + {Metres: "0", Imperial: true, Output: "0ft"}, |
| 150 | + // Imperial viewer, value not a whole number of feet → stays metres. |
| 151 | + {Metres: "20", Imperial: true, Output: "20m"}, // 65.6168 ft, not exact |
| 152 | + {Metres: "42.5", Imperial: true, Output: "42.5m"}, |
| 153 | + // Empty passthrough regardless of locale. |
| 154 | + {Metres: "", Imperial: true, Output: ""}, |
| 155 | + } |
| 156 | + _, gotAlt := runJSDisplay(t, nil, cases) |
| 157 | + for _, c := range cases { |
| 158 | + if got := gotAlt[altKey(c.Metres, c.Imperial)]; got != c.Output { |
| 159 | + t.Errorf("altitudeDisplayValue(%q, %t) = %q, want %q", c.Metres, c.Imperial, got, c.Output) |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments