is-test-file.test.ts 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 test-support modules and doubles by directory name', () => {
  16. expect(isTestFile('core/data-test/src/main/kotlin/com/example/FakeUserDataRepository.kt')).toBe(true);
  17. expect(isTestFile('core/datastore-test/src/main/kotlin/com/example/InMemoryDataStore.kt')).toBe(true);
  18. expect(isTestFile('core/testing/src/main/kotlin/com/example/TestUserDataRepository.kt')).toBe(true);
  19. expect(isTestFile('pkg/testdata/fixture.go')).toBe(true);
  20. expect(isTestFile('src/__mocks__/api.ts')).toBe(true);
  21. expect(isTestFile('internal/testutil/helpers.go')).toBe(true);
  22. });
  23. it('does NOT flag production code whose package path runs through a samples or examples segment', () => {
  24. // Only the project layout above `src/` decides; the package path below it never does.
  25. expect(isTestFile('core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/SyncUtilities.kt')).toBe(false);
  26. expect(isTestFile('feature/foryou/impl/src/main/kotlin/com/google/samples/apps/ForYouViewModel.kt')).toBe(false);
  27. expect(isTestFile('src/samples/demo.ts')).toBe(false);
  28. // …while a real examples folder in the layout still counts.
  29. expect(isTestFile('examples/basic/src/index.ts')).toBe(true);
  30. expect(isTestFile('packages/x/examples/basic.ts')).toBe(true);
  31. expect(isTestFile('benchmarks/run.py')).toBe(true);
  32. });
  33. it('flags Kotlin test files and source sets', () => {
  34. expect(isTestFile('okhttp/src/jvmTest/kotlin/okhttp3/CallTest.kt')).toBe(true);
  35. expect(isTestFile('okhttp/src/commonTest/kotlin/okhttp3/CompressionInterceptorTest.kt')).toBe(true);
  36. expect(isTestFile('app/src/androidTest/java/com/example/FooTest.kt')).toBe(true);
  37. expect(isTestFile('module/src/integrationTest/kotlin/BarSpec.kt')).toBe(true);
  38. });
  39. it('flags Swift test files', () => {
  40. expect(isTestFile('Tests/SessionTests.swift')).toBe(true);
  41. expect(isTestFile('Sources/FooTest.swift')).toBe(true);
  42. });
  43. it('still flags the previously-supported conventions', () => {
  44. expect(isTestFile('foo/test_bar.py')).toBe(true);
  45. expect(isTestFile('pkg/bar_test.go')).toBe(true);
  46. expect(isTestFile('src/foo.test.ts')).toBe(true);
  47. expect(isTestFile('src/foo.spec.ts')).toBe(true);
  48. expect(isTestFile('com/example/FooTest.java')).toBe(true);
  49. expect(isTestFile('com/example/FooTestCase.java')).toBe(true);
  50. expect(isTestFile('project/__tests__/foo.ts')).toBe(true);
  51. expect(isTestFile('project/tests/foo.rb')).toBe(true);
  52. });
  53. it('does NOT flag production files that merely contain "test" lowercase', () => {
  54. // The fix is capital-led so camelCase boundaries distinguish these.
  55. expect(isTestFile('src/latest/loader.kt')).toBe(false);
  56. expect(isTestFile('lib/manifest.kt')).toBe(false);
  57. expect(isTestFile('okhttp/src/jvmMain/kotlin/okhttp3/internal/connection/RealCall.kt')).toBe(false);
  58. expect(isTestFile('src/contestEntry.ts')).toBe(false);
  59. expect(isTestFile('pkg/greatest.go')).toBe(false);
  60. });
  61. it('does NOT flag ordinary production source', () => {
  62. expect(isTestFile('src/flask/app.py')).toBe(false);
  63. expect(isTestFile('src/vs/workbench/api/common/extensionHostMain.ts')).toBe(false);
  64. expect(isTestFile('okhttp/src/commonJvmAndroid/kotlin/okhttp3/OkHttpClient.kt')).toBe(false);
  65. });
  66. });