8000 fix(chat): persist session state edits on save by OKAN-SOFTWARE · Pull Request #556 · google/adk-web · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/app/components/chat/chat.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,84 @@ describe('ChatComponent', () => {
});
});
});
describe('Update State persistence', () => {
const originalState = {counter: 1};
const editedState = {counter: 2};

beforeEach(() => {
component.userId = USER_ID;
component.appName = TEST_APP_1_NAME;
component.sessionId = SESSION_1_ID;
component.currentSessionState = {...originalState};
});

it('persists edited state immediately after Save', () => {
mockDialog.open.and.returnValue({
afterClosed: () => of(editedState),
} as any);

component.updateState();

expect(mockSessionService.updateSession).toHaveBeenCalledOnceWith(
USER_ID,
TEST_APP_1_NAME,
SESSION_1_ID,
{stateDelta: editedState},
);

mockSessionService.updateSessionResponse.next({
id: SESSION_1_ID,
state: editedState,
} as any);

expect(component.currentSessionState).toEqual(editedState);
expect(component.updatedSessionState()).toBeNull();
});

it('does not update the session when canceled', () => {
mockDialog.open.and.returnValue({
afterClosed: () => of(null),
} as any);

component.updateState();

expect(mockSessionService.updateSession).not.toHaveBeenCalled();
expect(component.currentSessionState).toEqual(originalState);
});

it('preserves pending updates when no session exists', () => {
component.sessionId = '';

mockDialog.open.and.returnValue({
afterClosed: () => of(editedState),
} as any);

component.updateState();

expect(mockSessionService.updateSession).not.toHaveBeenCalled();
expect(component.updatedSessionState()).toEqual(editedState);
});

it('preserves state and reports API failures', () => {
spyOn(console, 'error');

mockDialog.open.and.returnValue({
afterClosed: () => of(editedState),
} as any);

component.updateState();

mockSessionService.updateSessionResponse.error(
new Error('Request failed'),
);

expect(component.currentSessionState).toEqual(originalState);
expect(mockSnackBar.open).toHaveBeenCalledWith(
'Failed to update session state',
'OK',
);
});
});

describe('ChatPanel integration', () => {
beforeEach(() => {
Expand Down
29 changes: 27 additions & 2 deletions src/app/components/chat/chat.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4951,10 +4951,35 @@ export class ChatComponent implements OnInit, AfterViewInit, OnDestroy {
},
});

dialogRef.afterClosed().subscribe((result: any) => {
if (result) {
dialogRef.afterClosed().subscribe((result: SessionState | null) => {
if (result === null || result === undefined) {
return;
}

// Preserve pending updates when a session has not been created yet.
if (!this.sessionId) {
this.updatedSessionState.set(result);
return;
}

// Persist changes immediately instead of waiting for the next message.
this.sessionService
.updateSession(this.userId, this.appName, this.sessionId, {
stateDelta: result,
})
.subscribe({
next: (session) => {
this.currentSessionState = session.state || result;
this.updatedSessionState.set(null);

this.sessionTab?.reloadSession(this.sessionId);
this.drawerSessionTab()?.reloadSession(this.sessionId);
},
error: (error) => {
console.error('Failed to update session state:', error);
this.openSnackBar('Failed to update session state', 'OK');
},
});
});
}

Expand Down
3 changes: 3 additions & 0 deletions src/app/core/services/testing/mock-session.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {SessionService} from '../session.service';

@Injectable()
export class MockSessionService implements Partial<SessionService> {
updateSessionResponse = new ReplaySubject<Session>(1);
updateSession = jasmine.createSpy('updateSession')
.and.returnValue(this.updateSessionResponse);
createSessionResponse = new ReplaySubject<Session>(1);
createSession = jasmine.createSpy('createSession')
.and.returnValue(this.createSessionResponse);
Expand Down
Loading
0