-
Notifications
You must be signed in to change notification settings - Fork 67.5k
Expand file tree
/
Copy pathdeduplication.ts
More file actions
159 lines (142 loc) · 6.35 KB
/
Copy pathdeduplication.ts
File metadata and controls
159 lines (142 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { describe, expect, test } from 'vitest'
import fs from 'fs'
import path from 'path'
const ENABLED_APPS_DIR = 'src/github-apps/data'
describe('GitHub Apps deduplication', () => {
describe('shared format file structure', () => {
test('shared/entries.json exists and is a non-empty array', () => {
const entriesPath = path.join(ENABLED_APPS_DIR, 'shared', 'entries.json')
expect(fs.existsSync(entriesPath), `${entriesPath} should exist`).toBe(true)
const entries = JSON.parse(fs.readFileSync(entriesPath, 'utf8'))
expect(Array.isArray(entries)).toBe(true)
expect(entries.length).toBeGreaterThan(0)
})
test('version-index.json exists and has valid structure', () => {
const indexPath = path.join(ENABLED_APPS_DIR, 'version-index.json')
expect(fs.existsSync(indexPath), `${indexPath} should exist`).toBe(true)
const index = JSON.parse(fs.readFileSync(indexPath, 'utf8'))
expect(typeof index).toBe('object')
const versions = Object.keys(index)
expect(versions.length).toBeGreaterThan(0)
})
})
describe('entries in the pool are unique', () => {
test('no duplicate entries in entries.json', () => {
const entries = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'shared', 'entries.json'), 'utf8'),
)
const keys = new Set<string>()
for (const entry of entries) {
const key = JSON.stringify(entry)
expect(keys.has(key), `Duplicate entry found`).toBe(false)
keys.add(key)
}
})
})
describe('reconstruction matches original data', () => {
test('reconstructed rest data matches per-version JSON files', () => {
const entries = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'shared', 'entries.json'), 'utf8'),
)
const index = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'version-index.json'), 'utf8'),
)
// Test a non-permissions page type
for (const [version, pageTypes] of Object.entries(
index as Record<string, Record<string, unknown>>,
)) {
for (const [pageType, pageData] of Object.entries(pageTypes as Record<string, unknown>)) {
if (pageType.includes('permissions')) continue
// Reconstruct
const reconstructed: Record<string, unknown[]> = {}
for (const [category, indices] of Object.entries(pageData as Record<string, number[]>)) {
reconstructed[category] = indices.map((idx: number) => entries[idx])
}
// Compare with original
const originalPath = path.join(ENABLED_APPS_DIR, version, `${pageType}.json`)
if (!fs.existsSync(originalPath)) continue
const original = JSON.parse(fs.readFileSync(originalPath, 'utf8'))
for (const category of Object.keys(original)) {
expect(reconstructed[category]?.length).toBe(original[category].length)
for (let i = 0; i < original[category].length; i++) {
expect(reconstructed[category][i]).toEqual(original[category][i])
}
}
}
}
})
test('reconstructed permission data matches per-version JSON files', () => {
const entries = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'shared', 'entries.json'), 'utf8'),
)
const index = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'version-index.json'), 'utf8'),
)
for (const [version, pageTypes] of Object.entries(
index as Record<string, Record<string, unknown>>,
)) {
for (const [pageType, pageData] of Object.entries(pageTypes as Record<string, unknown>)) {
if (!pageType.includes('permissions')) continue
// Reconstruct
const reconstructed: Record<
string,
{ title: string; displayTitle: string; permissions: unknown[] }
> = {}
for (const [permName, meta] of Object.entries(
pageData as Record<string, { title: string; displayTitle: string; indices: number[] }>,
)) {
reconstructed[permName] = {
title: meta.title,
displayTitle: meta.displayTitle,
permissions: meta.indices.map((idx: number) => entries[idx]),
}
}
// Compare with original
const originalPath = path.join(ENABLED_APPS_DIR, version, `${pageType}.json`)
if (!fs.existsSync(originalPath)) continue
const original = JSON.parse(fs.readFileSync(originalPath, 'utf8'))
for (const permName of Object.keys(original)) {
expect(reconstructed[permName].title).toBe(original[permName].title)
expect(reconstructed[permName].displayTitle).toBe(original[permName].displayTitle)
expect(reconstructed[permName].permissions.length).toBe(
original[permName].permissions.length,
)
for (let i = 0; i < original[permName].permissions.length; i++) {
expect(reconstructed[permName].permissions[i]).toEqual(
original[permName].permissions[i],
)
}
}
}
}
})
})
describe('deduplication provides savings', () => {
test('unique entries are significantly fewer than total references', () => {
const entries = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'shared', 'entries.json'), 'utf8'),
)
const index = JSON.parse(
fs.readFileSync(path.join(ENABLED_APPS_DIR, 'version-index.json'), 'utf8'),
)
let totalReferences = 0
for (const pageTypes of Object.values(index as Record<string, Record<string, unknown>>)) {
for (const [pageType, pageData] of Object.entries(pageTypes as Record<string, unknown>)) {
if (pageType.includes('permissions')) {
for (const meta of Object.values(pageData as Record<string, { indices: number[] }>)) {
totalReferences += meta.indices.length
}
} else {
for (const indices of Object.values(pageData as Record<string, number[]>)) {
totalReferences += indices.length
}
}
}
}
const uniqueEntries = entries.length
// We expect at least 70% dedup rate based on issue analysis (~84% reported)
const dedupRate = 1 - uniqueEntries / totalReferences
expect(dedupRate).toBeGreaterThan(0.7)
})
})
})