1
0

is-test-file.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * isTestFile heuristic — test-file detection used to deprioritize test code in
  3. * search/explore ranking.
  4. *
  5. * Regression coverage for the cold-query fix: the heuristic previously only
  6. * knew Java/JS/Python conventions, so Kotlin (`*Test.kt`, `jvmTest/`), Swift
  7. * (`*Tests.swift`), and camelCase test source-set dirs slipped through — which
  8. * let OkHttp's tests flood `codegraph_explore` results on a plain-language
  9. * query. The false-positive guards matter just as much: `latest.kt` /
  10. * `manifest.kt` / a `RealCall.kt` production file must NOT be flagged.
  11. */
  12. import { describe, it, expect } from 'vitest';
  13. import { isTestFile } from '../src/search/query-utils';
  14. describe('isTestFile', () => {
  15. it('flags Kotlin test files and source sets', () => {
  16. expect(isTestFile('okhttp/src/jvmTest/kotlin/okhttp3/CallTest.kt')).toBe(true);
  17. expect(isTestFile('okhttp/src/commonTest/kotlin/okhttp3/CompressionInterceptorTest.kt')).toBe(true);
  18. expect(isTestFile('app/src/androidTest/java/com/example/FooTest.kt')).toBe(true);
  19. expect(isTestFile('module/src/integrationTest/kotlin/BarSpec.kt')).toBe(true);
  20. });
  21. it('flags Swift test files', () => {
  22. expect(isTestFile('Tests/SessionTests.swift')).toBe(true);
  23. expect(isTestFile('Sources/FooTest.swift')).toBe(true);
  24. });
  25. it('still flags the previously-supported conventions', () => {
  26. expect(isTestFile('foo/test_bar.py')).toBe(true);
  27. expect(isTestFile('pkg/bar_test.go')).toBe(true);
  28. expect(isTestFile('src/foo.test.ts')).toBe(true);
  29. expect(isTestFile('src/foo.spec.ts')).toBe(true);
  30. expect(isTestFile('com/example/FooTest.java')).toBe(true);
  31. expect(isTestFile('com/example/FooTestCase.java')).toBe(true);
  32. expect(isTestFile('project/__tests__/foo.ts')).toBe(true);
  33. expect(isTestFile('project/tests/foo.rb')).toBe(true);
  34. });
  35. it('does NOT flag production files that merely contain "test" lowercase', () => {
  36. // The fix is capital-led so camelCase boundaries distinguish these.
  37. expect(isTestFile('src/latest/loader.kt')).toBe(false);
  38. expect(isTestFile('lib/manifest.kt')).toBe(false);
  39. expect(isTestFile('okhttp/src/jvmMain/kotlin/okhttp3/internal/connection/RealCall.kt')).toBe(false);
  40. expect(isTestFile('src/contestEntry.ts')).toBe(false);
  41. expect(isTestFile('pkg/greatest.go')).toBe(false);
  42. });
  43. it('does NOT flag ordinary production source', () => {
  44. expect(isTestFile('src/flask/app.py')).toBe(false);
  45. expect(isTestFile('src/vs/workbench/api/common/extensionHostMain.ts')).toBe(false);
  46. expect(isTestFile('okhttp/src/commonJvmAndroid/kotlin/okhttp3/OkHttpClient.kt')).toBe(false);
  47. });
  48. });