131 lines
3.0 KiB
TypeScript
131 lines
3.0 KiB
TypeScript
import { describe, it } from 'node:test';
|
|||
|
|
import assert from 'node:assert/strict';
|
||
|
|
import { looksLikeCode } from './looksLikeCode';
|
||
|
|
|
||
|
|
const code = (text: string, html?: string) => looksLikeCode(text, html);
|
||
|
|
|
||
|
|
describe('looksLikeCode: code', () => {
|
||
|
|
it('JavaScript', () => {
|
||
|
|
const r = code(`function add(a, b) {
|
||
|
|
const sum = a + b;
|
||
|
|
if (sum > 10) {
|
||
|
|
return sum;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}`);
|
||
|
|
assert.equal(r.isCode, true);
|
||
|
|
assert.equal(r.lang, 'js');
|
||
|
|
});
|
||
|
|
|
||
|
|
it('TypeScript', () => {
|
||
|
|
const r = code(`interface User {
|
||
|
|
id: string;
|
||
|
|
name: string;
|
||
|
|
}
|
||
|
|
const u: User = { id: '1', name: 'a' };`);
|
||
|
|
assert.deepEqual(r, { isCode: true, lang: 'ts' });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('Python', () => {
|
||
|
|
const r = code(`def greet(name):
|
||
|
|
if not name:
|
||
|
|
return "hi"
|
||
|
|
return f"hi {name}"
|
||
|
|
print(greet("bob"))`);
|
||
|
|
assert.deepEqual(r, { isCode: true, lang: 'python' });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('SQL', () => {
|
||
|
|
const r = code(`SELECT id, name
|
||
|
|
FROM users
|
||
|
|
WHERE active = 1
|
||
|
|
ORDER BY name;`);
|
||
|
|
assert.deepEqual(r, { isCode: true, lang: 'sql' });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('Rust', () => {
|
||
|
|
const r = code(`pub fn main() {
|
||
|
|
let mut total = 0;
|
||
|
|
for i in 0..10 {
|
||
|
|
total += i;
|
||
|
|
}
|
||
|
|
}`);
|
||
|
|
assert.deepEqual(r, { isCode: true, lang: 'rust' });
|
||
|
|
});
|
||
|
|
|
||
|
|
it('config-ish JSON without a language guess', () => {
|
||
|
|
const r = code(`{
|
||
|
|
"name": "lotus",
|
||
|
|
"version": "1.0.0",
|
||
|
|
"private": true
|
||
|
|
}`);
|
||
|
|
assert.equal(r.isCode, true);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('an editor paste carrying monospace HTML counts as a signal', () => {
|
||
|
|
const text = `server {
|
||
|
|
listen 443 ssl;
|
||
|
|
server_name chat.example.org;
|
||
|
|
}`;
|
||
|
|
assert.equal(code(text, '<div style="font-family: Menlo, monospace">x</div>').isCode, true);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
|
||
|
|
describe('looksLikeCode: not code', () => {
|
||
|
|
it('fewer than three lines', () => {
|
||
|
|
assert.equal(code('const a = 1;\nconst b = 2;').isCode, false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('prose with line breaks', () => {
|
||
|
|
assert.equal(
|
||
|
|
code(`Hey, are we still on for tonight?
|
||
|
|
I can bring snacks if someone else handles drinks.
|
||
|
|
Let me know what time works, I'm free after six.`).isCode,
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('a long paragraph pasted as lines', () => {
|
||
|
|
const line =
|
||
|
|
'This is a long sentence about the plan. It keeps going for a while. And then some more words follow here.';
|
||
|
|
assert.equal(code([line, line, line].join('\n')).isCode, false);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('a markdown list', () => {
|
||
|
|
assert.equal(
|
||
|
|
code(`- milk (2 litres)
|
||
|
|
- eggs: a dozen
|
||
|
|
- bread, the good kind
|
||
|
|
- coffee!`).isCode,
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('a quoted reply', () => {
|
||
|
|
assert.equal(
|
||
|
|
code(`> const a = 1;
|
||
|
|
> if (a) { run(); }
|
||
|
|
> return a;`).isCode,
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('a list of links', () => {
|
||
|
|
assert.equal(
|
||
|
|
code(`https://example.org/a?b=1
|
||
|
|
https://example.org/c
|
||
|
|
https://example.org/d;e`).isCode,
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('a log excerpt', () => {
|
||
|
|
assert.equal(
|
||
|
|
code(`2026-09-25 10:00:01 INFO server started
|
||
|
|
2026-09-25 10:00:02 INFO listening on 8008
|
||
|
|
2026-09-25 10:00:05 WARN slow request 1200ms`).isCode,
|
||
|
|
false,
|
||
|
|
);
|
||
|
|
});
|
||
|
|
});
|