1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 17:53:39 +00:00

refactor: introduce @bitwarden/state and other common libs (#15772)

* refactor: introduce @bitwarden/serialization

* refactor: introduce @bitwarden/guid

* refactor: introduce @bitwaren/client-type

* refactor: introduce @bitwarden/core-test-utils

* refactor: introduce @bitwarden/state and @bitwarden/state-test-utils

Creates initial project structure for centralized application state management. Part of modularization effort to extract state code from common.

* Added state provider documentation to README.

* Changed callouts to Github format.

* Fixed linting on file name.

* Forced git to accept rename

---------

Co-authored-by: Todd Martin <tmartin@bitwarden.com>
This commit is contained in:
Addison Beck
2025-08-04 11:01:28 -04:00
committed by GitHub
parent 5833ed459b
commit 361f7e3447
306 changed files with 4491 additions and 2702 deletions

View File

@@ -0,0 +1,60 @@
import { ClientLocations, StateDefinition } from "./state-definition";
import * as stateDefinitionsRecord from "./state-definitions";
describe.each(["web", "cli", "desktop", "browser"])(
"state definitions follow rules for client %s",
(clientType: keyof ClientLocations) => {
const trackedNames: [string, string][] = [];
test.each(Object.entries(stateDefinitionsRecord))(
"that export %s follows all rules",
(exportName, stateDefinition) => {
// All exports from state-definitions are expected to be StateDefinition's
if (!(stateDefinition instanceof StateDefinition)) {
throw new Error(`export ${exportName} is expected to be a StateDefinition`);
}
const storageLocation =
stateDefinition.storageLocationOverrides[clientType] ??
stateDefinition.defaultStorageLocation;
const fullName = `${stateDefinition.name}_${storageLocation}`;
const exactConflictingExport = trackedNames.find(
([_, trackedName]) => trackedName === fullName,
);
if (exactConflictingExport !== undefined) {
const [conflictingExportName] = exactConflictingExport;
throw new Error(
`The export '${exportName}' has a conflicting state name and storage location with export ` +
`'${conflictingExportName}' please ensure that you choose a unique name and location for all clients.`,
);
}
const roughConflictingExport = trackedNames.find(
([_, trackedName]) => trackedName.toLowerCase() === fullName.toLowerCase(),
);
if (roughConflictingExport !== undefined) {
const [conflictingExportName] = roughConflictingExport;
throw new Error(
`The export '${exportName}' differs its state name and storage location ` +
`only by casing with export '${conflictingExportName}' please ensure it differs by more than casing.`,
);
}
const name = stateDefinition.name;
expect(name).not.toBeUndefined(); // undefined in an invalid name
expect(name).not.toBeNull(); // null is in invalid name
expect(name.length).toBeGreaterThan(3); // A 3 characters or less name is not descriptive enough
expect(name[0]).toEqual(name[0].toLowerCase()); // First character should be lower case since camelCase is required
expect(name).not.toContain(" "); // There should be no spaces in a state name
expect(name).not.toContain("_"); // We should not be doing snake_case for state name
// NOTE: We could expect some details about the export name as well
trackedNames.push([exportName, fullName]);
},
);
},
);