Skip to content
Merged
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
6 changes: 6 additions & 0 deletions js-sdk-framework-tests/nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default async function Page() {
<p />
<h2>Manually test:</h2>
<ul>
<li>AI</li>
<ul>
<li><Link href="/tests/ai/web_client">AI Web SDK client-side tests</Link></li>
<li><Link href="/tests/ai/web_ssr">AI Web SDK server-side tests</Link></li>
</ul>
<p />
<li>Analytics</li>
<ul>
<li><Link href="/tests/analytics/web_client">Analytics Web SDK client-side tests</Link></li>
Expand Down
31 changes: 31 additions & 0 deletions js-sdk-framework-tests/nextjs/app/tests/ai/web_client/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Metadata } from 'next'
import ClientResults from '@/components/app_tests/ai/csr_test_runner';

export const metadata: Metadata = {
title: 'AI Web SDK CSR test'
}

export default function Page() {
return (
<>
<h1>AI CSR Test results:</h1>
<ClientResults />
</>
);
}
33 changes: 33 additions & 0 deletions js-sdk-framework-tests/nextjs/app/tests/ai/web_ssr/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Metadata } from 'next'
import { testAI, TestResults } from '@/lib/app_tests/ai/test';
import ResultsDisplay from '@/components/app_tests/ai/results_display';

export const metadata: Metadata = {
title: 'AI Web SDK SSR test'
}

