Skip to content

Commit

Permalink
tests: clean up, add more test about merging
Browse files Browse the repository at this point in the history
  • Loading branch information
bodinsamuel committed May 3, 2023
1 parent f4d0f3a commit b0f26ce
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 55 deletions.
71 changes: 70 additions & 1 deletion src/payload/__snapshots__/helpers.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Payload {
}
`;

exports[`flatten > should flatten 1`] = `
exports[`flatten > should flatten and merge dependencies 1`] = `
{
"childs": [
{
Expand Down Expand Up @@ -154,3 +154,72 @@ exports[`flatten > should flatten 1`] = `
"techs": [],
}
`;

exports[`flatten > should flatten and merge host 1`] = `
{
"childs": [
{
"childs": [],
"dependencies": [],
"edges": [],
"group": "component",
"id": "2",
"inComponent": "4",
"languages": {},
"name": "2",
"path": [
"/",
],
"tech": null,
"techs": [],
},
{
"childs": [],
"dependencies": [],
"edges": [],
"group": "hosting",
"id": "4",
"inComponent": null,
"languages": {},
"name": "4",
"path": [
"/",
],
"tech": "vercel",
"techs": [
"vercel",
],
},
{
"childs": [],
"dependencies": [],
"edges": [],
"group": "component",
"id": "6",
"inComponent": "4",
"languages": {},
"name": "6",
"path": [
"/",
],
"tech": null,
"techs": [],
},
],
"dependencies": [],
"edges": [],
"group": "component",
"id": "new",
"inComponent": null,
"languages": {},
"name": "flatten",
"path": [
"/",
"",
],
"tech": null,
"techs": [
"vercel",
],
}
`;
46 changes: 45 additions & 1 deletion src/payload/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('findEdgesInDependencies', () => {
});

describe('flatten', () => {
it('should flatten', () => {
it('should flatten and merge dependencies', () => {
const main = new Payload({ id: '1', name: 'main', folderPath: '' });
const sub = new Payload({
id: '2',
Expand All @@ -48,4 +48,48 @@ describe('flatten', () => {

expect(flat.toJson()).toMatchSnapshot();
});

it('should flatten and merge host', () => {
const main = new Payload({ id: '1', name: 'main', folderPath: '' });

// first component
const host1 = new Payload({
id: '4',
name: '4',
folderPath: '/',
tech: 'vercel',
});
const sub1 = new Payload({
id: '2',
name: '2',
folderPath: '/',
});
sub1.childs.push(host1);
sub1.inComponent = host1;

// second component
const host2 = new Payload({
id: '5',
name: '5',
folderPath: '/',
tech: 'vercel',
});
const sub2 = new Payload({
id: '6',
name: '6',
folderPath: '/',
});
sub2.childs.push(host2);
sub2.inComponent = host2;

// Push everything into main
main.childs.push(sub1, sub2);

const flat = flatten(main);
flat.id = 'new';

expect(flat.toJson()).toMatchSnapshot();
expect(flat.childs[1].inComponent!.id).toEqual('4');
expect(flat.childs[2].inComponent!.id).toEqual('4');
});
});
60 changes: 7 additions & 53 deletions src/payload/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ export function findHosting(pl: Payload, tech: AllowedKeys) {
pl.inComponent = find;
}

/**
* Some edges can be found in the dependencies, i.e:
* - in monorepo or in some language you can have many folders and import another folder.
*
* We try to find those import, using only the dependencies (not opening the code),
* it can lead to some false positive with very generic names.
*/
export function findEdgesInDependencies(pl: Payload) {
const names = new Set<string>();
pl.childs.forEach((child) => names.add(child.name));
Expand Down Expand Up @@ -80,30 +87,6 @@ export function findEdgesInDependencies(pl: Payload) {
});
}

// TODO: is it still necessary?
export function findEdges(pl: Payload) {
pl.childs.forEach((component) => {
component.techs.forEach((tech) => {
const ref = listIndexed[tech];

if (
ref.type === 'sass' ||
ref.type === 'app' ||
ref.type === 'db' ||
ref.type === 'messaging'
) {
const find = pl.childs.find((c) => c.tech === ref.key);
if (!find) {
throw new Error(`cant find sass ${ref.key}`);
}
component.addEdges(find);

return;
}
});
});
}

function pushChids(src: Payload, dest: Payload) {
src.childs.forEach((pl) => {
const cp = pl.copy();
Expand Down Expand Up @@ -179,34 +162,5 @@ export function flatten(src: Payload): Payload {

dest.combine(src);

// for (let index = 0; index < dest.childs.length; index++) {
// const child = dest.childs[index];

// if (duplicates.includes(child.id)) {
// dest.childs.
// }
// }

// src.childs.forEach((component) => flatten(component, dest));
// src.techs.forEach((tech) => dest!.techs.add(tech));
// dest.dependencies = [...dest.dependencies, ...src.dependencies];

// if (src.tech) {
// dest.techs.add(src.tech);
// }

// for (const [lang, count] of Object.entries(src.languages)) {
// dest.addLang(lang, count);
// }

// const cp = src.copy();
// cp.childs = [];
// dest.addComponent(cp);

// cp.setParent(null);

// if (isRoot) {
// findEdgesInDependencies(dest);
// }
return dest;
}

0 comments on commit b0f26ce

Please sign in to comment.