Add stateful and stateless enterprise selection
This commit is contained in:
1 parent
d383b3aad8
commit
0959cef1cc
22 files changed
+609
-187
No files matched your search
@@ -2,10 +2,17 @@ import { afterEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { loadCorpConfigs, resolveEnterprisesDir } from "./loader.js";
|
||||
import {
|
||||
loadCorpConfigs,
|
||||
loadGlobalConfig,
|
||||
resolveEnterprisesDir,
|
||||
resolveGlobalConfigFile,
|
||||
saveGlobalConfig
|
||||
} from "./loader.js";
|
||||
|
||||
const ORIGINAL_ENV = process.env.SEAL_HOME_ENTERPRISES_DIR;
|
||||
const ORIGINAL_USER_DIR_ENV = process.env.SEAL_HOME_USER_ENTERPRISES_DIR;
|
||||
const ORIGINAL_CONFIG_FILE_ENV = process.env.SEAL_HOME_CONFIG_FILE;
|
||||
const ORIGINAL_CWD = process.cwd();
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
@@ -21,6 +28,11 @@ afterEach(() => {
|
||||
} else {
|
||||
process.env.SEAL_HOME_USER_ENTERPRISES_DIR = ORIGINAL_USER_DIR_ENV;
|
||||
}
|
||||
if (ORIGINAL_CONFIG_FILE_ENV === undefined) {
|
||||
delete process.env.SEAL_HOME_CONFIG_FILE;
|
||||
} else {
|
||||
process.env.SEAL_HOME_CONFIG_FILE = ORIGINAL_CONFIG_FILE_ENV;
|
||||
}
|
||||
|
||||
for (const dir of tempDirs.splice(0)) {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
@@ -69,6 +81,24 @@ describe("loadCorpConfigs", () => {
|
||||
["user-only", "User Only"]
|
||||
]);
|
||||
});
|
||||
|
||||
test("defaults global config to stateless", () => {
|
||||
process.env.SEAL_HOME_CONFIG_FILE = join(makeTempDir(), "config.json");
|
||||
|
||||
expect(loadGlobalConfig()).toEqual({ stateful: false });
|
||||
});
|
||||
|
||||
test("saves and loads the global state mode", () => {
|
||||
const file = join(makeTempDir(), "nested", "config.json");
|
||||
process.env.SEAL_HOME_CONFIG_FILE = file;
|
||||
|
||||
expect(saveGlobalConfig({ stateful: true, defaultCorpId: "corp-a" })).toEqual({
|
||||
stateful: true,
|
||||
defaultCorpId: "corp-a"
|
||||
});
|
||||
expect(resolveGlobalConfigFile()).toBe(file);
|
||||
expect(loadGlobalConfig()).toEqual({ stateful: true, defaultCorpId: "corp-a" });
|
||||
});
|
||||
});
|
||||
|
||||
function makeTempDir(): string {
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
||||
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
import { dirname, join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "enterprises";
|
||||
const CONFIG_ENV = "SEAL_HOME_ENTERPRISES_DIR";
|
||||
const USER_DIR_ENV = "SEAL_HOME_USER_ENTERPRISES_DIR";
|
||||
const GLOBAL_CONFIG_ENV = "SEAL_HOME_CONFIG_FILE";
|
||||
const GLOBAL_CONFIG_FILE = join(homedir(), ".config", "seal-home", "config.json");
|
||||
export const USER_ENTERPRISES_DIR = join(homedir(), ".config", "seal-home", "enterprises");
|
||||
|
||||
export function loadCorpConfigs(): CorpConfig[] {
|
||||
@@ -56,3 +58,24 @@ export function getEnterprisesDirCandidates(): string[] {
|
||||
USER_ENTERPRISES_DIR
|
||||
];
|
||||
}
|
||||
|
||||
export function resolveGlobalConfigFile(): string {
|
||||
return process.env[GLOBAL_CONFIG_ENV]?.trim() || GLOBAL_CONFIG_FILE;
|
||||
}
|
||||
|
||||
export function loadGlobalConfig(): GlobalConfig {
|
||||
const file = resolveGlobalConfigFile();
|
||||
if (!existsSync(file)) {
|
||||
return GlobalConfigSchema.parse({});
|
||||
}
|
||||
|
||||
return GlobalConfigSchema.parse(JSON.parse(readFileSync(file, "utf-8")));
|
||||
}
|
||||
|
||||
export function saveGlobalConfig(config: GlobalConfig): GlobalConfig {
|
||||
const normalized = GlobalConfigSchema.parse(config);
|
||||
const file = resolveGlobalConfigFile();
|
||||
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
||||
writeFileSync(file, `${JSON.stringify(normalized, null, 2)}\n`, { mode: 0o600 });
|
||||
return normalized;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CorpConfig } from "./types.js";
|
||||
import { CorpContext } from "./manager.js";
|
||||
import { CorpContext, resolveCorpConfig } from "./manager.js";
|
||||
|
||||
function corp(id: string, name = id): CorpConfig {
|
||||
return {
|
||||
@@ -26,7 +26,7 @@ function corp(id: string, name = id): CorpConfig {
|
||||
|
||||
describe("CorpContext", () => {
|
||||
test("refresh adds newly loaded corps", () => {
|
||||
const context = new CorpContext([corp("corp-a")]);
|
||||
const context = new CorpContext([corp("corp-a")], { stateful: true });
|
||||
|
||||
context.refresh([corp("corp-a"), corp("corp-b")]);
|
||||
|
||||
@@ -34,7 +34,7 @@ describe("CorpContext", () => {
|
||||
});
|
||||
|
||||
test("refresh keeps the selected corp when it still exists", () => {
|
||||
const context = new CorpContext([corp("corp-a"), corp("corp-b")]);
|
||||
const context = new CorpContext([corp("corp-a"), corp("corp-b")], { stateful: true });
|
||||
expect(context.switchCorp("corp-b")).toBe(true);
|
||||
|
||||
context.refresh([corp("corp-a"), corp("corp-b", "Corp B updated")]);
|
||||
@@ -43,12 +43,51 @@ describe("CorpContext", () => {
|
||||
expect(context.getCurrent()?.name).toBe("Corp B updated");
|
||||
});
|
||||
|
||||
test("refresh falls back when the selected corp disappears", () => {
|
||||
const context = new CorpContext([corp("corp-a"), corp("corp-b")]);
|
||||
test("clears the default when the selected corp disappears", () => {
|
||||
const context = new CorpContext([corp("corp-a"), corp("corp-b")], { stateful: true });
|
||||
expect(context.switchCorp("corp-b")).toBe(true);
|
||||
|
||||
context.refresh([corp("corp-c")]);
|
||||
|
||||
expect(context.getCurrent()?.id).toBe("corp-c");
|
||||
expect(context.getCurrent()).toBeNull();
|
||||
});
|
||||
|
||||
test("does not keep a current corp in stateless mode", () => {
|
||||
const context = new CorpContext([corp("corp-a"), corp("corp-b")]);
|
||||
|
||||
expect(context.getCurrent()).toBeNull();
|
||||
expect(context.switchCorp("corp-b")).toBe(false);
|
||||
expect(context.resolve("corp-b")?.id).toBe("corp-b");
|
||||
});
|
||||
|
||||
test("uses the configured default corp in stateful mode", () => {
|
||||
const context = new CorpContext(
|
||||
[corp("corp-a"), corp("corp-b")],
|
||||
{ stateful: true, defaultCorpId: "corp-b" }
|
||||
);
|
||||
|
||||
expect(context.getCurrent()?.id).toBe("corp-b");
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveCorpConfig", () => {
|
||||
test("requires an explicit corp in stateless mode", () => {
|
||||
expect(() => resolveCorpConfig([corp("corp-a")], { stateful: false })).toThrow(
|
||||
"Every enterprise command requires --corp <corpId>"
|
||||
);
|
||||
});
|
||||
|
||||
test("resolves an explicit corp in stateless mode", () => {
|
||||
expect(resolveCorpConfig(
|
||||
[corp("corp-a"), corp("corp-b")],
|
||||
{ stateful: false, corpId: "corp-b" }
|
||||
).id).toBe("corp-b");
|
||||
});
|
||||
|
||||
test("resolves the configured default corp in stateful mode", () => {
|
||||
expect(resolveCorpConfig(
|
||||
[corp("corp-a"), corp("corp-b")],
|
||||
{ stateful: true, defaultCorpId: "corp-b" }
|
||||
).id).toBe("corp-b");
|
||||
});
|
||||
});
|
||||
@@ -1,19 +1,62 @@
|
||||
import { CorpConfig } from "./types.js";
|
||||
import { CorpConfig, GlobalConfig } from "./types.js";
|
||||
|
||||
export function resolveCorpConfig(
|
||||
corps: CorpConfig[],
|
||||
options: Pick<GlobalConfig, "stateful" | "defaultCorpId"> & { corpId?: string },
|
||||
candidates: string[] = []
|
||||
): CorpConfig {
|
||||
const requestedCorpId = options.corpId?.trim() || undefined;
|
||||
const configuredCorpId = options.stateful
|
||||
? options.defaultCorpId?.trim() || undefined
|
||||
: undefined;
|
||||
const selectedCorpId = requestedCorpId ?? configuredCorpId;
|
||||
|
||||
if (selectedCorpId) {
|
||||
const corp = corps.find((item) => item.id === selectedCorpId);
|
||||
if (corp) return corp;
|
||||
throw new Error(`No enterprise config found for ${selectedCorpId}`);
|
||||
}
|
||||
|
||||
if (!options.stateful) {
|
||||
throw new Error(
|
||||
"Stateless mode is enabled. Every enterprise command requires --corp <corpId>. " +
|
||||
"Use `seal-home config mode stateful` to enable a default enterprise."
|
||||
);
|
||||
}
|
||||
|
||||
const firstCorp = corps[0];
|
||||
if (firstCorp) return firstCorp;
|
||||
|
||||
throw new Error(
|
||||
"No enterprise config found. Add a non-example JSON file under one of: " +
|
||||
(candidates.length ? candidates.join(", ") : "the configured enterprise directories")
|
||||
);
|
||||
}
|
||||
|
||||
export type CorpContextOptions = Pick<GlobalConfig, "stateful" | "defaultCorpId">;
|
||||
|
||||
export class CorpContext {
|
||||
private current: CorpConfig | null = null;
|
||||
private corps = new Map<string, CorpConfig>();
|
||||
private stateful = false;
|
||||
private defaultCorpId: string | undefined;
|
||||
|
||||
constructor(corps: CorpConfig[]) {
|
||||
constructor(corps: CorpConfig[], options: CorpContextOptions = { stateful: false }) {
|
||||
this.stateful = options.stateful;
|
||||
this.defaultCorpId = options.defaultCorpId;
|
||||
this.replaceCorps(corps);
|
||||
this.current = corps[0] ?? null;
|
||||
this.selectCurrent(corps[0]);
|
||||
}
|
||||
|
||||
refresh(corps: CorpConfig[]): void {
|
||||
const currentId = this.current?.id ?? null;
|
||||
refresh(corps: CorpConfig[], options: CorpContextOptions = {
|
||||
stateful: this.stateful,
|
||||
defaultCorpId: this.defaultCorpId
|
||||
}): void {
|
||||
const currentId = this.current?.id;
|
||||
this.stateful = options.stateful;
|
||||
this.defaultCorpId = options.defaultCorpId;
|
||||
this.replaceCorps(corps);
|
||||
this.current = currentId ? this.corps.get(currentId) ?? null : null;
|
||||
this.current ??= corps[0] ?? null;
|
||||
this.selectCurrent(currentId ? this.corps.get(currentId) ?? corps[0] : corps[0]);
|
||||
}
|
||||
|
||||
private replaceCorps(corps: CorpConfig[]): void {
|
||||
@@ -27,10 +70,21 @@ export class CorpContext {
|
||||
return this.current;
|
||||
}
|
||||
|
||||
getCorp(corpId: string): CorpConfig | null {
|
||||
return this.corps.get(corpId) ?? null;
|
||||
}
|
||||
|
||||
resolve(corpId?: string): CorpConfig | null {
|
||||
if (corpId) return this.getCorp(corpId);
|
||||
return this.current;
|
||||
}
|
||||
|
||||
switchCorp(corpId: string): boolean {
|
||||
if (!this.stateful) return false;
|
||||
const corp = this.corps.get(corpId);
|
||||
if (!corp) return false;
|
||||
this.current = corp;
|
||||
this.defaultCorpId = corp.id;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -42,4 +96,15 @@ export class CorpContext {
|
||||
current: corp.id === this.current?.id
|
||||
}));
|
||||
}
|
||||
|
||||
private selectCurrent(fallback?: CorpConfig): void {
|
||||
if (!this.stateful) {
|
||||
this.current = null;
|
||||
return;
|
||||
}
|
||||
|
||||
this.current = this.defaultCorpId !== undefined
|
||||
? this.corps.get(this.defaultCorpId) ?? null
|
||||
: fallback ?? null;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { CorpConfigSchema } from "./types.js";
|
||||
import { CorpConfigSchema, GlobalConfigSchema } from "./types.js";
|
||||
|
||||
describe("CorpConfigSchema", () => {
|
||||
test("accepts Hose key/password aliases", () => {
|
||||
@@ -79,3 +79,19 @@ describe("CorpConfigSchema", () => {
|
||||
expect(config.source.tenantSlug).toBe("xiezihao");
|
||||
});
|
||||
});
|
||||
|
||||
describe("GlobalConfigSchema", () => {
|
||||
test("defaults to stateless mode", () => {
|
||||
expect(GlobalConfigSchema.parse({})).toEqual({ stateful: false });
|
||||
});
|
||||
|
||||
test("accepts a stateful default enterprise", () => {
|
||||
expect(GlobalConfigSchema.parse({
|
||||
stateful: true,
|
||||
defaultCorpId: "corp-a"
|
||||
})).toEqual({
|
||||
stateful: true,
|
||||
defaultCorpId: "corp-a"
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -84,9 +84,15 @@ export const CorpConfigSchema = z.object({
|
||||
auth: CorpAuthConfigSchema
|
||||
});
|
||||
|
||||
export const GlobalConfigSchema = z.object({
|
||||
stateful: z.boolean().default(false),
|
||||
defaultCorpId: z.string().min(1).optional()
|
||||
});
|
||||
|
||||
export type HoseSealSource = z.infer<typeof HoseSealSourceSchema>;
|
||||
export type DirectSealSource = z.infer<typeof DirectSealSourceSchema>;
|
||||
export type NativeSealSource = z.infer<typeof NativeSealSourceSchema>;
|
||||
export type SealSource = z.infer<typeof SealSourceSchema>;
|
||||
export type CorpAuthConfig = z.infer<typeof CorpAuthConfigSchema>;
|
||||
export type CorpConfig = z.infer<typeof CorpConfigSchema>;
|
||||
export type GlobalConfig = z.infer<typeof GlobalConfigSchema>;
|
||||
Reference in new issue
Block a user