package linthost

import (
  "strings"
  "testing"
)

// TestRuleCorpusUnboundMethod verifies an authored typed trigger for
// typescript/unbound-method under a real Program.
//
// `typescript/unbound-method` is type-aware: a parser-only engine run skips it
// because Context.Checker is nil. The rule reuses the `command_*` shape established
// by `no-floating-promises`'s corpus test: materialize a tsconfig project, run
// `ttsc lint check`, and assert on the rendered diagnostics.
//
// The corpus fixture packages/lint/test/testdata/corpus/typescript-unbound-method.ts is run by TestLintFixtureCorpus; this Go scenario locks the minimum-viable
// trigger (a class method referenced as a value) so a future shim regression
// surfaces here without depending on the full fixture.
//
// 1. Seed a project that pulls a class instance method off as a value.
// 2. Run `check` with typescript/unbound-method enabled as error.
// 3. Assert the command exits non-zero and stderr mentions the rule.
//
// @evidence contracts/testing.md#behavioral-verification Extracting a receiver-dependent instance method must report.
// @evidence contracts/testing.md#independent-expectations The original authored input fixes exactly one typescript/unbound-method rendered error at line 8, code 2 and empty stdout; its independently authored typed counterpart requires code 0 and no rule errors.
// @evidence contracts/testing.md#distinguishing-cases The independently authored direct receiver call stays clean, contrasting with the reported extraction; the test does not execute either method.
// @evidence contracts/testing.md#execution-ownership TestRuleCorpusUnboundMethod invokes the in-process check command and shared typed semantic oracles over real Program/Checker instances; fixture configuration feeds that operation without a native build, child compiler or installed consumer.
func TestRuleCorpusUnboundMethod(t *testing.T) {
  root := seedLintProject(t, `class Greeter {
  public name = "world";
  public greet(): string {
    return this.name;
  }
}
const g = new Greeter();
const fn = g.greet;
JSON.stringify(fn);
`)
  seedLintRules(t, root, map[string]string{"typescript/unbound-method": "error"})

  code, stdout, stderr := captureCommandOutput(t, func() int {
    return run([]string{
      "check",
      "--cwd", root,
      "--plugins-json", lintManifest(t),
    })
  })
  if code != 2 || stdout != "" || !strings.Contains(stderr, "[typescript/unbound-method]") {
    t.Fatalf("unbound-method diagnostic mismatch: code=%d stdout=%q stderr=%q", code, stdout, stderr)
  }
  assertTypedRuleRenderedErrors(t, "typescript/unbound-method", stderr, 8)
  assertTypedRuleCleanSource(t, "typescript/unbound-method", "class Greeter { public name = \"world\"; public greet(): string { return this.name; } }\nconst g = new Greeter();\nconst value = g.greet();\n")
}
