test: add test for VNG

This commit is contained in:
EnixCoda 2022-04-24 15:58:23 +08:00
parent d9272a7b24
commit abcd3c3a3e
6 changed files with 2165 additions and 3 deletions

View file

@ -64,9 +64,10 @@ module.exports = {
maxWorkers: 8,
// An array of directory names to be searched recursively up from the requiring module's location
// moduleDirectories: [
// "node_modules"
// ],
moduleDirectories: [
"src",
"node_modules"
],
// An array of file extensions your modules use
// moduleFileExtensions: [

View file

@ -0,0 +1,50 @@
import { VisibleNodes, VisibleNodesGenerator } from '.'
import { searchModes } from '../../components/searchModes/index'
import { prepareVisibleNodes } from './prepare'
import searchResult__json from './searchResults/json.json'
import treeRoot from './treeData.json'
const compressSingletonFolder = true
const restoreExpandedFolders = true
const visibleNodesGenerator = new VisibleNodesGenerator({
root: treeRoot as TreeNode,
compress: compressSingletonFolder,
async getTreeData() {
throw new Error(`\`getTreeData\` should not be called`)
},
})
const search = (searchKey: string, searchMode: 'regex' | 'fuzzy') =>
new Promise<VisibleNodes>(resolve => {
const cancel = visibleNodesGenerator.onUpdate(visibleNodes => {
cancel()
resolve(visibleNodes)
})
visibleNodesGenerator.search(
searchModes[searchMode].getSearchParams(searchKey),
restoreExpandedFolders,
)
})
// In Jest, `it`s does not handle async flow as expected,
// there will be errors if the `it`s are in the same `describe`.
// But `describes` run in sequence.
describe('VisibleNodesGenerator Regex Search', () => {
it('finds all nodes when searching regexp /./', async () => {
for (const node of visibleNodesGenerator.visibleNodes.nodes) {
await visibleNodesGenerator.toggleExpand(node, true)
}
const initialVisibleNodes = visibleNodesGenerator.visibleNodes
expect(prepareVisibleNodes(await search('.', 'regex')).nodes).toEqual(
prepareVisibleNodes(initialVisibleNodes).nodes,
)
})
})
describe('VisibleNodesGenerator Fuzzy Search', () => {
it('finds correct nodes when searching path `json`', async () => {
expect(prepareVisibleNodes(await search('json', 'fuzzy'))).toEqual(searchResult__json)
})
})

View file

@ -0,0 +1,86 @@
import { VisibleNodes } from '.'
type SimpleTreeNode =
| [string]
| [
string, // the path
SimpleTreeNode[], // the contents
]
export const prepareNode = (node: VisibleNodes['nodes'][number]): SimpleTreeNode =>
node.contents ? [node.path, node.contents.map(prepareNode)] : [node.path]
export const prepareMap = <K, T>(map: Map<K, T>) => Array.from(map.entries())
export const prepareSet = <T>(set: Set<T>) => Array.from(set.values())
export const prepareVisibleNodes = ({
depths,
expandedNodes,
focusedNode,
loading,
nodes,
}: VisibleNodes) => ({
focusedNode: focusedNode && prepareNode(focusedNode),
depths: prepareMap(depths).map(([node, depth]) => [prepareNode(node), depth]),
expandedNodes: prepareSet(expandedNodes),
loading: prepareSet(loading),
nodes: nodes.map(prepareNode),
})
function recursiveMarkDiff(
node: SimpleTreeNode,
state: 'lack' | 'extra' | 'diff',
markDiff: (path: string, state: 'lack' | 'extra' | 'diff') => void,
) {
if (node.length === 1) {
const [path] = node
markDiff(path, state)
} else {
const [name, children] = node
for (const child of children) {
recursiveMarkDiff(child, state, markDiff)
}
}
}
export function diff(
a: SimpleTreeNode,
b: SimpleTreeNode,
markDiff: (path: string, state: 'lack' | 'extra' | 'diff') => void,
): boolean {
let isDiff = false
if (a.length === 2 && b.length === 2) {
const [, aContents] = a
const [, bContents] = b
for (const aContent of aContents) {
const [aPath] = aContent
for (const bContent of bContents) {
const [bPath] = bContent
if (aPath === bPath) {
isDiff = diff(aContent, bContent, markDiff) || isDiff
break
}
}
}
const aPaths = new Map<string, SimpleTreeNode>(aContents.map(content => [content[0], content]))
const bPaths = new Map<string, SimpleTreeNode>(bContents.map(content => [content[0], content]))
for (const [path, simpleTreeNode] of bPaths.entries()) {
if (!aPaths.has(path)) recursiveMarkDiff(simpleTreeNode, 'extra', markDiff)
}
for (const [path, simpleTreeNode] of aPaths.entries()) {
if (!bPaths.has(path)) recursiveMarkDiff(simpleTreeNode, 'lack', markDiff)
}
} else if (a.length === 2 && b.length === 1) {
isDiff = true
const [, aContents] = a
aContents.forEach(aContent => recursiveMarkDiff(aContent, 'lack', markDiff))
} else if (a.length === 1 && b.length === 2) {
isDiff = true
const [, bContents] = b
bContents.forEach(bContent => recursiveMarkDiff(bContent, 'extra', markDiff))
}
if (isDiff) markDiff(a[0], 'diff')
return isDiff
}

View file

@ -0,0 +1,244 @@
{
"focusedNode": null,
"depths": [
[
[
"",
[
[
"Safari/Gitako",
[
[
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets",
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"]
]
]
]
],
[
"__tests__",
[
["__tests__/jest.config.js"],
["__tests__/jest.non-parallel.config.js"],
["__tests__/jest.parallel.config.js"],
["__tests__/tsconfig.json"]
]
],
["scripts", [["scripts/language-id-ext.json"]]],
["server", [["server/package.json"], ["server/tsconfig.json"], ["server/vercel.json"]]],
["src", [["src/manifest.json"]]],
["jest-puppeteer.config.js"],
["jest.config.js"],
["package.json"],
["tsconfig.json"]
]
],
-1
],
[
[
"Safari/Gitako",
[
[
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets",
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"]
]
]
]
],
0
],
[
[
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"]]
],
1
],
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"], 2],
[
[
"Safari/Gitako/Gitako/Assets.xcassets",
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"]
]
],
1
],
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
2
],
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"], 3],
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
2
],
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"], 3],
[["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"], 2],
[
[
"__tests__",
[
["__tests__/jest.config.js"],
["__tests__/jest.non-parallel.config.js"],
["__tests__/jest.parallel.config.js"],
["__tests__/tsconfig.json"]
]
],
0
],
[["__tests__/jest.config.js"], 1],
[["__tests__/jest.non-parallel.config.js"], 1],
[["__tests__/jest.parallel.config.js"], 1],
[["__tests__/tsconfig.json"], 1],
[["scripts", [["scripts/language-id-ext.json"]]], 0],
[["scripts/language-id-ext.json"], 1],
[["server", [["server/package.json"], ["server/tsconfig.json"], ["server/vercel.json"]]], 0],
[["server/package.json"], 1],
[["server/tsconfig.json"], 1],
[["server/vercel.json"], 1],
[["src", [["src/manifest.json"]]], 0],
[["src/manifest.json"], 1],
[["jest-puppeteer.config.js"], 0],
[["jest.config.js"], 0],
[["package.json"], 0],
[["tsconfig.json"], 0]
],
"expandedNodes": [
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
"Safari/Gitako/Gitako.xcodeproj",
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
"Safari/Gitako/Gitako/Assets.xcassets",
"Safari/Gitako/Gitako",
"Safari/Gitako",
"Safari",
"__tests__",
"scripts",
"server",
"src",
""
],
"loading": [],
"nodes": [
[
"Safari/Gitako",
[
[
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets",
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"]
]
]
]
],
[
"Safari/Gitako/Gitako.xcodeproj/project.xcworkspace",
[["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"]]
],
["Safari/Gitako/Gitako.xcodeproj/project.xcworkspace/contents.xcworkspacedata"],
[
"Safari/Gitako/Gitako/Assets.xcassets",
[
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"]
]
],
[
"Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset",
[["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/AccentColor.colorset/Contents.json"],
[
"Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset",
[["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"]]
],
["Safari/Gitako/Gitako/Assets.xcassets/AppIcon.appiconset/Contents.json"],
["Safari/Gitako/Gitako/Assets.xcassets/Contents.json"],
[
"__tests__",
[
["__tests__/jest.config.js"],
["__tests__/jest.non-parallel.config.js"],
["__tests__/jest.parallel.config.js"],
["__tests__/tsconfig.json"]
]
],
["__tests__/jest.config.js"],
["__tests__/jest.non-parallel.config.js"],
["__tests__/jest.parallel.config.js"],
["__tests__/tsconfig.json"],
["scripts", [["scripts/language-id-ext.json"]]],
["scripts/language-id-ext.json"],
["server", [["server/package.json"], ["server/tsconfig.json"], ["server/vercel.json"]]],
["server/package.json"],
["server/tsconfig.json"],
["server/vercel.json"],
["src", [["src/manifest.json"]]],
["src/manifest.json"],
["jest-puppeteer.config.js"],
["jest.config.js"],
["package.json"],
["tsconfig.json"]
]
}

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,7 @@
"strict": true,
"lib": ["dom", "es2017.object", "es2016", "ES2020.String"],
"baseUrl": "src",
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"outDir": "dist"
},