Refactor user and instance manager (#837)

* Refactor UserManager to use Datastore

* Rename lib.User to UserDetails also adds to/fromUserDetails

* Fix existing tests

* Add user class tests

* Add tests for finaliseUsers

* Move HostDetails to own file with tests

* Move InstanceDetails to its own file with tests

* Rename HostInfo and add tests

* Rename InstanceInfo and add tests

- Also changes the names of the host properties related to instances
- Also removes the need to specify the location of InstanceInfo

* Make json round trip test validate schema

* Add InstanceManager

* Fix lint errors

* Extra tests

* Add final test coverage

* Add one last test to InstanceManager

* Response to comments

* Use generator rather than return anonumous generator

Co-authored-by: Hornwitser <github@hornwitser.no>

* Fix lint issue with generator

---------

Co-authored-by: Hornwitser <github@hornwitser.no>
This commit is contained in:
Cooldude2606andHornwitser authored and GitHub committed 2026-03-01 23:04:37 +00:00
1 parent 5bea930d05
commit 2235294c59
60 files changed
+3371 -1227

No files matched your search

+38 -7
View File
@@ -53,7 +53,7 @@ export class JsonDatastoreProvider<
private filePath: string,
private fromJson: (json: J) => V = v => v as any as V,
private migrations: (rawJson: Record<string, unknown>) => Record<string, J> = v => v as Record<string, J>,
private finalise: (obj: V) => V = v => v,
private finalise: (obj: V, map: Map<string, V>) => V | undefined = v => v,
) {
super();
}
@@ -82,7 +82,24 @@ export class JsonDatastoreProvider<
const serialized = this.migrations(rawJson);
// Convert to data class objects
return new Map(Object.entries(serialized).map(([k, v]) => [k, this.finalise(this.fromJson(v))]));
let serializedCount = 0;
const result = new Map<string, V>();
for (const [key, raw] of Object.entries(serialized)) {
const final = this.finalise(this.fromJson(raw), result);
if (final !== undefined) { result.set(key, final); }
serializedCount += 1;
}
// Save a backup if the map does not contain all values (happens where there are duplicates)
if (result.size < serializedCount) {
const backupPath = `${this.filePath}.${Date.now()}.bak`;
logger.warn(
`Duplicates detected in ${this.filePath}, a backup was written to: ${backupPath}`
);
await safeOutputFile(backupPath, JSON.stringify(rawJson, null, "\t"));
}
return result;
}
}
@@ -95,7 +112,7 @@ export class JsonIdDatastoreProvider<
private filePath: string,
private fromJson: (json: J) => V = v => v as any as V,
private migrations: (rawJson: Array<unknown>) => Array<J> = v => v as Array<J>,
private finalise: (obj: V) => V = v => v,
private finalise: (obj: V, map: Map<V["id"], V>) => V | undefined = v => v,
) {
super();
}
@@ -124,10 +141,24 @@ export class JsonIdDatastoreProvider<
const serialized = this.migrations(rawJson);
// Convert to data class objects
return new Map(serialized.map((e) => {
const v = this.finalise(this.fromJson(e));
return [v.id, v];
}));
let serializedCount = 0;
const result = new Map<V["id"], V>();
for (const raw of serialized) {
const final = this.finalise(this.fromJson(raw), result);
if (final !== undefined) { result.set(final.id, final); }
serializedCount += 1;
}
// Save a backup if the map does not contain all values (happens where there are duplicates)
if (result.size < serializedCount) {
const backupPath = `${this.filePath}.${Date.now()}.bak`;
logger.warn(
`Duplicates detected in ${this.filePath}, a backup was written to: ${backupPath}`
);
await safeOutputFile(backupPath, JSON.stringify(rawJson, null, "\t"));
}
return result;
}
}