From 1d5f74bca29c6bb28bef6035aa09a1e35884b40a Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Sat, 16 Mar 2024 22:54:54 -0400 Subject: test: begin writing tests --- assembly/index.ts | 45 +++++++++++++++++------------ bunfig.toml | 2 ++ package.json | 2 ++ src/components/NodeEditor.tsx | 67 ++++++++++++++++++++++--------------------- src/index.tsx | 3 +- src/pages/Editor.tsx | 6 +--- src/pages/SignUp.test.tsx | 34 ++++++++++++++++++++++ src/pages/SignUp.tsx | 9 +++++- src/test-preload.ts | 9 ++++++ 9 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 bunfig.toml create mode 100644 src/pages/SignUp.test.tsx create mode 100644 src/test-preload.ts diff --git a/assembly/index.ts b/assembly/index.ts index e94b0a8..8218a08 100644 --- a/assembly/index.ts +++ b/assembly/index.ts @@ -320,11 +320,12 @@ export function unzip(x: StaticArray): StaticArray { } function ditfft2(x: StaticArray, n: i32, s: i32, out: StaticArray): void { - if (n == 1) { + //trace('x,n,s,X:', 4, x.length, n, s, out.length); + /*if (n == 1) { out[0] = x[0]; out[1] = x[1]; return; - } else if (n == 2) { + } /*else if (n == 2) {*/ for (let k = 0; k < out.length - out.length % 2; k += 2) { for (let n = 0; n < x.length - x.length % 2; n += 2) { const y = -2 * Mathf.PI * k / x.length * n; @@ -334,11 +335,11 @@ function ditfft2(x: StaticArray, n: i32, s: i32, out: StaticArray): vo out[k+1] += x[n] * v + x[n+1] * u; } } - return; - } - ditfft2(x.slice>(0, n), n/2, 2*s, out.slice>(0, n)); - ditfft2(x.slice>(s * 2), n/2, 2*s, out.slice>(n)); - const outptr = changetype(out); + /*return; + }*/ + // ditfft2(x, n/2, 2*s, out.slice>(0, n)); + // ditfft2(x.slice>(s * 2), n/2, 2*s, out.slice>(n)); + /*const outptr = changetype(out); const twiddle = vf32(-2 * Mathf.PI / n); let vk = f32x4(0,1,2,3); for (let k = 0; k < n/2 - n/2 % 2; k += 4) { @@ -349,22 +350,30 @@ function ditfft2(x: StaticArray, n: i32, s: i32, out: StaticArray): vo v128.store(outptr + k*2 * 4, v128.add(p, q)); v128.store(outptr + (k*2 + n/2) * 4, v128.sub(p, q)); vk = v128.add(vk, vf32(1)); - } - for (let k = n/2 - n/2 % 2; k < n/2; k++) { + }*/ + //for (let k = n/2 - n/2 % 2; k < n/2; k++) { + /*for (let k = 0; k < n/2; k++) { + trace('n,s,k', 3, n, s, k); const pr = unchecked(out[k*2]); const pi = unchecked(out[k*2+1]); - const y = -2 * Mathf.PI * k / n; - const qr = Mathf.cos(y) * out[k*2+n/2] - const qi = Mathf.sin(y) * out[k*2+n/2+1]; + const tw = -2 * Mathf.PI / n * k; + const u = Mathf.cos(tw); + const v = Mathf.sin(tw); + const x = out[(k+n/2)*2]; + const y = out[(k+n/2)*2+1]; + const qr = x * u - y * v; + const qi = x * v + y * u; unchecked(out[k*2] = pr + qr); unchecked(out[k*2+1] = pi + qi); - unchecked(out[k*2+n/2] = pr - qr); - unchecked(out[k*2+n/2+1] = pi - qi); - } + unchecked(out[(k+n/2)*2] = pr - qr); + unchecked(out[(k+n/2)*2+1] = pi - qi); + }*/ } export function fft(x: StaticArray): StaticArray { - const out = new StaticArray(x.length); - ditfft2(x, x.length/2, 1, out); + const len = x.length - x.length % 2; + const out = new StaticArray(len); + if (len == 0) return out; + ditfft2(x.slice>(0, len), len/2, 1, out); return out; -} \ No newline at end of file +} diff --git a/bunfig.toml b/bunfig.toml new file mode 100644 index 0000000..445a41d --- /dev/null +++ b/bunfig.toml @@ -0,0 +1,2 @@ +[test] +preload = "./src/test-preload.ts" diff --git a/package.json b/package.json index 5f00d1c..4cd959f 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,8 @@ "preact-router": "^4.1.2" }, "devDependencies": { + "@happy-dom/global-registrator": "^13.8.6", + "@testing-library/preact": "^3.2.3", "assemblyscript": "^0.27.24", "esbuild": "^0.20.1", "typescript": "^5.3.3" diff --git a/src/components/NodeEditor.tsx b/src/components/NodeEditor.tsx index 758fb76..4cb7011 100644 --- a/src/components/NodeEditor.tsx +++ b/src/components/NodeEditor.tsx @@ -87,7 +87,7 @@ const NodeEditor = ({ project }: NodeEditorProps) => { const filter = pb.filter('project.id = {:id}', { id: project.id }); const projectNodes = await pb.collection('nodes').getFullList({ filter }); const projectLinks = await pb.collection('links').getFullList({ filter }); - const instances = projectNodes.map(node => instantiateNode(node.id, node.x, node.y, node.name)); + const instances = projectNodes.map(node => instantiateNode(node.id, node.x, node.y, nodeRegistry[node.type])); nodes.value = nodes.value.concat(instances); })(); }, []); @@ -237,14 +237,13 @@ const NodeEditor = ({ project }: NodeEditorProps) => { scale.value *= 1 + delta; }), []); - const addNode = useCallback(async (name: string, info: NodeInfo) => { - const node = await pb.collection('nodes').create({ x: 100, y: 100, type: name, project: projectId, collapsed: false }); - alert(JSON.stringify(node)); + const addNode = useCallback(async (type: string, info: NodeInfo) => { + const node = await pb.collection('nodes').create({ x: 100, y: 100, type, project: project.id, collapsed: false }); nodes.value = nodes.value.concat(instantiateNode(node.id, node.x, node.y, info)); }, []); return ( -
+ <> {Object.entries(nodeRegistry).map(([name, node]) => ( @@ -252,34 +251,36 @@ const NodeEditor = ({ project }: NodeEditorProps) => { ))} - - - - - - - - - - - - - {allLinks.value.map(({fromX, fromY, toX, toY}) => ( - - ))} - - {nodes.value.map(node => ( - - ))} - - - -
+
+ + + + + + + + + + + + + {allLinks.value.map(({fromX, fromY, toX, toY}) => ( + + ))} + + {nodes.value.map(node => ( + + ))} + + + +
+ ); }; diff --git a/src/index.tsx b/src/index.tsx index cd8c870..a67fd94 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,3 +1,4 @@ +import 'preact/debug'; import { render } from 'preact'; import { useMemo } from 'preact/hooks'; import { Router } from 'preact-router'; @@ -7,7 +8,7 @@ import { Home, SignUp, LogIn, ProjectsList, Editor } from './pages'; import './index.css'; export const App = () => { - const pb = useMemo(() => new PocketBase(`/`), []); + const pb = useMemo(() => new PocketBase('http://localhost:8090/'), []); return ( diff --git a/src/pages/Editor.tsx b/src/pages/Editor.tsx index c929595..c5b369f 100644 --- a/src/pages/Editor.tsx +++ b/src/pages/Editor.tsx @@ -20,11 +20,7 @@ const Editor = ({ user, project }: EditorProps) => { })(); }, []); - return ( - <> - {!!projectData.value && } - - ); + return projectData.value ? : null; }; export default Editor; diff --git a/src/pages/SignUp.test.tsx b/src/pages/SignUp.test.tsx new file mode 100644 index 0000000..3029beb --- /dev/null +++ b/src/pages/SignUp.test.tsx @@ -0,0 +1,34 @@ +import { expect, test } from 'bun:test'; +import { render, fireEvent } from '@testing-library/preact'; +import { Pb } from '../context.ts'; +import { pb } from '../preload.ts'; +import SignUp from './SignUp.tsx'; + +test('has a link to log in', () => { + const { getAllByText, getByLabelText, getByText } = render( + + ); + + expect(getAllByText(/log in/i)).not.toHaveLength(0); +}); + +test('can sign up', () => { + const username = 'foo'; + const email = 'foo@example.com'; + const password = '12345678'; + + const { getByText, getByLabelText } = render( + + + + ); + + fireEvent.change(getByLabelText(/username/i), {target: {value: username}}); + fireEvent.change(getByLabelText(/email/i), {target: {value: email}}); + fireEvent.change(getByLabelText(/password/i), {target: {value: password}}); + fireEvent.change(getByLabelText(/confirm password/i), {target: {value: password}}); + + fireEvent.click(getByText(/continue/i)); + + expect(pb.authStore.isValid()).toBeTrue(); +}); diff --git a/src/pages/SignUp.tsx b/src/pages/SignUp.tsx index e1cee3c..755c180 100644 --- a/src/pages/SignUp.tsx +++ b/src/pages/SignUp.tsx @@ -2,7 +2,14 @@ import { useContext, useCallback } from 'preact/hooks'; import { useSignal } from '@preact/signals'; import { route } from 'preact-router'; import { Pb } from '../context.ts'; -import { Header, Content, Form, FormLabel, TextInput, Button, ArrowButton } from '../components'; + +import Header from '../components/Header.tsx'; +import Content from '../components/Content.tsx'; +import Form from '../components/Form.tsx'; +import FormLabel from '../components/FormLabel.tsx'; +import TextInput from '../components/TextInput.tsx'; +import Button from '../components/Button.tsx'; +import ArrowButton from '../components/ArrowButton.tsx'; const SignUp = () => { const pb = useContext(Pb)!; diff --git a/src/test-preload.ts b/src/test-preload.ts new file mode 100644 index 0000000..3ec72a1 --- /dev/null +++ b/src/test-preload.ts @@ -0,0 +1,9 @@ +import { GlobalRegistrator } from '@happy-dom/global-registrator'; +import PocketBase from 'pocketbase'; + +GlobalRegistrator.register(); + +const proc = Bun.spawn(['./pocketbase', 'serve', '--http', '127.0.0.1:8090']); + +const pb = new PocketBase('http://localhost:8090/'); +export { pb }; -- cgit v1.2.3