Skip to content

Commit 9a2a062

Browse files
OrKoNDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Fix AI Assistance storage
Use structured clone to make sure internal setting data is not mutated. Fixes the test isolation. Bug: 369303513 Change-Id: I39ab62fe54c1137d891c2d8a05d192e16c9e4bc7 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6084696 Commit-Queue: Alex Rudenko <[email protected]> Reviewed-by: Samiya Caur <[email protected]>
1 parent 728a4c6 commit 9a2a062

File tree

2 files changed

+59
-36
lines changed

2 files changed

+59
-36
lines changed

front_end/panels/ai_assistance/AiHistoryStorage.test.ts

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,64 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import {
6-
describeWithEnvironment,
7-
} from '../../testing/EnvironmentHelpers.js';
5+
import * as Common from '../../core/common/common.js';
86

9-
import * as Freestyler from './ai_assistance.js';
7+
import * as AiAssistance from './ai_assistance.js';
108

11-
describeWithEnvironment('Freestyler.AiHistoryStorage', () => {
12-
const agent1: Freestyler.SerializedAgent = {
9+
describe('AiHistoryStorage', () => {
10+
const agent1: AiAssistance.SerializedAgent = {
1311
id: 'id1',
14-
type: Freestyler.AgentType.STYLING,
12+
type: AiAssistance.AgentType.STYLING,
1513
history: [],
1614
};
17-
const agent2: Freestyler.SerializedAgent = {
15+
const agent2: AiAssistance.SerializedAgent = {
1816
id: 'id2',
19-
type: Freestyler.AgentType.FILE,
17+
type: AiAssistance.AgentType.FILE,
2018
history: [],
2119
};
22-
const agent3: Freestyler.SerializedAgent = {
20+
const agent3: AiAssistance.SerializedAgent = {
2321
id: 'id3',
24-
type: Freestyler.AgentType.NETWORK,
22+
type: AiAssistance.AgentType.NETWORK,
2523
history: [],
2624
};
2725

28-
afterEach(() => {
29-
Freestyler.AiHistoryStorage.instance().clearForTest();
26+
beforeEach(() => {
27+
let data: Record<string, string> = {};
28+
const dummyStorage = new Common.Settings.SettingsStorage({}, {
29+
get(setting) {
30+
return Promise.resolve(data[setting]);
31+
},
32+
set(setting, value) {
33+
data[setting] = value;
34+
},
35+
clear() {
36+
data = {};
37+
},
38+
remove(setting) {
39+
delete data[setting];
40+
},
41+
register(_setting) {},
42+
});
43+
Common.Settings.Settings.instance({
44+
forceNew: true,
45+
syncedStorage: dummyStorage,
46+
globalStorage: dummyStorage,
47+
localStorage: dummyStorage,
48+
});
3049
});
3150

51+
function getStorage() {
52+
return AiAssistance.AiHistoryStorage.instance(true);
53+
}
54+
3255
it('should create and retrieve history entry', async () => {
33-
const storage = Freestyler.AiHistoryStorage.instance();
56+
const storage = getStorage();
3457
await storage.upsertHistoryEntry(agent1);
3558
assert.deepEqual(
3659
storage.getHistory(),
3760
[{
3861
id: 'id1',
39-
type: 'freestyler' as Freestyler.AgentType,
62+
type: 'freestyler' as AiAssistance.AgentType,
4063
history: [],
4164
}],
4265
);
@@ -46,27 +69,27 @@ describeWithEnvironment('Freestyler.AiHistoryStorage', () => {
4669
[
4770
{
4871
id: 'id1',
49-
type: 'freestyler' as Freestyler.AgentType,
72+
type: 'freestyler' as AiAssistance.AgentType,
5073
history: [],
5174
},
5275
{
5376
id: 'id2',
54-
type: 'drjones-file' as Freestyler.AgentType,
77+
type: 'drjones-file' as AiAssistance.AgentType,
5578
history: [],
5679
},
5780
],
5881
);
5982
});
6083

6184
it('should update history entries correctly', async () => {
62-
const storage = Freestyler.AiHistoryStorage.instance();
85+
const storage = getStorage();
6386
await storage.upsertHistoryEntry(agent1);
6487
await storage.upsertHistoryEntry(agent2);
6588
await storage.upsertHistoryEntry({
6689
...agent1,
6790
history: [
6891
{
69-
type: Freestyler.ResponseType.USER_QUERY,
92+
type: AiAssistance.ResponseType.USER_QUERY,
7093
query: 'text',
7194
},
7295
],
@@ -76,17 +99,17 @@ describeWithEnvironment('Freestyler.AiHistoryStorage', () => {
7699
[
77100
{
78101
id: 'id1',
79-
type: 'freestyler' as Freestyler.AgentType,
102+
type: 'freestyler' as AiAssistance.AgentType,
80103
history: [
81104
{
82-
type: Freestyler.ResponseType.USER_QUERY,
105+
type: AiAssistance.ResponseType.USER_QUERY,
83106
query: 'text',
84107
},
85108
],
86109
},
87110
{
88111
id: 'id2',
89-
type: 'drjones-file' as Freestyler.AgentType,
112+
type: 'drjones-file' as AiAssistance.AgentType,
90113
history: [],
91114
},
92115
],
@@ -98,30 +121,30 @@ describeWithEnvironment('Freestyler.AiHistoryStorage', () => {
98121
[
99122
{
100123
id: 'id1',
101-
type: 'freestyler' as Freestyler.AgentType,
124+
type: 'freestyler' as AiAssistance.AgentType,
102125
history: [
103126
{
104-
type: Freestyler.ResponseType.USER_QUERY,
127+
type: AiAssistance.ResponseType.USER_QUERY,
105128
query: 'text',
106129
},
107130
],
108131
},
109132
{
110133
id: 'id2',
111-
type: 'drjones-file' as Freestyler.AgentType,
134+
type: 'drjones-file' as AiAssistance.AgentType,
112135
history: [],
113136
},
114137
{
115138
id: 'id3',
116-
type: 'drjones-network-request' as Freestyler.AgentType,
139+
type: 'drjones-network-request' as AiAssistance.AgentType,
117140
history: [],
118141
},
119142
],
120143
);
121144
});
122145

123146
it('should delete a single entry', async () => {
124-
const storage = Freestyler.AiHistoryStorage.instance();
147+
const storage = getStorage();
125148
await storage.upsertHistoryEntry(agent1);
126149
await storage.upsertHistoryEntry(agent2);
127150
await storage.upsertHistoryEntry(agent3);
@@ -131,22 +154,22 @@ describeWithEnvironment('Freestyler.AiHistoryStorage', () => {
131154
[
132155
{
133156
id: 'id1',
134-
type: 'freestyler' as Freestyler.AgentType,
157+
type: 'freestyler' as AiAssistance.AgentType,
135158
history: [
136159

137160
],
138161
},
139162
{
140163
id: 'id3',
141-
type: 'drjones-network-request' as Freestyler.AgentType,
164+
type: 'drjones-network-request' as AiAssistance.AgentType,
142165
history: [],
143166
},
144167
],
145168
);
146169
});
147170

148171
it('should delete all entries', async () => {
149-
const storage = Freestyler.AiHistoryStorage.instance();
172+
const storage = getStorage();
150173
await storage.upsertHistoryEntry(agent1);
151174
await storage.upsertHistoryEntry(agent2);
152175
await storage.upsertHistoryEntry(agent3);

front_end/panels/ai_assistance/AiHistoryStorage.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class AiHistoryStorage {
1414

1515
constructor() {
1616
// This should not throw as we should be creating the setting in the `-meta.ts` file
17-
this.#historySetting = Common.Settings.Settings.instance().moduleSetting('ai-assistance-history-entries');
17+
this.#historySetting = Common.Settings.Settings.instance().createSetting('ai-assistance-history-entries', []);
1818
}
1919

2020
clearForTest(): void {
@@ -24,7 +24,7 @@ export class AiHistoryStorage {
2424
async upsertHistoryEntry(agentEntry: SerializedAgent): Promise<void> {
2525
const release = await this.#mutex.acquire();
2626
try {
27-
const history = await this.#historySetting.forceGet();
27+
const history = structuredClone(await this.#historySetting.forceGet());
2828
const historyEntryIndex = history.findIndex(entry => entry.id === agentEntry.id);
2929
if (historyEntryIndex !== -1) {
3030
history[historyEntryIndex] = agentEntry;
@@ -40,7 +40,7 @@ export class AiHistoryStorage {
4040
async deleteHistoryEntry(id: string): Promise<void> {
4141
const release = await this.#mutex.acquire();
4242
try {
43-
const history = await this.#historySetting.forceGet();
43+
const history = structuredClone(await this.#historySetting.forceGet());
4444
this.#historySetting.set(
4545
history.filter(entry => entry.id !== id),
4646
);
@@ -59,11 +59,11 @@ export class AiHistoryStorage {
5959
}
6060

6161
getHistory(): SerializedAgent[] {
62-
return this.#historySetting.get();
62+
return structuredClone(this.#historySetting.get());
6363
}
6464

65-
static instance(): AiHistoryStorage {
66-
if (!instance) {
65+
static instance(forceNew = false): AiHistoryStorage {
66+
if (!instance || forceNew) {
6767
instance = new AiHistoryStorage();
6868
}
6969
return instance;

0 commit comments

Comments
 (0)