Something went wrong. Try again.
A fork of @slices.network/slices forked from slices.network/slices
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177import { assertEquals, assertStringIncludes } from "jsr:@std/assert";import { generateTypeScript } from "../src/mod.ts";import type { Lexicon } from "../src/mod.ts";
Deno.test("generateTypeScript - full integration test", async () => { const lexicons: Lexicon[] = [ { id: "com.example.post", definitions: { main: { type: "record", record: { type: "record", properties: { text: { type: "string" }, createdAt: { type: "string" }, likes: { type: "integer" }, tags: { type: "array", items: { type: "string" }, }, }, required: ["text", "createdAt"], }, }, }, }, { id: "com.example.user", definitions: { main: { type: "record", record: { type: "record", properties: { handle: { type: "string" }, displayName: { type: "string" }, }, required: ["handle"], }, }, }, }, { id: "network.slices.slice", definitions: { main: { type: "record", record: { type: "record", properties: { name: { type: "string" }, }, required: ["name"], }, }, }, }, ];
const result = await generateTypeScript(lexicons, { sliceUri: "at://did:example/com.example.slice/abc123", });
// Should include header comment with usage example assertStringIncludes(result, "Generated TypeScript client for AT Protocol records"); assertStringIncludes(result, "Lexicons: 3"); assertStringIncludes(result, "at://did:example/com.example.slice/abc123");
// Should include imports assertStringIncludes(result, 'SlicesClient'); assertStringIncludes(result, 'OAuthClient');
// Should include record interfaces assertStringIncludes(result, "export interface ComExamplePost"); assertStringIncludes(result, "export interface ComExampleUser"); assertStringIncludes(result, "text: string;"); assertStringIncludes(result, "handle: string;"); assertStringIncludes(result, "displayName?: string;");
// Should include sort fields types assertStringIncludes(result, "export type ComExamplePostSortFields"); assertStringIncludes(result, "export type ComExampleUserSortFields");
// Should include client class assertStringIncludes(result, "export class AtProtoClient extends SlicesClient"); assertStringIncludes(result, "readonly com: ComClient;");
// Should include CRUD methods assertStringIncludes(result, "async getRecords("); assertStringIncludes(result, "async createRecord(");
// Should include standard client methods for all lexicons
// Code should be formatted (no obvious formatting issues) assertEquals(result.includes(" ;"), false); // Double spaces before semicolons assertEquals(result.includes("\t\t\t"), false); // Triple tabs});
Deno.test("generateTypeScript - generates client for standard lexicons", async () => { const lexicons: Lexicon[] = [ { id: "app.bsky.feed.post", definitions: { main: { type: "record", record: { type: "record", properties: { text: { type: "string" }, }, }, }, }, }, ];
const result = await generateTypeScript(lexicons, { sliceUri: "at://test/slice", });
// Should include basic functionality assertStringIncludes(result, "export interface AppBskyFeedPost"); assertStringIncludes(result, "export class AtProtoClient"); assertStringIncludes(result, "async getRecords("); assertStringIncludes(result, "text?: string;");
// Should include imports assertStringIncludes(result, 'SlicesClient'); assertStringIncludes(result, 'OAuthClient');});
Deno.test("generateTypeScript - handles empty lexicons", async () => { const result = await generateTypeScript([], { sliceUri: "at://test/slice", });
// Should include basic structure even with no lexicons assertStringIncludes(result, "Generated TypeScript client"); assertStringIncludes(result, "Lexicons: 0"); assertStringIncludes(result, 'SlicesClient');
// Should include imports even with no lexicons assertStringIncludes(result, "@slices/client");
// Should not create any client class (no records to work with) assertEquals(result.includes("export class AtProtoClient"), false);});
Deno.test("generateTypeScript - creates correct usage example", async () => { const lexicons: Lexicon[] = [ { id: "social.app.post", definitions: { main: { type: "record", record: { type: "record", properties: { message: { type: "string" }, }, }, }, }, }, ];
const result = await generateTypeScript(lexicons, { sliceUri: "at://did:example/slice/123", });
// Should create usage example with the first non-network.slices lexicon assertStringIncludes(result, "client.social.app.post.getRecords()"); assertStringIncludes(result, "at://did:example/slice/123"); assertStringIncludes(result, "social.app.post"); assertStringIncludes(result, "getRecords()");});