import assert from 'node:assert/strict'
import test from 'node:test'
import {
classifyPath,
kindFor,
languageFor,
parseLinguistGenerated,
patchLooksGenerated,
} from './classify.ts'
test('counts hand-written source, including names that mention generated', () => {
const result = classifyPath('packages/worker/src/identity/generated-username.ts')
assert.equal(result.countable, true)
assert.equal(result.language, 'TypeScript')
assert.equal(result.kind, 'source')
assert.equal(
classifyPath('packages/worker/src/app/saved-ui-hosted-html.ts').countable,
true,
)
})
test('skips wrangler types even though they are committed', () => {
const result = classifyPath('packages/worker/worker-configuration.d.ts', {
patch: '+// Generated by Wrangler by running `wrangler types`\n+interface Env {}',
})
assert.equal(result.countable, false)
assert.match(result.reason, /wrangler-types|generated-content-marker/)
})
test('skips generated declaration stubs and lockfiles', () => {
assert.equal(
classifyPath('packages/worker/src/generated-guide-catalog.d.ts').countable,
false,
)
assert.equal(
classifyPath('packages/shared/src/generated-ui-utils.ts').countable,
false,
)
assert.equal(classifyPath('package-lock.json').countable, false)
assert.equal(classifyPath('pnpm-lock.yaml').countable, false)
})
test('skips files under generated directories', () => {
assert.equal(
classifyPath('packages/worker/src/generated/guide-catalog.mjs').countable,
false,
)
})
test('classifies tests, docs, and source', () => {
assert.equal(kindFor('tools/ci/resource-utils.node.test.ts'), 'tests')
assert.equal(kindFor('docs/use/packages.md'), 'docs')
assert.equal(kindFor('README.md'), 'docs')
assert.equal(kindFor('packages/worker/src/index.ts'), 'source')
assert.equal(languageFor('packages/worker/src/app.ts'), 'TypeScript')
assert.equal(languageFor('docs/use/packages.md'), 'Markdown')
})
test('detects generated file headers in patches', () => {
assert.equal(
patchLooksGenerated(
'@@ -1,2 +1,3 @@\n+/* eslint-disable */\n+// Generated by Wrangler by running `wrangler types`\n interface Env {}',
),
true,
)
assert.equal(
patchLooksGenerated('@@ -1 +1 @@\n-export const x = 1\n+export const x = 2\n'),
false,
)
})
test('honors linguist-generated attributes', () => {
const patterns = parseLinguistGenerated(
'packages/worker/migrations/*.sql text eol=lf\n*.generated.ts linguist-generated=true\n',
)
const result = classifyPath('src/foo.generated.ts', {
ignore: {
gitignore: [],
linguistGenerated: patterns,
knownGeneratedPaths: new Set(),
},
})
assert.equal(result.countable, false)
assert.equal(result.reason, 'gitattributes-linguist-generated')
})