package jslang_test import ( "context" "errors" "os" "os/exec" "path/filepath" "strings" "testing" "time" "github.com/gdamore/tcell/v2" "rickub.com/turbo-editors/turbo-core/app" "rickub.com/turbo-editors/turbo-core/buffer" "rickub.com/turbo-editors/turbo-core/lsp" "rickub.com/turbo-editors/turbo-core/syntax" "rickub.com/turbo-editors/turbo-core/ui" "rickub.com/turbo-editors/turbo-js/internal/jslang" ) // --- the editor, assembled -------------------------------------------------- func TestTheEditorCallsItselfTurboJS(t *testing.T) { editor := newTestEditor(t) if got := editor.Profile().Name; got != jslang.Name { t.Errorf("Profile().Name = %q, want %q", got, jslang.Name) } if got := editor.Profile().ProjectDir(); got != ".turbo-js" { t.Errorf("ProjectDir() = %q, want %q", got, ".turbo-js") } } func TestTheEditorColoursJavaScriptItOpens(t *testing.T) { // The whole path in one test: Register taught the library this editor's // JavaScript, the profile named the editor, and a .js file opened through // the public API comes out coloured — by this scanner, which is what the // regular expression proves. root := t.TempDir() path := filepath.Join(root, "main.js") writeFile(t, path, "const re = /x/;\nconsole.log(re);\n") editor := newTestEditor(t) editor.Open(path) if got := editor.ActiveView().Language(); got != jslang.Language { t.Fatalf("the view colours the file as %q, want %q", got, jslang.Language) } spans := syntax.Highlight(jslang.Language, "const re = /x/;") if len(spans[0]) == 0 { t.Fatal("the registered JavaScript scanner colours nothing") } if !coloured(spans[0], 11, syntax.ClassChar) { t.Errorf("/x/ is not coloured as a regular expression; the library's own scanner is still in charge: %v", spans[0]) } } func TestTheEditorColoursPackageJSON(t *testing.T) { root := t.TempDir() path := filepath.Join(root, "package.json") writeFile(t, path, "{\n \"name\": \"demo\"\n}\n") editor := newTestEditor(t) editor.Open(path) if got := editor.ActiveView().Language(); got != jslang.LanguageJSON { t.Errorf("package.json is coloured as %q, want %q", got, jslang.LanguageJSON) } } func TestAScriptWithANodeShebangAndNoExtensionIsJavaScriptToo(t *testing.T) { // A command-line tool has no extension; its first line is what identifies // it, and the editor reads that line before choosing a scanner. root := t.TempDir() path := filepath.Join(root, "cli") writeFile(t, path, "#!/usr/bin/env node\nconsole.log('hi');\n") editor := newTestEditor(t) editor.Open(path) if got := editor.ActiveView().Language(); got != jslang.Language { t.Errorf("a script opening with a node shebang is coloured as %q, want %q", got, jslang.Language) } } func TestTheEditorDoesNotColourTheOtherEditorsLanguages(t *testing.T) { // "JavaScript instead of Python" is the whole point of this editor being // a separate one: a .py, .rs or .go file opens as plain text here — and // so does TypeScript, which the reference documents as a boundary. root := t.TempDir() editor := newTestEditor(t) for name, source := range map[string]string{ "main.py": "print('hi')\n", "main.rs": "fn main() {}\n", "main.go": "package main\n", "main.ts": "const x: number = 1;\n", } { path := filepath.Join(root, name) writeFile(t, path, source) editor.Open(path) if got := editor.ActiveView().Language(); got != syntax.LanguageNone { t.Errorf("%s is coloured as %q; Turbo JS registers JavaScript and JSON, nothing else", name, got) } } } func TestAProjectsOwnFilesAreStillColouredByTheLibrary(t *testing.T) { // A README, a compose file, a Dockerfile and a web page are what a Node // project is made of besides its scripts, and turbo-core colours all four // without this editor doing anything. That the inherited languages // survive registration is worth one test, because syntax.Register writes // into package-level state — and this editor replaces one of them. root := t.TempDir() editor := newTestEditor(t) for name, want := range map[string]syntax.Language{ "README.md": syntax.LanguageMarkdown, "compose.yaml": syntax.LanguageYAML, "Dockerfile": syntax.LanguageDockerfile, "index.html": syntax.LanguageHTML, } { path := filepath.Join(root, name) writeFile(t, path, "# heading\n") editor.Open(path) if got := editor.ActiveView().Language(); got != want { t.Errorf("%s is coloured as %q, want %q", name, got, want) } } } func TestTheToolchainMenuIsCalledJavaScriptAndNoTwoMenusShareAHotKey(t *testing.T) { // The bar answers the first menu whose hot key matches, so a clash makes // one of the two unreachable from the keyboard — silently, and with every // other test still passing. JavaScript takes J because none of the fixed // menus does, which is exactly the sort of thing only this test notices. editor := newTestEditor(t) seen := map[rune]string{} found := false for _, menu := range editor.MenuBar().Menus() { label, hot, _ := ui.SplitHotKey(menu.Label) if label == "JavaScript" { found = true } if hot == 0 { t.Errorf("the %q menu has no hot key", label) continue } if other, clash := seen[hot]; clash { t.Errorf("%q and %q both answer to Alt-%c", other, label, hot) } seen[hot] = label } if !found { t.Error("there is no JavaScript menu on the bar") } } func TestTheAgentMenuOffersToCreateTheAgentsFileInAProjectWithoutOne(t *testing.T) { // The agent windows are turbo-core's; what this editor contributes is the // starter file, and the menu item that writes it has to be reachable in a // project that has none — which is every project, at first. t.Chdir(t.TempDir()) editor := newTestEditor(t) var agentMenu *ui.Menu for _, menu := range editor.MenuBar().Menus() { if label, _, _ := ui.SplitHotKey(menu.Label); label == "Agent" { agentMenu = menu } } if agentMenu == nil { t.Fatal("there is no Agent menu on the bar") } for _, item := range agentMenu.Items { if label, _, _ := ui.SplitHotKey(item.Label); label == "Create agents file" { if item.Enabled != nil && !item.Enabled() { t.Error("Create agents file is greyed out in a project that has no agents file") } return } } t.Error("the Agent menu has no Create agents file item") } // --- driven against a real typescript-language-server ----------------------- // TestCompletionEndToEndWithRealServer drives the exact sequence the command // does at start-up: open the file first, start the language server second, // then ask for a completion. // // That order is the whole point, and it is the one Turbo Go got wrong once: an // editor that announces its open documents to a server which does not exist yet // and never mentions them again gets answers about a file the server has never // heard of — which looks, from the outside, exactly like completion not // working. // // It skips itself when typescript-language-server is not installed, and under // -short. func TestCompletionEndToEndWithRealServer(t *testing.T) { root, editor := startRealServer(t) path := filepath.Join(root, "main.js") // Both lines on disk are blank. A function is *declared* by typing, at top // level, then its name is typed inside main, so the answer can only come // from what the editor told the server — which is the whole point of this // test. The server offers every global for any file at all, so a // completion holding console would prove nothing; a completion holding a // function that exists only in the buffer proves the buffer was sent. view := editor.ActiveView() view.Buffer().SetCursor(buffer.Position{Line: declarationLine, Col: 0}) typeText(editor, "function zorglub(x) { return x; }") view.Buffer().SetCursor(buffer.Position{Line: completionLine, Col: 2}) typeText(editor, "zorg") if !waitForCompletion(t, editor) { t.Fatalf("no completion list opened for %s; the status bar says %q", path, editor.StatusBar().Message()) } if !completionOffers(editor, "zorglub") { t.Errorf("the list does not offer the function typed into the buffer; it has %d entries", editor.Completion().Count()) } } func TestGoToDefinitionWithRealServer(t *testing.T) { root, editor := startRealServer(t) path := filepath.Join(root, "main.js") locations := waitForLocations(t, func(ctx context.Context) ([]lsp.Location, error) { return editor.Language().Definition(ctx, path, callLine, callColumn, callLineText) }) if len(locations) != 1 { t.Fatalf("the call to helper has %d definitions, want exactly 1: %v", len(locations), locations) } if got := locations[0].Range.Start.Line; got != helperLine { t.Errorf("the definition of helper is on line %d, want %d", got, helperLine) } } func TestReferencesListEveryUseWithRealServer(t *testing.T) { // helper is declared once and called twice. Three answers is the shape // the editor turns into a list rather than a jump, and the one a single // definition never exercises. root, editor := startRealServer(t) path := filepath.Join(root, "main.js") locations := waitForLocations(t, func(ctx context.Context) ([]lsp.Location, error) { return editor.Language().References(ctx, path, helperLine, helperColumn, helperLineText) }) lines := map[int]bool{} for _, location := range locations { lines[location.Range.Start.Line] = true } for _, want := range []int{firstCallLine, secondCallLine} { if !lines[want] { t.Errorf("the references to helper do not include the call on line %d: %v", want, lines) } } } func TestImplementationsOfAClassAreItsSubclassesWithRealServer(t *testing.T) { // Shape has two subclasses. Asking for its implementations is the one // question whose answer is a list by nature, and the server answers it // for JavaScript as it does for TypeScript. root, editor := startRealServer(t) path := filepath.Join(root, "main.js") locations := waitForLocations(t, func(ctx context.Context) ([]lsp.Location, error) { return editor.Language().Implementation(ctx, path, shapeLine, shapeColumn, shapeLineText) }) lines := map[int]bool{} for _, location := range locations { lines[location.Range.Start.Line] = true } for _, want := range []int{circleLine, squareLine} { if !lines[want] { t.Errorf("the implementations of Shape do not include the class on line %d: %v", want, lines) } } } func TestTypeDefinitionOfAnInstanceIsItsClassWithRealServer(t *testing.T) { root, editor := startRealServer(t) path := filepath.Join(root, "main.js") locations := waitForLocations(t, func(ctx context.Context) ([]lsp.Location, error) { return editor.Language().TypeDefinition(ctx, path, shapeVarLine, shapeVarColumn, shapeVarLineText) }) if len(locations) != 1 || locations[0].Range.Start.Line != circleLine { t.Errorf("the type of `shape` is defined at %v, want line %d (class Circle)", locations, circleLine) } } func TestHoverShowsTheDocCommentAboveADeclarationWithRealServer(t *testing.T) { // A JSDoc block above a function is its documentation, and the server // shows it on hover. This is what F1 — Code ▸ Describe symbol — draws, and // it is the one answer here that carries prose a person wrote. root, editor := startRealServer(t) path := filepath.Join(root, "main.js") var text string waitUntil(t, 30*time.Second, func() bool { ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) defer cancel() answer, err := editor.Language().Hover(ctx, path, callLine, callColumn, callLineText) if err != nil { return false } text = answer return strings.Contains(text, "Adds one") }) if !strings.Contains(text, "Adds one") { t.Errorf("hovering helper gave %q, want the comment written above its declaration", text) } } func TestTheSymbolsOfAFileWithRealServer(t *testing.T) { root, editor := startRealServer(t) path := filepath.Join(root, "main.js") var symbols []lsp.Symbol waitUntil(t, 30*time.Second, func() bool { ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) defer cancel() found, err := editor.Language().DocumentSymbols(ctx, path) if err != nil { return false } symbols = found return len(symbols) > 0 }) names := map[string]bool{} for _, symbol := range symbols { names[symbol.Name] = true } for _, want := range []string{"helper", "first", "second", "Shape", "Circle", "Square", "main"} { if !names[want] { t.Errorf("the file's symbols do not include %q: %v", want, names) } } } func TestAProjectWideSymbolSearchWithRealServer(t *testing.T) { // Ctrl-T. The server indexes the project it was started in, so the // answer names the file the symbol is declared in. _, editor := startRealServer(t) var symbols []lsp.Symbol waitUntil(t, 30*time.Second, func() bool { ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) defer cancel() found, err := editor.Language().WorkspaceSymbols(ctx, "helper") if err != nil { return false } symbols = found return len(symbols) > 0 }) found := false for _, symbol := range symbols { if symbol.Name == "helper" { found = true } } if !found { t.Errorf("a project-wide search for helper found %v", symbols) } } // Diagnostics are the one thing a language server sends without being asked, // and the only feature whose failure looks exactly like success: an editor with // no error to show and one that cannot find the error are the same blank // gutter. So this opens a file that does not parse and waits for the mark. func TestDiagnosticsForAFileThatDoesNotParseWithRealServer(t *testing.T) { root, editor := startRealServerOn(t, brokenScript) path := filepath.Join(root, "main.js") waitUntil(t, 30*time.Second, func() bool { editor.Tick() return len(editor.Language().Diagnostics(path)) > 0 }) problems := editor.Language().Diagnostics(path) if len(problems) == 0 { t.Fatalf("no diagnostic ever arrived for %s; the status bar says %q", path, editor.StatusBar().Message()) } if _, ok := editor.Language().FirstError(path); !ok { t.Errorf("the diagnostics hold no error, only %v", problems) } } // --- the fixtures and the waiting ------------------------------------------- // realScript is the file every language-server test works against. Line // numbers are counted from zero and are named by the constants below, so // inserting a line here moves them and the constants have to move too. // // It runs under node with no error, which matters: a fixture the runtime // complains about would make the diagnostics test pass for the wrong reason. const realScript = "// A tiny module.\n" + // 0 "\n" + // 1 "/** Adds one. */\n" + // 2 "function helper(x) {\n" + // 3 " return x + 1;\n" + // 4 "}\n" + // 5 "\n" + // 6 "function first(x) {\n" + // 7 " return helper(x);\n" + // 8 "}\n" + // 9 "\n" + // 10 "function second(x) {\n" + // 11 " return helper(x) * 2;\n" + // 12 "}\n" + // 13 "\n" + // 14 "class Shape {\n" + // 15 " area() {\n" + // 16 " return 0;\n" + // 17 " }\n" + // 18 "}\n" + // 19 "\n" + // 20 "class Circle extends Shape {\n" + // 21 " area() {\n" + // 22 " return 3;\n" + // 23 " }\n" + // 24 "}\n" + // 25 "\n" + // 26 "class Square extends Shape {\n" + // 27 " area() {\n" + // 28 " return 4;\n" + // 29 " }\n" + // 30 "}\n" + // 31 "\n" + // 32 "\n" + // 33 ← where the declaration is typed, at top level "\n" + // 34 "function main() {\n" + // 35 " const text = \"hi\";\n" + // 36 " const shape = new Circle();\n" + // 37 " \n" + // 38 ← two spaces, and where the completion is typed " console.log(first(1) + second(2) + text, shape.area());\n" + // 39 "}\n" + // 40 "\n" + // 41 "main();\n" // 42 // brokenScript is a file that does not parse: the parenthesis of main's // parameter list is never closed. It exists as a second fixture rather than as // a line added to the first, because a file holding a syntax error is a file // whose *other* answers are worth nothing: the completion test would then be // measuring a parse that never finished. const brokenScript = "// Does not parse.\n" + "\n" + "function main( {\n" + " const x = (1 +\n" // Where the fixture's interesting lines are, counted from zero. const ( helperLine = 3 helperColumn = 9 helperLineText = "function helper(x) {" callLine = 8 callColumn = 9 callLineText = " return helper(x);" firstCallLine = 8 secondCallLine = 12 shapeLine = 15 shapeColumn = 6 shapeLineText = "class Shape {" circleLine = 21 squareLine = 27 declarationLine = 33 shapeVarLine = 37 shapeVarColumn = 8 shapeVarLineText = " const shape = new Circle();" completionLine = 38 ) // startRealServer writes a project, opens its file, starts the server and // waits for it, in the order the command does. It skips the test when the // server is missing. func startRealServer(t *testing.T) (root string, editor *app.App) { t.Helper() return startRealServerOn(t, realScript) } // startRealServerOn is startRealServer over a chosen main.js. func startRealServerOn(t *testing.T, source string) (root string, editor *app.App) { t.Helper() if testing.Short() { t.Skip("-short: not starting a language server") } server, err := lsp.FindServer(jslang.Profile().Server) if errors.Is(err, lsp.ErrServerNotFound) { t.Skipf("%s is not installed; %s", jslang.ServerCommand, jslang.InstallHint) } // Finding it is not the same as being able to run it: a shim left behind // by a version manager whose Node has since been removed is on PATH and // fails only when started. if !serverRuns(server) { t.Skipf("%s at %s cannot run; %s", jslang.ServerCommand, server, jslang.InstallHint) } root = t.TempDir() writeFile(t, filepath.Join(root, "package.json"), "{\n \"name\": \"demo\",\n \"type\": \"commonjs\"\n}\n") writeFile(t, filepath.Join(root, "main.js"), source) editor = newTestEditor(t) // 1. Open the file, exactly as main does — before there is any server. editor.Open(filepath.Join(root, "main.js")) // 2. Start the language server, exactly as main does — afterwards, in the // directory holding package.json, which is what ProjectRoot answers. ctx, cancel := context.WithCancel(t.Context()) t.Cleanup(cancel) editor.StartLanguageServer(ctx, root) t.Cleanup(func() { editor.Language().Stop(context.Background()) }) waitUntilReady(t, editor) // 3. Let the event loop notice the server is ready, as Run does on every // turn. This is what announces the file that was already open. editor.Tick() return root, editor } // newTestEditor returns Turbo JS drawing on a simulated terminal, set up the // way the command sets it up. func newTestEditor(t *testing.T) *app.App { t.Helper() jslang.Register() screen := tcell.NewSimulationScreen("UTF-8") if err := screen.Init(); err != nil { t.Fatalf("initialising the simulation screen: %v", err) } t.Cleanup(screen.Fini) screen.SetSize(80, 24) // Never read the themes or snippets of whoever is running the tests. p := jslang.Profile() t.Setenv(p.ThemeDirEnvVar(), t.TempDir()) t.Setenv(p.SnippetDirEnvVar(), t.TempDir()) editor := app.New(screen, "turbo-classic", p) editor.Render() return editor } // coloured reports whether a rune column on a line carries a class. func coloured(spans []syntax.Span, col int, want syntax.Class) bool { for _, span := range spans { if span.Start <= col && col < span.End { return span.Class == want } } return false } // typeText sends a run of printable characters through the whole routing chain. func typeText(editor *app.App, text string) { for _, r := range text { editor.Handle(tcell.NewEventKey(tcell.KeyRune, r, tcell.ModNone)) } } // completionOffers reports whether the open popup holds an entry starting with // a label. func completionOffers(editor *app.App, label string) bool { for _, item := range editor.Completion().Matches() { if strings.HasPrefix(item.Label, label) { return true } } return false } // waitUntilReady blocks until the language server has finished starting. func waitUntilReady(t *testing.T, editor *app.App) { t.Helper() deadline := time.After(lsp.InitializeTimeout) for !editor.Language().Ready() { select { case <-deadline: t.Fatalf("the language server never became ready: %s", editor.Language().Status()) case <-time.After(10 * time.Millisecond): } } } // waitUntil polls a condition until it holds or the time runs out, and fails // the test if it never does. func waitUntil(t *testing.T, within time.Duration, done func() bool) { t.Helper() deadline := time.Now().Add(within) for time.Now().Before(deadline) { if done() { return } time.Sleep(200 * time.Millisecond) } t.Errorf("the server never answered within %s", within) } // waitForLocations asks a location question until it is answered, because a // server that is still loading its project answers an empty list rather than // an error. func waitForLocations(t *testing.T, ask func(context.Context) ([]lsp.Location, error)) []lsp.Location { t.Helper() var found []lsp.Location waitUntil(t, 30*time.Second, func() bool { ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) defer cancel() locations, err := ask(ctx) if err != nil { return false } found = locations return len(found) > 0 }) return found } // waitForCompletion asks for a completion until one arrives, or gives up. // // tsserver loads its project after it has finished initialising, and answers // an empty list until that is done. There is no notification this client reads // that says when — so it asks again, which is what the editor's user would do. func waitForCompletion(t *testing.T, editor *app.App) bool { t.Helper() deadline := time.Now().Add(60 * time.Second) for time.Now().Before(deadline) { if editor.Completion().Visible() { return true } editor.RequestCompletion() if editor.Completion().Visible() { return true } time.Sleep(500 * time.Millisecond) } return false } // serverRuns reports whether the language server at path actually starts. func serverRuns(path string) bool { return exec.Command(path, "--version").Run() == nil } // writeFile creates a file, making its directory first. func writeFile(t *testing.T, path, content string) { t.Helper() if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { t.Fatalf("creating %s: %v", filepath.Dir(path), err) } if err := os.WriteFile(path, []byte(content), 0o644); err != nil { t.Fatalf("writing %s: %v", path, err) } }