Skip to content

Commit

Permalink
test refund message
Browse files Browse the repository at this point in the history
  • Loading branch information
petersalomonsen committed Dec 24, 2024
1 parent 521676f commit b391cf1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/aiproxy/.test.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SPIN_VARIABLE_OPENAI_API_KEY=abcd
SPIN_VARIABLE_REFUND_SIGNING_KEY=defgh
SPIN_VARIABLE_REFUND_SIGNING_KEY=48QM3KLHFY22hDNnDx6zvgakY1dy66Jsv4dtTT6mt131DtjvPrQn7zyr3CVb1ZKPuVLbmvjQSK9o5vuEvMyiLR5Y
SPIN_VARIABLE_FT_CONTRACT_ID=aitoken.test.near
SPIN_VARIABLE_OPENAI_COMPLETIONS_ENDPOINT=http://127.0.0.1:3001/v1/chat/completions
SPIN_VARIABLE_RPC_URL=http://localhost:14500
16 changes: 12 additions & 4 deletions examples/aiproxy/playwright-tests/aiproxy.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@

import { test, expect } from '@playwright/test';
import { createHash } from 'crypto';

test('ask question', async ({ page }) => {
const {functionAccessKeyPair, publicKey, accountId, contractId} = await fetch('http://localhost:14501').then(r => r.json());
const { functionAccessKeyPair, publicKey, accountId, contractId } = await fetch('http://localhost:14501').then(r => r.json());

await page.goto('/');
await page.evaluate(({accountId, publicKey, functionAccessKeyPair, contractId}) => {
localStorage.setItem("aiproxy_wallet_auth_key", JSON.stringify({accountId, allKeys: [publicKey]}));
await page.evaluate(({ accountId, publicKey, functionAccessKeyPair, contractId }) => {
localStorage.setItem("aiproxy_wallet_auth_key", JSON.stringify({ accountId, allKeys: [publicKey] }));
localStorage.setItem(`near-api-js:keystore:${accountId}:sandbox`, functionAccessKeyPair);
localStorage.setItem(`contractId`, contractId);
}, {accountId, publicKey, functionAccessKeyPair, contractId});
}, { accountId, publicKey, functionAccessKeyPair, contractId });

await page.reload();

Expand All @@ -20,4 +21,11 @@ test('ask question', async ({ page }) => {
questionArea.fill("Hello!");
await page.getByRole('button', { name: 'Ask AI' }).click();
await expect(await page.getByText("I am just a mockserver")).toBeVisible();
await page.locator("#refundButton").click();
await expect(await page.locator("#refund_message")).toContainText("refund_message");
const refundMessageObj = JSON.parse(JSON.parse(await page.locator("#refund_message").innerText()).refund_message);
expect(refundMessageObj.refund_amount).toEqual("127999973");
const conversationId = await page.locator("#conversation_id").inputValue();
const conversationIdHash = await createHash('sha256').update(Buffer.from(conversationId)).digest('hex');
await expect(conversationIdHash).toEqual(refundMessageObj.conversation_id);
});
2 changes: 1 addition & 1 deletion examples/aiproxy/playwright-tests/near_rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ await aiTokenAccount.deploy(await readFile('../fungibletoken/out/fungible_token.
await aiTokenAccount.call(aiTokenAccount.accountId, 'new_default_meta', { owner_id: aiTokenAccount.accountId, total_supply: 1_000_000_000_000n.toString() });

const javascript = (await readFile(new URL('../../fungibletoken/e2e/aiconversation.js', import.meta.url))).toString()
.replace("REPLACE_REFUND_SIGNATURE_PUBLIC_KEY", JSON.stringify(Array.from((await aiTokenAccount.getKey()).getPublicKey().data)));
.replace("REPLACE_REFUND_SIGNATURE_PUBLIC_KEY", JSON.stringify("ed25519:4vUdkHdG3DSDmw39DCADuwovsnPeTtymsigZSaPTEQA2"));

await aiTokenAccount.call(
aiTokenAccount.accountId,
Expand Down
10 changes: 10 additions & 0 deletions examples/aiproxy/playwright-tests/openaimockserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const server = createServer((req, res) => {
},
},
],
usage: null
}),
JSON.stringify({
choices: [
Expand All @@ -30,6 +31,15 @@ const server = createServer((req, res) => {
},
},
],
usage:
{
"prompt_tokens": 18,
"completion_tokens": 9,
"total_tokens": 27,
"prompt_tokens_details":
{ "cached_tokens": 0 },
"completion_tokens_details": { "reasoning_tokens": 0 }
}
}),
"[DONE]",
];
Expand Down
3 changes: 2 additions & 1 deletion examples/aiproxy/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@
<h1>OpenAI Proxy Streaming UI</h1>
<input id="conversation_id" readonly ="conversation id" />
<button id="startConversationButton">Start conversation</button>"
<button id="refundButton">Stop conversation and refund tokens</button><br />

<div id="messages"></div>
<div id="question-container">

<textarea id="question" disabled rows="4" placeholder="Type your question here..."></textarea>
<button id="askAIButton" disabled>Ask AI</button>

</div>
<button id="refundButton">Stop conversation and refund tokens</button><br />
<div>
<pre>
<code id="refund_message"></code>
Expand Down

0 comments on commit b391cf1

Please sign in to comment.