export default async function Page() {
const testResults: TestResults = await testAI(/*isServer=*/true);
return (
<>
<h1>AI SSR Test results:</h1>
<ResultsDisplay statusString='Tests Complete!' testResults={testResults} />
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use client'

import { useState, useEffect } from 'react'
import { testAI, initializeTestResults } from '@/lib/app_tests/ai/test';
import ResultsDisplay from './results_display';

export default function ClientResults() {
const [testStatus, setTestStatus] = useState<string>("running...");
const [testResults, setTestResults] = useState(initializeTestResults());
useEffect(() => {
const asyncTest = async () => {
setTestResults(await testAI());
setTestStatus("Complete!");
}
asyncTest().catch((e) => {
console.error("Error encountered during testing: ", e);
setTestStatus("Errored!");
});
}, []);

return (
<ResultsDisplay statusString={testStatus} testResults={testResults} />
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Link from 'next/link';
export default function ResultsDisplay({ statusString, testResults }) {
return (
<>
<h2 title="testStatus">{statusString}</h2>
<h4 title="initializeAppResult">initializeAppResult: {testResults.initializeAppResult}</h4>
<h4 title="getAIResult">getAIResult: {testResults.getAIResult}</h4>
<h4 title="getGenerativeModelResult">getGenerativeModelResult: {testResults.getGenerativeModelResult}</h4>
<h4 title="startChatResult">startChatResult: {testResults.startChatResult}</h4>
<h4 title="chatSendFirstMessageResult">chatSendFirstMessageResult: {testResults.chatSendFirstMessageResult}</h4>
<h4 title="chatFirstResponseCheckResult">chatFirstResponseCheckResult: {testResults.chatFirstResponseCheckResult}</h4>
<h4 title="chatSendSecondMessageResult">chatSendSecondMessageResult: {testResults.chatSendSecondMessageResult}</h4>
<h4 title="chatSecondResponseCheckResult">chatSecondResponseCheckResult: {testResults.chatSecondResponseCheckResult}</h4>
<h4 title="getHistoryResult">getHistoryResult: {testResults.getHistoryResult}</h4>
<p />
<Link href="/">Back to test index</Link>
</>
);
}
140 changes: 140 additions & 0 deletions js-sdk-framework-tests/nextjs/src/lib/app_tests/ai/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { initializeApp } from 'firebase/app';
import {
GenerationConfig,
HarmBlockThreshold,
HarmCategory,
Content,
SafetySetting,
getAI,
getGenerativeModel,
GoogleAIBackend
} from 'firebase/ai';
import { firebaseConfig } from '@/lib/app_tests/firebase';
import { OK, FAILED } from '@/lib/app_tests/util';

export type TestResults = {
initializeAppResult: string,
getAIResult: string,
getGenerativeModelResult: string,
startChatResult: string,
chatSendFirstMessageResult: string,
chatFirstResponseCheckResult: string,
chatSendSecondMessageResult: string,
chatSecondResponseCheckResult: string,
getHistoryResult: string,
};

export function initializeTestResults(): TestResults {
const testAnalyticsResult: TestResults = {
initializeAppResult: FAILED,
getAIResult: FAILED,
getGenerativeModelResult: FAILED,
startChatResult: FAILED,
chatSendFirstMessageResult: FAILED,
chatFirstResponseCheckResult: FAILED,
chatSendSecondMessageResult: FAILED,
chatSecondResponseCheckResult: FAILED,
getHistoryResult: FAILED,
};
return testAnalyticsResult;
}

const commonGenerationConfig: GenerationConfig = {
temperature: 0,
topP: 0,
responseMimeType: 'text/plain'
};

const commonSafetySettings: SafetySetting[] = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
},
{
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE
}
];

const commonSystemInstruction: Content = {
role: 'system',
parts: [
{
text: 'You are a friendly and helpful assistant.'
}
]
};

export async function testAI(isServer: boolean = false): Promise<TestResults> {
if (isServer) {
console.log("Server side");
}
const result: TestResults = initializeTestResults();
const firebaseApp = initializeApp(firebaseConfig);
result.initializeAppResult = OK;

result.signInAnonymouslyResult = OK;

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
result.getAIResult = OK;

const model = getGenerativeModel(ai, {
model: "gemini-2.5-flash",
generationConfig: commonGenerationConfig,
safetySettings: commonSafetySettings,
systemInstruction: commonSystemInstruction
});
result.getGenerativeModelResult = OK;

const chat = model.startChat();
result.startChatResult = OK;

const result1 = await chat.sendMessage(
'What is the capital of France?'
);
result.chatSendFirstMessageResult = OK;

const response1 = result1.response;
if (response1.text().length !== 0) {
result.chatFirstResponseCheckResult = OK;
}

const result2 = await chat.sendMessage('And what about Italy?');
result.chatSendSecondMessageResult = OK;

const response2 = result2.response;
if (response2.text().length !== 0) {
result.chatSecondResponseCheckResult = OK;
}

const history = await chat.getHistory();
if (history.length !== 0) {
result.getHistoryResult = OK;
}

return result;
}
44 changes: 44 additions & 0 deletions js-sdk-framework-tests/nextjs/tests/ai.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { test, expect } from '@playwright/test';

async function commonExpectations(page) {
await expect(page.getByTitle('initializeAppResult')).not.toContainText("FAILED");
await expect(page.getByTitle('getAIResult')).not.toContainText("FAILED");
await expect(page.getByTitle('getGenerativeModelResult')).not.toContainText("FAILED");
await expect(page.getByTitle('startChatResult')).not.toContainText("FAILED");
await expect(page.getByTitle('chatSendFirstMessageResult')).not.toContainText("FAILED");
await expect(page.getByTitle('chatFirstResponseCheckResult')).not.toContainText("FAILED");
await expect(page.getByTitle('chatSendSecondMessageResult')).not.toContainText("FAILED");
await expect(page.getByTitle('chatSecondResponseCheckResult')).not.toContainText("FAILED");
await expect(page.getByTitle('getHistoryResult')).not.toContainText("FAILED");

}

test('ai operations should pass - client', async ({ page, baseURL }) => {
await page.goto(`${baseURL}/tests/ai/web_client`);
await expect(page.getByTitle('testStatus')).toContainText('Complete', { timeout: 10000 });
await expect(page.locator('h1')).toContainText('AI CSR Test');
await commonExpectations(page);
});

test('ai operations should pass - server', async ({ page, baseURL }) => {
await page.goto(`${baseURL}/tests/ai/web_ssr`);
await expect(page.getByTitle('testStatus')).toContainText('Complete', { timeout: 10000 });
await expect(page.locator('h1')).toContainText('AI SSR Test');
await commonExpectations(page);
});
Loading