Skip to content

Commit d32c661

Browse files
committed
add some tests for on-device availability cases
1 parent 4f469cf commit d32c661

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

packages/ai/src/methods/chrome-adapter.test.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,55 @@ describe('ChromeAdapter', () => {
112112
it('returns false if mode is only cloud', async () => {
113113
const adapter = new ChromeAdapterImpl(
114114
{} as LanguageModel,
115-
'only_in_cloud'
115+
InferenceMode.ONLY_IN_CLOUD
116116
);
117117
expect(
118118
await adapter.isAvailable({
119119
contents: []
120120
})
121121
).to.be.false;
122122
});
123+
it('returns true if mode is only on device and is available', async () => {
124+
const adapter = new ChromeAdapterImpl(
125+
{
126+
availability: async () => Availability.AVAILABLE
127+
} as LanguageModel,
128+
InferenceMode.ONLY_ON_DEVICE
129+
);
130+
expect(
131+
await adapter.isAvailable({
132+
contents: []
133+
})
134+
).to.be.true;
135+
});
136+
it('throws if mode is only on device and is unavailable', async () => {
137+
const adapter = new ChromeAdapterImpl(
138+
{
139+
availability: async () => Availability.UNAVAILABLE
140+
} as LanguageModel,
141+
InferenceMode.ONLY_ON_DEVICE
142+
);
143+
await expect(
144+
adapter.isAvailable({
145+
contents: []
146+
})
147+
).to.be.rejected;
148+
});
149+
it('returns true after waiting for download if mode is only on device', async () => {
150+
const adapter = new ChromeAdapterImpl(
151+
{
152+
availability: async () => Availability.DOWNLOADING,
153+
create: ({}: LanguageModelCreateOptions) =>
154+
Promise.resolve({} as LanguageModel)
155+
} as LanguageModel,
156+
InferenceMode.ONLY_ON_DEVICE
157+
);
158+
expect(
159+
await adapter.isAvailable({
160+
contents: []
161+
})
162+
).to.be.true;
163+
});
123164
it('returns false if LanguageModel API is undefined', async () => {
124165
const adapter = new ChromeAdapterImpl(
125166
// @ts-expect-error
@@ -301,8 +342,11 @@ describe('ChromeAdapter', () => {
301342
});
302343
describe('generateContent', () => {
303344
it('throws if Chrome API is undefined', async () => {
304-
// @ts-expect-error
305-
const adapter = new ChromeAdapterImpl(undefined, 'only_on_device');
345+
const adapter = new ChromeAdapterImpl(
346+
// @ts-expect-error
347+
undefined,
348+
InferenceMode.ONLY_ON_DEVICE
349+
);
306350
await expect(
307351
adapter.generateContent({
308352
contents: []

packages/ai/src/types/chrome-adapter.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { InferenceMode } from './enums';
1918
import { CountTokensRequest, GenerateContentRequest } from './requests';
2019

2120
/**

0 commit comments

Comments
 (0)