Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): show the saved Use Built-in flags on the infra page

Yudhi Armyndharis680c06be4d10
diff --git a/dashboard/src/hooks/useInfraConfigForm.test.ts b/dashboard/src/hooks/useInfraConfigForm.test.ts
index c16c1d7b7..85a05ca35 100644
--- a/dashboard/src/hooks/useInfraConfigForm.test.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.test.ts
@@ -166,47 +166,82 @@ const RUNNING_BUILTIN: InfraStatus = {
engine: { type: 'whatsapp-web.js', headless: true },
};
-test('a built-in flag the operator has not touched is saved as it is stored, not as it runs', () => {
- const { result } = renderForm({ status: RUNNING_BUILTIN, saved: SAVED });
- rtl.act(() => result.current.updateDbConfig('password', 'typed-db-secret'));
+// The operator saved bundled containers and has not restarted: external services run now.
+const STORED_BUILTIN: SavedConfig = {
+ ...SAVED,
+ database: { ...SAVED.database, builtIn: true },
+ redis: { ...SAVED.redis, builtIn: true },
+ storage: { ...SAVED.storage, builtIn: true },
+};
- const { database, redis, storage } = result.current.buildSavePayload();
+const shownFlags = (form: ReturnType<typeof renderForm>['result']['current']) => [
+ form.dbConfig.builtIn,
+ form.redisConfig.builtIn,
+ form.storageConfig.builtIn,
+];
+
+// The toggles are the saved choice like every other selection. Shown from what runs, one that reads "external"
+// invites typing an external host and credentials that the gateway then drops under the stored bundled mode.
+test('the built-in toggles show what was saved, not what runs', () => {
+ const external = renderForm({ status: RUNNING_BUILTIN, saved: SAVED });
+ assert.deepEqual(shownFlags(external.result.current), [false, false, false]);
+ rtl.cleanup();
+
+ const bundled = renderForm({ status: RUNNING, saved: STORED_BUILTIN });
+ assert.deepEqual(shownFlags(bundled.result.current), [true, true, true]);
+});
+
+for (const order of ['status first', 'config first'] as const) {
+ test(`the built-in toggles show what was saved when the reads settle ${order}`, () => {
+ const { result, rerender } = renderForm(order === 'status first' ? { status: RUNNING } : { saved: STORED_BUILTIN });
+ rerender({ status: RUNNING, saved: STORED_BUILTIN });
+
+ assert.deepEqual(shownFlags(result.current), [true, true, true]);
+ });
+}
+
+test('a save carries the built-in flag the form shows', () => {
+ for (const [status, saved] of [
+ [RUNNING_BUILTIN, SAVED],
+ [RUNNING, STORED_BUILTIN],
+ ] as const) {
+ const { result } = renderForm({ status, saved });
+ rtl.act(() => result.current.updateDbConfig('host', 'db.external.example'));
+
+ const { database, redis, storage } = result.current.buildSavePayload();
- assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], [false, false, false]);
- // Still an external database, so what was typed for it is sent.
- assert.equal(database?.password, 'typed-db-secret');
+ assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], shownFlags(result.current));
+ rtl.cleanup();
+ }
});
-test('a stored built-in container is kept when the page runs an external one, and never gets a typed password', () => {
- const storedBuiltIn: SavedConfig = {
- ...SAVED,
- database: { ...SAVED.database, builtIn: true },
- redis: { ...SAVED.redis, builtIn: true },
- storage: { ...SAVED.storage, builtIn: true },
- };
- const { result } = renderForm({ status: RUNNING, saved: storedBuiltIn });
+test('external values typed after flipping a stored bundled container off are saved with the external flag', () => {
+ const { result } = renderForm({ status: RUNNING, saved: STORED_BUILTIN });
rtl.act(() => {
- result.current.updateDbConfig('password', 'typed-db-secret');
- result.current.updateRedisConfig('password', 'typed-redis-secret');
+ result.current.updateDbConfig('builtIn', false);
+ result.current.updateDbConfig('host', 'db.external.example');
+ result.current.updateStorageConfig('builtIn', false);
+ result.current.updateStorageConfig('s3Bucket', 'ext-bucket');
});
- const { database, redis, storage } = result.current.buildSavePayload();
+ const { database, storage } = result.current.buildSavePayload();
- assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], [true, true, true]);
- assert.deepEqual([database?.password, redis?.password], ['', '']);
+ assert.deepEqual(
+ [database?.builtIn, database?.host, storage?.builtIn, storage?.s3Bucket],
+ [false, 'db.external.example', false, 'ext-bucket'],
+ );
});
-test('a built-in flag the operator flipped is saved as flipped', () => {
- const { result } = renderForm({ status: RUNNING_BUILTIN, saved: SAVED });
+test('a bundled container never gets a typed password', () => {
+ const { result } = renderForm({ status: RUNNING, saved: STORED_BUILTIN });
rtl.act(() => {
- result.current.updateDbConfig('builtIn', true);
- result.current.updateRedisConfig('builtIn', true);
- result.current.updateStorageConfig('builtIn', true);
+ result.current.updateDbConfig('password', 'typed-db-secret');
+ result.current.updateRedisConfig('password', 'typed-redis-secret');
});
- const { database, redis, storage } = result.current.buildSavePayload();
+ const { database, redis } = result.current.buildSavePayload();
- assert.deepEqual([database?.builtIn, redis?.builtIn, storage?.builtIn], [true, true, true]);
+ assert.deepEqual([database?.password, redis?.password], ['', '']);
});
test('a refetch after the form is seeded does not overwrite an edit in progress', () => {
diff --git a/dashboard/src/hooks/useInfraConfigForm.ts b/dashboard/src/hooks/useInfraConfigForm.ts
index b6bd01539..98d196df8 100644
--- a/dashboard/src/hooks/useInfraConfigForm.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.ts
@@ -122,10 +122,6 @@ export function useInfraConfigForm(
// operator's in-progress, unsaved edits. A successful save restarts → full page reload, re-arming it.
const formHydrated = useRef(false);
- // The built-in flags are seeded from what is RUNNING, so until the operator flips one it is an
- // observation, not a choice. A save then carries the stored flag instead (see builtInToSave).
- const builtInTouched = useRef({ database: false, redis: false, storage: false });
-
// The engine radio seeds ONCE from the saved file (see the effect below), then is never re-stamped by
// a background refetch. `engineTouched` additionally wins over a late first resolution: if the
// operator clicked a different engine before the seed resolved, the delayed seed must not revert
@@ -141,11 +137,13 @@ export function useInfraConfigForm(
// operator's intent, so it supplies every selection and detail field: the running process is stale from
// the moment a change is saved until the server restarts, and a form seeded from it writes that stale
// value back over the saved one on the next save of ANY field, deleting the stored settings of the
- // service it switched away from (the engine radio below learned the same, #1082). Only what the saved
- // config cannot say comes from the live /status: whether a bundled container is actually running (#488),
- // and a host, port or path nothing has saved. One effect once both exist also keeps the result from
- // depending on which read settled last. Secrets are never returned, so their inputs stay empty; an empty
- // submit preserves the stored secret on the backend (#226).
+ // service it switched away from (the engine radio below learned the same, #1082). That holds for the Use
+ // Built-in toggles too: shown from what runs, one that reads "external" invites typing an external host
+ // and credentials that the gateway drops under the stored bundled mode. What is running is reported by the
+ // badges and the pending-restart notes. Only a host, port or path nothing has saved comes from the live
+ // /status. One effect once both exist also keeps the result from depending on which read settled last.
+ // Secrets are never returned, so their inputs stay empty; an empty submit preserves the stored secret on
+ // the backend (#226).
useEffect(() => {
if (!infraStatus || !savedConfig || formHydrated.current) return;
formHydrated.current = true;
@@ -161,14 +159,13 @@ export function useInfraConfigForm(
poolSize: database.poolSize,
sslEnabled: database.sslEnabled,
sslRejectUnauthorized: database.sslRejectUnauthorized,
- // builtIn reflects whether OpenWA's bundled container is actually running (live), not saved intent.
- builtIn: infraStatus.database.builtIn,
+ builtIn: database.builtIn,
}));
setRedisConfig(prev => ({
...prev,
host: redis.host || infraStatus.redis.host,
port: redis.port || String(infraStatus.redis.port),
- builtIn: infraStatus.redis.builtIn,
+ builtIn: redis.builtIn,
}));
setRedisEnabledState(redis.enabled);
setQueueEnabled(queue.enabled);
@@ -179,7 +176,7 @@ export function useInfraConfigForm(
s3Bucket: storage.s3Bucket || prev.s3Bucket,
s3Region: storage.s3Region || prev.s3Region,
s3Endpoint: storage.s3Endpoint || prev.s3Endpoint,
- builtIn: infraStatus.storage.builtIn,
+ builtIn: storage.builtIn,
}));
setEngineConfig(prev => ({
...prev,
@@ -205,18 +202,12 @@ export function useInfraConfigForm(
setEngineConfig(prev => (prev.type === seed ? prev : { ...prev, type: seed }));
}, [savedConfig]);
- const updateDbConfig = (key: keyof DatabaseConfig, value: string | number | boolean) => {
- if (key === 'builtIn') builtInTouched.current.database = true;
+ const updateDbConfig = (key: keyof DatabaseConfig, value: string | number | boolean) =>
setDbConfig(prev => ({ ...prev, [key]: value }));
- };
- const updateRedisConfig = (key: keyof RedisConfig, value: string | boolean) => {
- if (key === 'builtIn') builtInTouched.current.redis = true;
+ const updateRedisConfig = (key: keyof RedisConfig, value: string | boolean) =>
setRedisConfig(prev => ({ ...prev, [key]: value }));
- };
- const updateStorageConfig = (key: keyof StorageConfig, value: string | boolean) => {
- if (key === 'builtIn') builtInTouched.current.storage = true;
+ const updateStorageConfig = (key: keyof StorageConfig, value: string | boolean) =>
setStorageConfig(prev => ({ ...prev, [key]: value }));
- };
const updateEngineConfig = (key: keyof EngineConfig, value: string | boolean) => {
if (key === 'type') engineTouched.current = true;
setEngineConfig(prev => ({ ...prev, [key]: value }));
@@ -230,34 +221,26 @@ export function useInfraConfigForm(
const setRedisConnected = (connected: boolean) => setRedisConfig(prev => ({ ...prev, connected }));
- // The flag a save carries: the operator's flip, else the stored one. Until it is flipped, the flag shown
- // is what runs, and writing that back would turn a bundled service into an external one (or the reverse)
- // that was saved and is waiting for a restart, along with the credentials that service owns. It is always
- // sent, never left out: the backend builds the Docker profile list from it.
- const builtInToSave = (section: 'database' | 'redis' | 'storage', shown: boolean): boolean =>
- builtInTouched.current[section] ? shown : (savedConfig?.[section].builtIn ?? shown);
-
const buildSavePayload = (): SaveConfigPayload => ({
// A bundled container never receives a typed password (the field is hidden once built-in is on,
// but a value typed before the toggle survives in state). '' means "unchanged" on the backend.
// An emptied pool size is NaN, which JSON writes as null and the backend refuses: leave it out.
database: {
...dbConfig,
- builtIn: builtInToSave('database', dbConfig.builtIn),
- password: builtInToSave('database', dbConfig.builtIn) ? '' : dbConfig.password,
+ password: dbConfig.builtIn ? '' : dbConfig.password,
poolSize: Number.isInteger(dbConfig.poolSize) ? dbConfig.poolSize : undefined,
},
// `connected` is runtime-only status, not persisted configuration. Keep it out of the
// whitelisted backend DTO so a valid dashboard save cannot be rejected as an unknown field.
redis: {
enabled: redisEnabled,
- builtIn: builtInToSave('redis', redisConfig.builtIn),
+ builtIn: redisConfig.builtIn,
host: redisConfig.host,
port: redisConfig.port,
- password: builtInToSave('redis', redisConfig.builtIn) ? '' : redisConfig.password,
+ password: redisConfig.builtIn ? '' : redisConfig.password,
},
queue: { enabled: queueEnabled },
- storage: { ...storageConfig, builtIn: builtInToSave('storage', storageConfig.builtIn) },
+ storage: { ...storageConfig },
// Only send `type` once we actually know it — either the radio seeded from /config or the
// operator picked one. If /config never resolved, engineConfig.type still holds its useState
// default, and sending that would persist ENGINE_TYPE and silently flip the engine on the next
diff --git a/dashboard/src/pages/Infrastructure.test.ts b/dashboard/src/pages/Infrastructure.test.ts
index c70d75708..eb11aa4a7 100644
--- a/dashboard/src/pages/Infrastructure.test.ts
+++ b/dashboard/src/pages/Infrastructure.test.ts
@@ -712,6 +712,88 @@ test('a queue switch saved and left for a restart shows the pending-restart note
assert.equal(screen.getAllByText(PENDING_RESTART_NOTE).length, 1, 'the note belongs to the queue alone');
});
+// ── A built-in switch saved and not restarted for ────────────────────────────
+// Same service, other mode: the type is unchanged, so only the Use Built-in toggle differs from what runs.
+
+const STORAGE_WARNING = 'Existing media files are not moved to the new storage backend';
+
+const RUNNING_EXTERNAL: InfraStatus = {
+ ...INFRA_STATUS,
+ storage: { type: 's3', bucket: 'media', builtIn: false, s3Available: true },
+};
+const SAVED_BUNDLED: SavedConfig = {
+ ...SAVED_CONFIG,
+ database: { ...SAVED_CONFIG.database, builtIn: true },
+ storage: { ...SAVED_CONFIG.storage, type: 's3', builtIn: true },
+};
+const RUNNING_BUNDLED: InfraStatus = {
+ ...INFRA_STATUS,
+ database: { connected: true, type: 'postgres', host: 'postgres', builtIn: true },
+ redis: { enabled: true, connected: true, host: 'redis', port: 6379, builtIn: true },
+ storage: { type: 's3', bucket: 'openwa', builtIn: true, s3Available: true },
+};
+const SAVED_EXTERNAL: SavedConfig = {
+ ...SAVED_CONFIG,
+ redis: { enabled: true, builtIn: false, host: 'redis.internal', port: '6379', passwordSet: false },
+ storage: { ...SAVED_CONFIG.storage, type: 's3', builtIn: false },
+};
+
+test('the built-in toggles show what was saved, and a later save warns of the switch it applies', async () => {
+ const { screen, fireEvent, within } = rtl;
+ resetFetchCalls();
+ overrides = { status: RUNNING_EXTERNAL, saved: SAVED_BUNDLED };
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitSeeded(container);
+ assert.equal(toggleInput(container, 'Use Built-in PostgreSQL Container').checked, true);
+ assert.equal(toggleInput(container, 'Use Built-in MinIO Container').checked, true);
+
+ fireEvent.click(toggleInput(container, 'Headless Mode'));
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+
+ const dialog = await screen.findByRole('dialog');
+ within(dialog).getByText(DB_SWITCH_WARNING, { exact: false });
+ within(dialog).getByText(STORAGE_WARNING, { exact: false });
+ const body = findFetchCall('PUT', '/api/infra/config')!.body as {
+ database?: { builtIn?: boolean };
+ storage?: { builtIn?: boolean };
+ };
+ assert.deepEqual([body.database?.builtIn, body.storage?.builtIn], [true, true]);
+});
+
+test('a switch from bundled to external services is warned of and stops the bundled containers', async () => {
+ const { screen, waitFor, fireEvent, within } = rtl;
+ resetFetchCalls();
+ overrides = { status: RUNNING_BUNDLED, saved: SAVED_EXTERNAL };
+ const { container } = renderInfrastructure();
+
+ await screen.findByText('Database Configuration');
+ await awaitSeeded(container);
+ assert.equal(toggleInput(container, 'Use Built-in PostgreSQL Container').checked, false);
+ assert.equal(toggleInput(container, 'Use Built-in Redis Container').checked, false);
+ assert.equal(toggleInput(container, 'Use Built-in MinIO Container').checked, false);
+
+ fireEvent.click(toggleInput(container, 'Headless Mode'));
+ fireEvent.click(screen.getByRole('button', { name: 'Save Configuration' }));
+
+ const dialog = await screen.findByRole('dialog');
+ within(dialog).getByText(DB_SWITCH_WARNING, { exact: false });
+ const body = findFetchCall('PUT', '/api/infra/config')!.body as {
+ database?: { builtIn?: boolean };
+ redis?: { builtIn?: boolean };
+ storage?: { builtIn?: boolean };
+ };
+ assert.deepEqual([body.database?.builtIn, body.redis?.builtIn, body.storage?.builtIn], [false, false, false]);
+
+ fireEvent.click(within(dialog).getByRole('button', { name: 'Restart Now' }));
+ await waitFor(() => assert.ok(findFetchCall('POST', '/api/infra/restart'), 'expected the restart POST'));
+ assert.deepEqual(findFetchCall('POST', '/api/infra/restart')!.body, {
+ profiles: [],
+ profilesToRemove: ['postgres', 'redis', 'minio'],
+ });
+});
+
test('the pending-restart note survives a successful save', async () => {
const { screen, waitFor, fireEvent } = rtl;
resetFetchCalls();