Emit canonical platform context preference payload

This commit is contained in:
master
2026-03-07 04:26:40 +02:00
parent edb947d602
commit 4bec133724
3 changed files with 190 additions and 7 deletions

View File

@@ -0,0 +1,100 @@
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http';
import { HttpTestingController, provideHttpClientTesting } from '@angular/common/http/testing';
import { TestBed } from '@angular/core/testing';
import { PlatformContextStore } from './platform-context.store';
describe('PlatformContextStore', () => {
let store: PlatformContextStore;
let httpMock: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
PlatformContextStore,
provideHttpClient(withInterceptorsFromDi()),
provideHttpClientTesting(),
],
});
store = TestBed.inject(PlatformContextStore);
httpMock = TestBed.inject(HttpTestingController);
(store as any).apiDisabled = false;
(store as any).persistPaused = false;
store.error.set(null);
});
afterEach(() => {
httpMock.verify();
});
it('persists only the canonical Platform context payload fields', () => {
store.selectedRegions.set(['us-east']);
store.selectedEnvironments.set(['dev']);
store.timeWindow.set('24h');
store.stage.set('all');
store.tenantId.set('demo-prod');
(store as any).persistPreferences();
const req = httpMock.expectOne('/api/v2/context/preferences');
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual({
regions: ['us-east'],
environments: ['dev'],
timeWindow: '24h',
});
expect(req.request.body.tenantId).toBeUndefined();
expect(req.request.body.stage).toBeUndefined();
req.flush({
tenantId: 'demo-prod',
actorId: 'context-tests',
regions: ['us-east'],
environments: ['dev'],
timeWindow: '24h',
updatedAt: '2026-03-07T00:00:00Z',
updatedBy: 'context-tests',
});
});
it('persists environment-only query scope without unsupported fields', () => {
store.initialized.set(true);
store.environments.set([
{
environmentId: 'dev',
regionId: 'us-east',
environmentType: 'development',
displayName: 'Development',
sortOrder: 10,
enabled: true,
},
]);
store.applyScopeQueryParams({ environment: 'dev' });
const req = httpMock.expectOne('/api/v2/context/preferences');
expect(req.request.method).toBe('PUT');
expect(req.request.body).toEqual({
regions: [],
environments: ['dev'],
timeWindow: '24h',
});
expect(req.request.body.tenantId).toBeUndefined();
expect(req.request.body.stage).toBeUndefined();
req.flush({
tenantId: 'demo-prod',
actorId: 'context-tests',
regions: ['us-east', 'eu-west', 'apac'],
environments: ['dev'],
timeWindow: '24h',
updatedAt: '2026-03-07T00:00:00Z',
updatedBy: 'context-tests',
});
expect(store.selectedEnvironments()).toEqual(['dev']);
expect(store.error()).toBeNull();
});
});

View File

@@ -45,6 +45,12 @@ interface PlatformContextQueryState {
stage: string;
}
interface PlatformContextPreferencesRequestPayload {
regions: string[];
environments: string[];
timeWindow: string;
}
@Injectable({ providedIn: 'root' })
export class PlatformContextStore {
private readonly http = inject(HttpClient);
@@ -378,13 +384,7 @@ export class PlatformContextStore {
return;
}
const payload = {
tenantId: this.tenantId(),
regions: this.selectedRegions(),
environments: this.selectedEnvironments(),
timeWindow: this.timeWindow(),
stage: this.stage(),
};
const payload = this.buildPreferencesPayload();
this.http
.put<PlatformContextPreferences>('/api/v2/context/preferences', payload)
@@ -396,6 +396,14 @@ export class PlatformContextStore {
});
}
private buildPreferencesPayload(): PlatformContextPreferencesRequestPayload {
return {
regions: this.selectedRegions(),
environments: this.selectedEnvironments(),
timeWindow: this.timeWindow(),
};
}
private finishInitialization(): void {
this.loading.set(false);
this.initialized.set(true);