Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions __tests__/near-duplicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const unrelated = [
describe('signature', () => {
it('matches copies that differ only in names and literals, not unrelated code', () => {
const a = signature(urlOf('fpUrlOf', 'fp'), 'fpUrlOf')!;
const b = signature(urlOf('ttdUrlOf', 'ttd'), 'ttdUrlOf')!;
const b = signature(urlOf('webUrlOf', 'web'), 'webUrlOf')!;
expect(similarity(a, b)).toBeGreaterThanOrEqual(0.8);
expect(similarity(a, signature(unrelated)!)).toBeLessThan(0.3);
});
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('near-duplicates in the index', () => {
beforeEach(async () => {
dir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-near-dups-'));
write('src/fp.ts', urlOf('fpUrlOf', 'fp'));
write('src/ttd.ts', urlOf('ttdUrlOf', 'ttd'));
write('src/web.ts', urlOf('webUrlOf', 'web'));
write('src/ds.ts', urlOf('dsUrlOf', 'ds'));
write('src/points.ts', unrelated);
// Copies in tests and vendored code are expected and stay out.
Expand All @@ -87,15 +87,15 @@ describe('near-duplicates in the index', () => {
});

it('pairs the copies and leaves out tests, vendored code and unrelated functions', () => {
expect(dupsOf('fpUrlOf')).toEqual(['dsUrlOf', 'ttdUrlOf']);
expect(dupsOf('fpUrlOf')).toEqual(['dsUrlOf', 'webUrlOf']);
expect(dupsOf('totalPoints')).toEqual([]);
});

it('names the copies in explore and codegraph_node', async () => {
const explore = await tool('codegraph_explore', { query: 'fpUrlOf' });
expect(explore).toMatch(/- `fpUrlOf` \(src\/fp\.ts:1\) — no callers; near-duplicates \(update together\): .*`dsUrlOf` \(src\/ds\.ts:1, \d+% similar\)/);
const node = await tool('codegraph_node', { symbol: 'fpUrlOf', includeCode: false });
expect(node).toMatch(/\*\*Near-duplicates \(update together\) ≈\*\* .*`ttdUrlOf` \(src\/ttd\.ts:1/);
expect(node).toMatch(/\*\*Near-duplicates \(update together\) ≈\*\* .*`webUrlOf` \(src\/web\.ts:1/);
});

it('re-scores only the pairs a changed body is in, and matches a full recompute', async () => {
Expand All @@ -114,7 +114,7 @@ describe('near-duplicates in the index', () => {
},
isExcluded: (p: string) => p.startsWith('test/') || p.startsWith('vendor/'),
};
const snapshot = () => ['fpUrlOf', 'ttdUrlOf', 'dsUrlOf', 'totalPoints', 'totalPoints2'].map((n) => [n, dupsOf(n)]);
const snapshot = () => ['fpUrlOf', 'webUrlOf', 'dsUrlOf', 'totalPoints', 'totalPoints2'].map((n) => [n, dupsOf(n)]);
const before = snapshot();
expect(dupsOf('totalPoints')).toEqual(['totalPoints2']);

Expand Down Expand Up @@ -171,13 +171,13 @@ describe('near-duplicates in the index', () => {
cg = await CodeGraph.open(dir);
write('src/ds.ts', unrelated.replace('totalPoints', 'dsTotals'));
await cg.sync();
expect(dupsOf('fpUrlOf')).toEqual(['ttdUrlOf']);
expect(dupsOf('fpUrlOf')).toEqual(['webUrlOf']);
expect(dupsOf('totalPoints')).toEqual(['dsTotals']);
});

it('follows edits and deletions on sync', async () => {
write('src/ds.ts', unrelated.replace('totalPoints', 'dsTotals'));
fs.rmSync(path.join(dir, 'src/ttd.ts'));
fs.rmSync(path.join(dir, 'src/web.ts'));
await cg.sync();
expect(dupsOf('fpUrlOf')).toEqual([]);
expect(dupsOf('totalPoints')).toEqual(['dsTotals']);
Expand Down
20 changes: 10 additions & 10 deletions __tests__/window-message-synthesizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ describe('window-message synthesizer', () => {

it('links a publisher to the listener that checks the same source discriminator', async () => {
fs.writeFileSync(path.join(dir, 'constants.ts'),
'export const MESSAGES = { TTD_AJAX_EVENT: "DEMO_AJAX", OTHER_EVENT: "DEMO_OTHER" };\n');
'export const MESSAGES = { DEMO_AJAX_EVENT: "DEMO_AJAX", OTHER_EVENT: "DEMO_OTHER" };\n');
fs.writeFileSync(path.join(dir, 'publish.ts'), `import { MESSAGES } from "./constants";
export function publishTtdLivewire() {
window.postMessage({ source: MESSAGES.TTD_AJAX_EVENT, payload: {} }, "*");
export function publishDemoLivewire() {
window.postMessage({ source: MESSAGES.DEMO_AJAX_EVENT, payload: {} }, "*");
}
export function publishOther() {
window.postMessage({ source: MESSAGES.OTHER_EVENT, payload: {} }, "*");
}
`);
fs.writeFileSync(path.join(dir, 'listen.ts'), `import { MESSAGES } from "./constants";
export function listenForTtdLivewireAjax() {
export function listenForDemoLivewireAjax() {
window.addEventListener("message", (ev) => {
if (ev.data.source !== MESSAGES.TTD_AJAX_EVENT) return;
receiveTtdSnapshot(ev.data.payload);
if (ev.data.source !== MESSAGES.DEMO_AJAX_EVENT) return;
receiveDemoSnapshot(ev.data.payload);
});
}
export function receiveTtdSnapshot(payload: unknown) { return payload; }
export function receiveDemoSnapshot(payload: unknown) { return payload; }
export function listenWithoutCheck() {
window.addEventListener("message", (ev) => { console.log(ev.data); });
}
Expand All @@ -46,11 +46,11 @@ export function listenWithoutCheck() {
await cg.indexAll();
const edges = synthEdges(cg);
expect(edges).toEqual([
expect.objectContaining({ source: 'publishTtdLivewire', target: 'listenForTtdLivewireAjax', event: 'TTD_AJAX_EVENT' }),
expect.objectContaining({ source: 'publishDemoLivewire', target: 'listenForDemoLivewireAjax', event: 'DEMO_AJAX_EVENT' }),
]);
expect(edges.some((e) => e.source === 'publishOther')).toBe(false);
expect(edges.some((e) => e.target === 'listenWithoutCheck')).toBe(false);
expect(edges.some((e) => e.target === 'receiveTtdSnapshot')).toBe(false);
expect(edges.some((e) => e.target === 'receiveDemoSnapshot')).toBe(false);
cg.close?.();
});

Expand All @@ -61,7 +61,7 @@ export function listenWithoutCheck() {
`);
fs.writeFileSync(path.join(dir, 'b.ts'), `export function listen() {
window.addEventListener("message", (ev) => {
if (ev.data.source !== OtherHub.TTD_OTHER_EVENT) return;
if (ev.data.source !== OtherHub.DEMO_OTHER_EVENT) return;
});
}
`);
Expand Down
2 changes: 1 addition & 1 deletion src/graph/near-duplicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function tokenize(body: string): string[] {
/**
* A body's shingle hashes, or null when it is too small to compare. The
* function's own name is masked: copies are usually renamed (`fpUrlOf`,
* `ttdUrlOf`), and the name alone moves several shingles.
* `webUrlOf`), and the name alone moves several shingles.
*/
export function shingles(body: string, ownName?: string): Set<number> | null {
const tokens = tokenize(body).map((t) => (t === ownName ? '\u0000self' : t));
Expand Down