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
12 changes: 11 additions & 1 deletion scripts/import/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,21 @@ function fillGaps(brand, current, { aliases, bootloader = {}, ...rest }) {
claim(brand, newAliases);
}

/** Lenovo's "ZUK Z2 Plus" is ZUK's Z2 Plus, as ZUK is a brand of its own here */
function ownBrand(vendor, device) {
const [first, ...rest] = device.name.trim().split(/\s+/);
const brands = read(join(DATA, 'brands.yml')) ?? [];
const owner = brands.find(({ key }) => key === slugify(first));
if (!owner || !rest.length || owner.key === brandKey(vendor)) return [vendor, device];
return [owner.name, { ...device, name: rest.join(' ') }];
}

/**
* Finds or creates a device, only ever filling fields that are missing or unknown.
* Returns its brand/codename, or null when it's out of scope
*/
export function upsertDevice(brandName, device) {
export function upsertDevice(vendor, imported) {
const [brandName, device] = ownBrand(vendor, imported);
const brand = brandKey(brandName);
const id = `${brand}/${device.codenames[0]}`;
if (device.type === 'skip') outOfScope.add(id);
Expand Down
6 changes: 6 additions & 0 deletions tests/import/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ test('a codename another brand already uses gets a device of its own', () => {
assert.ok(existsSync(join(process.env.DATA_DIR, 'devices/tidy/twin.yml')));
});

test('a name that opens with another brand we list goes to that brand', () => {
upsertDevice('Acme', { name: 'Rocket', codenames: ['rocket'] });
assert.equal(upsertDevice('Tidy', { name: 'Acme Rocket', codenames: ['rocket'] }), 'acme/rocket');
assert.ok(!existsSync(join(process.env.DATA_DIR, 'devices/tidy/rocket.yml')));
});

test('a source that loses a fifth of its devices fails and leaves the file alone', () => {
writeUpstream('floor', rows(10));
assert.throws(() => writeUpstream('floor', rows(7)), /only 7 active devices, last import had 10/);
Expand Down
Loading