A public library of components: publish yours, add everybody's
"Publish to the library…" in the menu on a circuit's name (or Space)
sends the component with every component it uses and their connector
types, under your leaderboard name. The server checks it like any
project, refuses what is missing, inside itself, nested too deep or too
big to run, and counts its transistors and pins itself. Publishing again
from the same browser makes the next version of the same entry; only that
browser holds the entry's key, so only it can update or remove it.
Challenge circuits and puzzle solutions stay private.
The Library (Components in the bottom bar, Space, the main menu) lists
every entry, newest first, searchable by name, description and author,
with a picture of its box, its transistors, pins and what comes along.
Add brings an entry into the open project and starts placing it: what the
project has in exactly that form already is used as it is, the rest comes
with new ids, a taken name gets a number, connector types join one with
the same lines. The This panel says where a component came from and
offers a newer version, which replaces it in place. A campaign only adds
what is built from parts it has earned, and its challenges take nothing
from the library.
Components keep an optional "from" mark; projects without it load and
compute as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:14:46 +00:00
|
|
|
// The library's API handlers, run against stand-ins for Nitro's request
|
|
|
|
|
// helpers and an in-memory store: publish, update, list, read, remove.
|
|
|
|
|
import { beforeAll, describe, expect, it } from 'vitest'
|
|
|
|
|
import { libraryBundle } from '../app/lib/logic/library'
|
|
|
|
|
import { createProject } from '../app/lib/logic/ops'
|
|
|
|
|
|
|
|
|
|
interface FakeEvent {
|
|
|
|
|
body?: string
|
|
|
|
|
params?: Record<string, string>
|
|
|
|
|
headers?: Record<string, string>
|
|
|
|
|
query?: Record<string, string>
|
|
|
|
|
status?: number
|
|
|
|
|
}
|
|
|
|
|
type Handler = (e: FakeEvent) => Promise<Record<string, unknown>>
|
|
|
|
|
|
|
|
|
|
const stores = new Map<string, Map<string, unknown>>()
|
|
|
|
|
const g = globalThis as Record<string, unknown>
|
|
|
|
|
const handlers: Record<string, Handler> = {}
|
|
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
Object.assign(g, {
|
|
|
|
|
defineEventHandler: (fn: Handler) => fn,
|
|
|
|
|
createError: ({ statusCode, message }: { statusCode: number; message: string }) => Object.assign(new Error(message), { statusCode }),
|
|
|
|
|
readRawBody: async (e: FakeEvent) => e.body,
|
|
|
|
|
readBody: async (e: FakeEvent) => JSON.parse(e.body ?? 'null'),
|
|
|
|
|
getRouterParam: (e: FakeEvent, k: string) => e.params?.[k],
|
|
|
|
|
getHeader: (e: FakeEvent, k: string) => e.headers?.[k],
|
|
|
|
|
getQuery: (e: FakeEvent) => e.query ?? {},
|
|
|
|
|
getRequestIP: () => '127.0.0.1',
|
|
|
|
|
setHeader: () => {},
|
|
|
|
|
setResponseStatus: (e: FakeEvent, s: number) => (e.status = s),
|
|
|
|
|
useStorage: (name: string) => {
|
|
|
|
|
if (!stores.has(name)) stores.set(name, new Map())
|
|
|
|
|
const m = stores.get(name)!
|
|
|
|
|
return {
|
|
|
|
|
getItem: async (k: string) => structuredClone(m.get(k) ?? null),
|
|
|
|
|
setItem: async (k: string, v: unknown) => void m.set(k, structuredClone(v)),
|
|
|
|
|
hasItem: async (k: string) => m.has(k),
|
|
|
|
|
removeItem: async (k: string) => void m.delete(k),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
// The server's own helpers, auto-imported there.
|
|
|
|
|
for (const mod of [await import('../server/utils/shares'), await import('../server/utils/puzzles'), await import('../server/utils/library')]) Object.assign(g, mod)
|
|
|
|
|
handlers.post = (await import('../server/api/library/index.post')).default as unknown as Handler
|
|
|
|
|
handlers.list = (await import('../server/api/library/index.get')).default as unknown as Handler
|
|
|
|
|
handlers.get = (await import('../server/api/library/[id].get')).default as unknown as Handler
|
|
|
|
|
handlers.put = (await import('../server/api/library/[id].put')).default as unknown as Handler
|
|
|
|
|
handlers.delete = (await import('../server/api/library/[id].delete')).default as unknown as Handler
|
|
|
|
|
handlers.name = (await import('../server/api/players/name.post')).default as unknown as Handler
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const ADA = 'player-ada-000001'
|
|
|
|
|
const call = (h: string, e: FakeEvent) => handlers[h]!(e)
|
|
|
|
|
|
|
|
|
|
describe('library API', () => {
|
|
|
|
|
it('publishes, lists, updates and removes an entry; only the key holder may change it', async () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
await call('name', { body: JSON.stringify({ player: ADA, name: 'Ada' }) })
|
|
|
|
|
const made = await call('post', { body: JSON.stringify({ player: ADA, ...libraryBundle(p, 'ex-fa') }) })
|
|
|
|
|
expect(made).toMatchObject({ version: 1 })
|
|
|
|
|
const id = made.id as string
|
|
|
|
|
const key = made.key as string
|
|
|
|
|
|
|
|
|
|
const list = await call('list', { query: { q: 'adder ada' } })
|
|
|
|
|
expect(list.total).toBe(1)
|
|
|
|
|
const [entry] = list.entries as Record<string, unknown>[]
|
|
|
|
|
// Two half adders (XOR of 4 NANDs, AND of 2) and an OR of 3: 15 NANDs.
|
BF library: no solutions inside, nothing into a campaign, marks only from the entry, a small listing, updates clear what they replace
A component with a challenge or puzzle solution anywhere inside stays
private, not only a challenge circuit itself, so wrapping a solution in a
part of its own no longer publishes it.
A campaign takes nothing from the library: Add and "Get version" are off
there, with a line saying why. A library part could be duplicated,
published again or taken apart into a solution, so a check on placing was
not enough.
Library marks come only from the entry being added. The server drops the
marks a publisher sends, and whether an entry is yours depends only on the
key this browser keeps for that project and component, so a part claiming
to be someone's entry can't be published over it.
The listing keeps pin counts and the first 16 pin names a side, short, and
a group of 40 characters at most, so it stays small whatever is sent.
"Get version" clears away what came along with the old version once
nothing else uses it, and the new parts take its names. Adding never
reuses another entry's component, so updating one entry leaves the others
as they were.
Wire corner points that aren't number pairs are dropped when a project,
link or library entry is read, so a circuit can always be drawn.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:44:13 +00:00
|
|
|
expect(entry).toMatchObject({ id, name: 'Full Adder', author: 'Ada', transistors: 60, inputs: 3, outputs: 2, inNames: ['A', 'B', 'Cin'], outNames: ['S', 'Cout'], uses: 5, version: 1 })
|
A public library of components: publish yours, add everybody's
"Publish to the library…" in the menu on a circuit's name (or Space)
sends the component with every component it uses and their connector
types, under your leaderboard name. The server checks it like any
project, refuses what is missing, inside itself, nested too deep or too
big to run, and counts its transistors and pins itself. Publishing again
from the same browser makes the next version of the same entry; only that
browser holds the entry's key, so only it can update or remove it.
Challenge circuits and puzzle solutions stay private.
The Library (Components in the bottom bar, Space, the main menu) lists
every entry, newest first, searchable by name, description and author,
with a picture of its box, its transistors, pins and what comes along.
Add brings an entry into the open project and starts placing it: what the
project has in exactly that form already is used as it is, the rest comes
with new ids, a taken name gets a number, connector types join one with
the same lines. The This panel says where a component came from and
offers a newer version, which replaces it in place. A campaign only adds
what is built from parts it has earned, and its challenges take nothing
from the library.
Components keep an optional "from" mark; projects without it load and
compute as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 17:14:46 +00:00
|
|
|
expect(JSON.stringify(list)).not.toContain(ADA)
|
|
|
|
|
expect((await call('list', { query: { q: 'counter' } })).entries).toEqual([])
|
|
|
|
|
|
|
|
|
|
const full = await call('get', { params: { id } })
|
|
|
|
|
expect((full.bundle as { root: string }).root).toBe('ex-fa')
|
|
|
|
|
expect(await call('get', { params: { id }, query: { meta: '' } })).not.toHaveProperty('bundle')
|
|
|
|
|
|
|
|
|
|
const next = JSON.stringify({ player: ADA, ...libraryBundle(p, 'ex-fa') })
|
|
|
|
|
await expect(call('put', { params: { id }, headers: { 'x-edit-key': 'wrong' }, body: next })).rejects.toMatchObject({ statusCode: 403 })
|
|
|
|
|
expect(await call('put', { params: { id }, headers: { 'x-edit-key': key }, body: next })).toEqual({ id, version: 2 })
|
|
|
|
|
expect(((await call('list', {})).entries as { version: number }[])[0]!.version).toBe(2)
|
|
|
|
|
|
|
|
|
|
await expect(call('delete', { params: { id }, headers: { 'x-edit-key': 'wrong' } })).rejects.toMatchObject({ statusCode: 403 })
|
|
|
|
|
await call('delete', { params: { id }, headers: { 'x-edit-key': key } })
|
|
|
|
|
expect((await call('list', {})).total).toBe(0)
|
|
|
|
|
await expect(call('get', { params: { id } })).rejects.toMatchObject({ statusCode: 404 })
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('refuses requests without a player, broken components and oversized ones', async () => {
|
|
|
|
|
const p = createProject({ examples: true })
|
|
|
|
|
const bundle = libraryBundle(p, 'ex-ha')
|
|
|
|
|
await expect(call('post', { body: JSON.stringify(bundle) })).rejects.toMatchObject({ statusCode: 400 })
|
|
|
|
|
await expect(call('post', { body: JSON.stringify({ player: ADA, root: 'ex-ha', components: [] }) })).rejects.toMatchObject({ statusCode: 400 })
|
|
|
|
|
await expect(call('post', { body: JSON.stringify({ player: ADA, ...bundle, pad: 'x'.repeat(1_000_001) }) })).rejects.toMatchObject({ statusCode: 413 })
|
|
|
|
|
})
|
|
|
|
|
})
|