| 📦 Turbo JS 91999d1 k33g 12h ago | 1 | package jslang |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "rickub.com/turbo-editors/turbo-core/settings" |
| 10 | "rickub.com/turbo-editors/turbo-core/snippets" |
| 11 | "rickub.com/turbo-editors/turbo-core/syntax" |
| 12 | "rickub.com/turbo-editors/turbo-core/tools" |
| 13 | ) |
| 14 | |
| 15 | // The starter files Turbo JS writes are the one part of a project's .turbo-js |
| 16 | // directory that is about JavaScript and Node, so this is where what is *in* |
| 17 | // them is checked. That the file written is the profile's template at all is |
| 18 | // turbo-core's test. |
| 19 | |
| 20 | // noUserSnippets points the user's own snippets at an empty directory, so a |
| 21 | // test never reads whoever is running it. |
| 22 | func noUserSnippets(t *testing.T) { |
| 23 | t.Helper() |
| 24 | t.Setenv(Profile().SnippetDirEnvVar(), t.TempDir()) |
| 25 | } |
| 26 | |
| 27 | // loadTools reads a project's tools, failing the test if it cannot. |
| 28 | func loadTools(t *testing.T, dir string) tools.List { |
| 29 | t.Helper() |
| 30 | |
| 31 | list, err := tools.Load(Profile(), dir) |
| 32 | if err != nil { |
| 33 | t.Fatalf("tools.Load(%q) error = %v", dir, err) |
| 34 | } |
| 35 | return list |
| 36 | } |
| 37 | |
| 38 | // loadSnippets reads a project's snippets, failing the test if it cannot. |
| 39 | func loadSnippets(t *testing.T, dir string) snippets.List { |
| 40 | t.Helper() |
| 41 | |
| 42 | list, err := snippets.Load(Profile(), dir) |
| 43 | if err != nil { |
| 44 | t.Fatalf("snippets.Load(%q) error = %v", dir, err) |
| 45 | } |
| 46 | return list |
| 47 | } |
| 48 | |
| 49 | // createTools writes the starter tools file into a fresh project. |
| 50 | func createTools(t *testing.T) string { |
| 51 | t.Helper() |
| 52 | |
| 53 | dir := t.TempDir() |
| 54 | if _, err := tools.Create(Profile(), dir); err != nil { |
| 55 | t.Fatalf("tools.Create() error = %v", err) |
| 56 | } |
| 57 | return dir |
| 58 | } |
| 59 | |
| 60 | // createSnippets writes the starter snippets file into a fresh project. |
| 61 | func createSnippets(t *testing.T) string { |
| 62 | t.Helper() |
| 63 | |
| 64 | noUserSnippets(t) |
| 65 | dir := t.TempDir() |
| 66 | if _, err := snippets.Create(Profile(), dir); err != nil { |
| 67 | t.Fatalf("snippets.Create() error = %v", err) |
| 68 | } |
| 69 | return dir |
| 70 | } |
| 71 | |
| 72 | // readFile returns a file's contents. |
| 73 | func readFile(t *testing.T, path string) string { |
| 74 | t.Helper() |
| 75 | |
| 76 | data, err := os.ReadFile(path) |
| 77 | if err != nil { |
| 78 | t.Fatalf("reading %s: %v", path, err) |
| 79 | } |
| 80 | return string(data) |
| 81 | } |
| 82 | |
| 83 | // plain strips the tilde hot-key markers from a label. |
| 84 | func plain(label string) string { return strings.ReplaceAll(label, "~", "") } |
| 85 | |
| 86 | // hotKey returns the character between the tildes, or 0 when there is none. |
| 87 | func hotKey(label string) rune { |
| 88 | first := strings.IndexByte(label, '~') |
| 89 | if first < 0 || first+1 >= len(label) { |
| 90 | return 0 |
| 91 | } |
| 92 | return rune(label[first+1]) |
| 93 | } |
| 94 | |
| 95 | // starterTools are the commands the starter file offers, by the plain name of |
| 96 | // each. Every one of them was run against Node 24 and npm 11 before it was |
| 97 | // written here. |
| 98 | var starterTools = map[string]string{ |
| 99 | "Install": "npm install", |
| 100 | "Format": "npx prettier --write .", |
| 101 | "Lint": "npx eslint .", |
| 102 | "Test": "node --test", |
| 103 | "Run": "node {{script, e.g. main.js}}", |
| 104 | "Start": "npm start", |
| 105 | "Echo": "echo 🎉 tada!", |
| 106 | } |
| 107 | |
| 108 | func TestTheCreatedToolsFileHoldsTheNodeCommandsAndTheExampleBesideThem(t *testing.T) { |
| 109 | // These are what a Node project runs before it commits, and they are the |
| 110 | // reason the file exists at all. |
| 111 | dir := createTools(t) |
| 112 | |
| 113 | byName := map[string]string{} |
| 114 | for _, tool := range loadTools(t, dir).Tools() { |
| 115 | byName[plain(tool.Name)] = tool.Command |
| 116 | } |
| 117 | |
| 118 | for name, command := range starterTools { |
| 119 | if got := byName[name]; got != command { |
| 120 | t.Errorf("%s runs %q, want %q", name, got, command) |
| 121 | } |
| 122 | } |
| 123 | for name := range byName { |
| 124 | if _, ok := starterTools[name]; !ok { |
| 125 | t.Errorf("the created file holds a tool this test does not know about: %q", name) |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestTheCreatedToolsCarryDistinctHotKeys(t *testing.T) { |
| 131 | // Seven items in two menus are worth reaching with one keystroke each. |
| 132 | dir := createTools(t) |
| 133 | |
| 134 | seen := map[rune]string{} |
| 135 | for _, tool := range loadTools(t, dir).Tools() { |
| 136 | key := hotKey(tool.Name) |
| 137 | if key == 0 { |
| 138 | t.Errorf("%q has no hot key", tool.Name) |
| 139 | continue |
| 140 | } |
| 141 | if other, clash := seen[key]; clash { |
| 142 | t.Errorf("%q and %q both answer to %c", other, tool.Name, key) |
| 143 | } |
| 144 | seen[key] = tool.Name |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestEachToolGoesWhereItsOwnOutputBelongs(t *testing.T) { |
| 149 | // `node` runs a program that may read the keyboard, and `npm start` |
| 150 | // usually runs a server that has to be interruptible; a popup can do |
| 151 | // neither. Echo is a terminal too, as the worked example of a tool in a |
| 152 | // menu of its own. The rest say something short and are read once. |
| 153 | dir := createTools(t) |
| 154 | |
| 155 | want := map[string]tools.Output{ |
| 156 | "Install": tools.OutputPopup, |
| 157 | "Format": tools.OutputPopup, |
| 158 | "Lint": tools.OutputPopup, |
| 159 | "Test": tools.OutputPopup, |
| 160 | "Run": tools.OutputTerminal, |
| 161 | "Start": tools.OutputTerminal, |
| 162 | "Echo": tools.OutputTerminal, |
| 163 | } |
| 164 | for _, tool := range loadTools(t, dir).Tools() { |
| 165 | name := plain(tool.Name) |
| 166 | if tool.Output == "" { |
| 167 | t.Errorf("%q leaves its output to the default rather than saying it", tool.Name) |
| 168 | } |
| 169 | if got := tool.Where(); got != want[name] { |
| 170 | t.Errorf("%s goes to %q, want %q", name, got, want[name]) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestTheCreatedToolsFileExplainsItself(t *testing.T) { |
| 176 | contents := readFile(t, tools.Path(Profile(), createTools(t))) |
| 177 | |
| 178 | for _, want := range []string{"[[tool]]", "sh -c", "hot key", "popup", "terminal", "editor", "package.json"} { |
| 179 | if !strings.Contains(contents, want) { |
| 180 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func TestTheCreatedToolsFileNamesTheJavaScriptMenuAndNoOtherEditors(t *testing.T) { |
| 186 | // The comments explain which menu a tool lands in by naming it, and naming |
| 187 | // the wrong editor's menu is the copy-and-paste mistake this catches. |
| 188 | contents := readFile(t, tools.Path(Profile(), createTools(t))) |
| 189 | |
| 190 | if !strings.Contains(contents, "JavaScript menu") { |
| 191 | t.Errorf("the created file never names the JavaScript menu:\n%s", contents) |
| 192 | } |
| 193 | for _, other := range []string{"Rust menu", "Go menu", "Python menu", "Golo menu", "MoonBit menu", "cargo", "uv "} { |
| 194 | if strings.Contains(contents, other) { |
| 195 | t.Errorf("the created file still says %q:\n%s", other, contents) |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | func TestTheCreatedToolsFileShowsHowToUseAnotherMenu(t *testing.T) { |
| 201 | contents := readFile(t, tools.Path(Profile(), createTools(t))) |
| 202 | |
| 203 | for _, want := range []string{"menu says which menu", `menu = "Tools"`} { |
| 204 | if !strings.Contains(contents, want) { |
| 205 | t.Errorf("the created file never shows %q:\n%s", want, contents) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | func TestTheCreatedToolsFileExplainsHowToAskForAValue(t *testing.T) { |
| 211 | // A parameterised tool is only discoverable if the file people get says the |
| 212 | // syntax exists. The double-brace warning is here too, because somebody |
| 213 | // reading this file may well have an awk one-liner in mind. |
| 214 | contents := readFile(t, tools.Path(Profile(), createTools(t))) |
| 215 | |
| 216 | for _, want := range []string{ |
| 217 | "{{label}}", |
| 218 | "npm install {{package name}}", |
| 219 | "{{extra flags...}}", |
| 220 | "Double braces, not single", |
| 221 | } { |
| 222 | if !strings.Contains(contents, want) { |
| 223 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | func TestOnlyRunAsksForAValueAndItAsksForTheScript(t *testing.T) { |
| 229 | // The examples live in comments, so none of them may become a real tool. |
| 230 | // Run is the one real placeholder: a Node project has no single entry |
| 231 | // point the editor could know, so it has to ask which script to run. |
| 232 | for _, tool := range loadTools(t, createTools(t)).Tools() { |
| 233 | got := tool.Placeholders() |
| 234 | if plain(tool.Name) != "Run" { |
| 235 | if got != nil { |
| 236 | t.Errorf("%q asks for %v; only Run takes a value", tool.Name, got) |
| 237 | } |
| 238 | continue |
| 239 | } |
| 240 | if len(got) != 1 || !strings.HasPrefix(got[0].Label, "script") { |
| 241 | t.Errorf("Run asks for %v, want exactly one value, the script", got) |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func TestTheCreatedSnippetsFileHoldsUsableJavaScriptAndJSONSnippets(t *testing.T) { |
| 247 | dir := createSnippets(t) |
| 248 | |
| 249 | for _, language := range []syntax.Language{Language, LanguageJSON} { |
| 250 | groups := loadSnippets(t, dir).Groups(string(language)) |
| 251 | if len(groups) == 0 { |
| 252 | t.Errorf("the created file offers nothing at all in a %s file", language) |
| 253 | } |
| 254 | for _, group := range groups { |
| 255 | for _, snippet := range group.Snippets { |
| 256 | if snippet.Name == "" || snippet.Body == "" { |
| 257 | t.Errorf("the created file holds an unusable snippet %+v", snippet) |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | func TestTheCreatedSnippetsIndentWithTwoSpacesTheWayPrettierDoes(t *testing.T) { |
| 265 | // Prettier writes two spaces, and it is what `npx prettier --write` — the |
| 266 | // Format tool — enforces. A tab, or four spaces, that crept in would land |
| 267 | // in somebody's file and be reformatted out on the next run, which is a |
| 268 | // diff nobody asked for. |
| 269 | for _, group := range loadSnippets(t, createSnippets(t)).Groups(string(Language)) { |
| 270 | for _, snippet := range group.Snippets { |
| 271 | if strings.Contains(snippet.Body, "\t") { |
| 272 | t.Errorf("%q indents with a tab:\n%q", snippet.Name, snippet.Body) |
| 273 | } |
| 274 | for _, line := range strings.Split(snippet.Body, "\n") { |
| 275 | indent := len(line) - len(strings.TrimLeft(line, " ")) |
| 276 | if indent%2 != 0 { |
| 277 | t.Errorf("%q has a line indented by %d spaces, want a multiple of two:\n%q", snippet.Name, indent, line) |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestTheCreatedSnippetsFileExplainsItself(t *testing.T) { |
| 285 | contents := readFile(t, snippets.ProjectPath(Profile(), createSnippets(t))) |
| 286 | |
| 287 | for _, want := range []string{"[[snippet]]", "languages", "group", "General", "Prettier", snippets.UserPath(Profile())} { |
| 288 | if !strings.Contains(contents, want) { |
| 289 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | func TestTheCreatedSnippetsFileListsEveryLanguageTheEditorKnows(t *testing.T) { |
| 295 | // The comment is where a user finds out what they may write in a languages |
| 296 | // key. One that omits a language the editor colours sends them looking for |
| 297 | // a feature that is already there. |
| 298 | Register() |
| 299 | contents := readFile(t, snippets.ProjectPath(Profile(), createSnippets(t))) |
| 300 | |
| 301 | // The header comment, before the first snippet: a name that only appears |
| 302 | // in a snippet's own languages key is not a list anyone reads. |
| 303 | // The comment itself mentions "[[snippet]]", so the cut is at the first |
| 304 | // one that begins a line. |
| 305 | header, _, _ := strings.Cut(contents, "\n[[snippet]]") |
| 306 | for _, language := range syntax.Registered() { |
| 307 | if !strings.Contains(header, string(language)) { |
| 308 | t.Errorf("the created file's header never mentions the %q language:\n%s", language, header) |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | func TestTheCreatedSettingsFileExplainsItself(t *testing.T) { |
| 314 | project := t.TempDir() |
| 315 | if _, err := settings.Create(Profile(), project, "turbo-classic"); err != nil { |
| 316 | t.Fatalf("settings.Create() error = %v", err) |
| 317 | } |
| 318 | |
| 319 | contents := readFile(t, settings.Path(Profile(), project)) |
| 320 | for _, want := range []string{"theme", "autosave", "autosave_delay", "-list-themes"} { |
| 321 | if !strings.Contains(contents, want) { |
| 322 | t.Errorf("the created file never mentions %q:\n%s", want, contents) |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | func TestTheCreatedSettingsFileTurnsAutosaveOn(t *testing.T) { |
| 328 | // A project that has gone to the trouble of creating a settings file has |
| 329 | // said what it wants. The file is the visible, editable place to say |
| 330 | // otherwise, which is why the default lives here and not in the library. |
| 331 | project := t.TempDir() |
| 332 | if _, err := settings.Create(Profile(), project, "turbo-classic"); err != nil { |
| 333 | t.Fatalf("settings.Create() error = %v", err) |
| 334 | } |
| 335 | |
| 336 | loaded, err := settings.Load(Profile(), project) |
| 337 | if err != nil { |
| 338 | t.Fatalf("settings.Load() error = %v", err) |
| 339 | } |
| 340 | if !loaded.Autosave { |
| 341 | t.Errorf("the created settings file leaves autosave off:\n%s", readFile(t, settings.Path(Profile(), project))) |
| 342 | } |
| 343 | if loaded.AutosaveDelay != settings.DefaultAutosaveDelay { |
| 344 | t.Errorf("AutosaveDelay = %v, want the library default %v", loaded.AutosaveDelay, settings.DefaultAutosaveDelay) |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | func TestAProjectWithNoSettingsFileStillDoesNotAutosave(t *testing.T) { |
| 349 | // The other half of the decision. Turning autosave on for a project that |
| 350 | // never opted in would mean the editor writing to disk in any directory it |
| 351 | // is started in, which is a different and much larger claim. |
| 352 | if settings.Default().Autosave { |
| 353 | t.Error("settings.Default() autosaves; a project with no settings file never opted in") |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // The four embedded templates and the blanks profile.Templates says each one |
| 358 | // takes. Kept together so that adding a verb to a .tmpl file without saying so |
| 359 | // here fails, which is the guard the constants used to get for free by sitting |
| 360 | // next to the contract. |
| 361 | var embeddedTemplates = []struct { |
| 362 | name string |
| 363 | body string |
| 364 | verb string |
| 365 | blanks int |
| 366 | filledBy []any |
| 367 | }{ |
| 368 | {"settings.toml.tmpl", settingsTemplate, "%q", 2, []any{"turbo-classic", "2s"}}, |
| 369 | {"snippets.toml.tmpl", snippetsTemplate, "%s", 2, []any{"General", "/tmp/snippets.toml"}}, |
| 370 | {"tools.toml.tmpl", toolsTemplate, "%", 0, nil}, |
| 371 | {"acp.toml.tmpl", agentsTemplate, "%[", 2, []any{".turbo-js", "/tmp/acp.toml"}}, |
| 372 | } |
| 373 | |
| 374 | func TestEveryTemplateIsEmbeddedAndNotEmpty(t *testing.T) { |
| 375 | // go:embed fails to compile when a file is missing, but an empty file |
| 376 | // compiles happily and writes an empty starter file into somebody's |
| 377 | // project. |
| 378 | for _, template := range embeddedTemplates { |
| 379 | if len(template.body) == 0 { |
| 380 | t.Errorf("%s embedded as nothing", template.name) |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | func TestEveryTemplateTakesTheBlanksItsContractPromises(t *testing.T) { |
| 386 | // profile.Templates documents the count and the verb of each. The |
| 387 | // templates live in files of their own, so nothing but this notices a |
| 388 | // verb added, removed, or changed. |
| 389 | for _, template := range embeddedTemplates { |
| 390 | if got := strings.Count(template.body, template.verb); got != template.blanks { |
| 391 | t.Errorf("%s holds %d %q, want %d", template.name, got, template.verb, template.blanks) |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | func TestFillingATemplateLeavesNoFormattingMarker(t *testing.T) { |
| 397 | // Go writes %!q(MISSING) or %!(EXTRA …) into the output rather than |
| 398 | // failing, so a template with the wrong number of blanks produces a file |
| 399 | // that is written, opened, and wrong. |
| 400 | for _, template := range embeddedTemplates { |
| 401 | filled := template.body |
| 402 | if template.filledBy != nil { |
| 403 | filled = fmt.Sprintf(template.body, template.filledBy...) |
| 404 | } |
| 405 | if strings.Contains(filled, "%!") { |
| 406 | t.Errorf("%s filled to:\n%s", template.name, filled) |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | func TestEveryTemplateNamesThisEditorAndNoOther(t *testing.T) { |
| 412 | // The four templates started as Turbo Rust's. A leftover "turbo-rust" — |
| 413 | // or a leftover Rust word — in a file written into somebody's Node |
| 414 | // project is the whole class of mistake this catches, and it is invisible |
| 415 | // to every other test here. |
| 416 | others := []string{ |
| 417 | "turbo-go", "turbo-rust", "turbo-python", "turbo-moonbit", "turbo-golo", |
| 418 | "cargo", "rustup", ".rs files", "gopls", "pylsp", "moon ", "golo", |
| 419 | } |
| 420 | |
| 421 | for _, template := range embeddedTemplates { |
| 422 | t.Run(template.name, func(t *testing.T) { |
| 423 | for _, other := range others { |
| 424 | if strings.Contains(template.body, other) { |
| 425 | t.Errorf("the %s template still says %q:\n%s", template.name, other, template.body) |
| 426 | } |
| 427 | } |
| 428 | if !strings.Contains(template.body, Slug) { |
| 429 | t.Errorf("the %s template never names %s:\n%s", template.name, Slug, template.body) |
| 430 | } |
| 431 | }) |
| 432 | } |
| 433 | } |