From d46637ef949b251ecc8eef1c6805c1ab4440a679 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 11 Nov 2023 20:45:24 -0800 Subject: [PATCH 1/9] Adds simple experimental POST endpoint --- packages/hub/src/app/api/runSquiggle/route.ts | 43 +++++++++++++++++++ .../hub/src/graphql/queries/runSquiggle.ts | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 packages/hub/src/app/api/runSquiggle/route.ts diff --git a/packages/hub/src/app/api/runSquiggle/route.ts b/packages/hub/src/app/api/runSquiggle/route.ts new file mode 100644 index 0000000000..5e772a1c6b --- /dev/null +++ b/packages/hub/src/app/api/runSquiggle/route.ts @@ -0,0 +1,43 @@ +import { runSquiggle } from "@/graphql/queries/runSquiggle"; +import { NextRequest, NextResponse } from "next/server"; + +export function GET(request: NextRequest) { + return new NextResponse(JSON.stringify({ foo: 3 }), { + status: 200, + statusText: "OK", + headers: { "Content-Type": "application/json" }, + }); +} + +export async function POST(req: NextRequest) { + // Assuming 'code' is sent in the request body and is a string + const body = await req.json(); + if (body.code) { + let response = await runSquiggle(body.code); + if (response.isOk) { + return new NextResponse( + JSON.stringify({ + result: response.resultJSON, + bindings: response.bindingsJSON, + }), + { + status: 200, + statusText: "OK", + headers: { "Content-Type": "application/json" }, + } + ); + } else { + return new NextResponse(JSON.stringify({ error: response.errorString }), { + status: 400, + statusText: "ERROR", + headers: { "Content-Type": "application/json" }, + }); + } + } else { + return new NextResponse(JSON.stringify({ error: "No code provided" }), { + status: 400, + statusText: "ERROR", + headers: { "Content-Type": "application/json" }, + }); + } +} diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index ef9da8ea97..aea1300769 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -91,7 +91,7 @@ builder.objectType( } ); -async function runSquiggle(code: string): Promise { +export async function runSquiggle(code: string): Promise { const MAIN = "main"; const project = SqProject.create(); From 380b83b635842847a0df1d9d96d0ccad2bb58ec4 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Mon, 13 Nov 2023 21:00:07 -0800 Subject: [PATCH 2/9] Don't show all of lambdas in API response --- packages/hub/src/graphql/queries/runSquiggle.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index aea1300769..249a2d0a90 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -3,6 +3,8 @@ import { Prisma } from "@prisma/client"; import { SqAbstractDistribution, + SqLambda, + SqDistribution, SqProject, SqValue, } from "@quri/squiggle-lang"; @@ -21,6 +23,9 @@ export const squiggleValueToJSON = (value: SqValue) => { if (value instanceof Map) { return Object.fromEntries(value.entries()); } + if (value instanceof SqLambda) { + return value.toString(); + } if (value instanceof SqAbstractDistribution) { return value.toString(); } From 967568a0c8f3ef5466e0e553552c7ab3c3e60e8e Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Mon, 13 Nov 2023 21:15:59 -0800 Subject: [PATCH 3/9] Quick sampleSet data --- .../hub/src/graphql/queries/runSquiggle.ts | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index 249a2d0a90..050b1c270a 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -7,6 +7,8 @@ import { SqDistribution, SqProject, SqValue, + SqPointSetDistribution, + SqSampleSetDistribution, } from "@quri/squiggle-lang"; import { builder } from "@/graphql/builder"; @@ -26,6 +28,17 @@ export const squiggleValueToJSON = (value: SqValue) => { if (value instanceof SqLambda) { return value.toString(); } + if (value instanceof SqSampleSetDistribution) { + return { + type: "SampleSetDistribution", + first100Samples: value._value.samples.slice(0, 100), + sampleCount: value._value.samples.length, + mean: value._value.mean(), + stdev: value._value.stdev(), + p5: value._value.cdf(0.05), + p95: value._value.cdf(0.95), + }; + } if (value instanceof SqAbstractDistribution) { return value.toString(); } @@ -99,7 +112,12 @@ builder.objectType( export async function runSquiggle(code: string): Promise { const MAIN = "main"; - const project = SqProject.create(); + const env = { + sampleCount: 1000, // int + xyPointLength: 1000, // int + }; + + const project = SqProject.create({ environment: env }); project.setSource(MAIN, code); await project.run(MAIN); From 1c86c39f91a0803adc41a2b1e4ac320a6bc134c3 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Thu, 16 Nov 2023 17:38:01 -0800 Subject: [PATCH 4/9] Hide calculator fns from API --- packages/hub/src/app/api/runSquiggle/route.ts | 8 -------- packages/hub/src/graphql/queries/runSquiggle.ts | 9 +++++++++ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/packages/hub/src/app/api/runSquiggle/route.ts b/packages/hub/src/app/api/runSquiggle/route.ts index 5e772a1c6b..195130a863 100644 --- a/packages/hub/src/app/api/runSquiggle/route.ts +++ b/packages/hub/src/app/api/runSquiggle/route.ts @@ -1,14 +1,6 @@ import { runSquiggle } from "@/graphql/queries/runSquiggle"; import { NextRequest, NextResponse } from "next/server"; -export function GET(request: NextRequest) { - return new NextResponse(JSON.stringify({ foo: 3 }), { - status: 200, - statusText: "OK", - headers: { "Content-Type": "application/json" }, - }); -} - export async function POST(req: NextRequest) { // Assuming 'code' is sent in the request body and is a string const body = await req.json(); diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index 050b1c270a..79eb71c64e 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -9,6 +9,7 @@ import { SqValue, SqPointSetDistribution, SqSampleSetDistribution, + SqCalculator, } from "@quri/squiggle-lang"; import { builder } from "@/graphql/builder"; @@ -39,6 +40,14 @@ export const squiggleValueToJSON = (value: SqValue) => { p95: value._value.cdf(0.95), }; } + if (value instanceof SqCalculator) { + return { + type: "Calculator", + title: value.title, + description: value.description, + inputs: value.inputs, + }; + } if (value instanceof SqAbstractDistribution) { return value.toString(); } From 61ff33f609ec23ee547631b7eac38d1a0c9a7a3e Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 30 Dec 2023 21:06:54 -0800 Subject: [PATCH 5/9] Simplifying code, adding schema --- .../hub/src/graphql/queries/runSquiggle.ts | 46 +----------- packages/hub/src/public/openapi-schema.json | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+), 44 deletions(-) create mode 100644 packages/hub/src/public/openapi-schema.json diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index 9b9b6b1907..3cd92165ba 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -1,16 +1,7 @@ import { Prisma } from "@prisma/client"; import crypto from "crypto"; -import { - SqAbstractDistribution, - SqLambda, - SqDistribution, - SqProject, - SqValue, - SqPointSetDistribution, - SqSampleSetDistribution, - SqCalculator, -} from "@quri/squiggle-lang"; +import { SqProject, SqValue } from "@quri/squiggle-lang"; import { builder } from "@/graphql/builder"; import { prisma } from "@/prisma"; @@ -20,40 +11,7 @@ function getKey(code: string): string { } export const squiggleValueToJSON = (value: SqValue) => { - // this is a lazy shortcut to traverse the value tree; should be reimplemented without parse/stringify - return JSON.parse( - JSON.stringify(value.asJS(), (key, value) => { - if (value instanceof Map) { - return Object.fromEntries(value.entries()); - } - if (value instanceof SqLambda) { - return value.toString(); - } - if (value instanceof SqSampleSetDistribution) { - return { - type: "SampleSetDistribution", - first100Samples: value._value.samples.slice(0, 100), - sampleCount: value._value.samples.length, - mean: value._value.mean(), - stdev: value._value.stdev(), - p5: value._value.cdf(0.05), - p95: value._value.cdf(0.95), - }; - } - if (value instanceof SqCalculator) { - return { - type: "Calculator", - title: value.title, - description: value.description, - inputs: value.inputs, - }; - } - if (value instanceof SqAbstractDistribution) { - return value.toString(); - } - return value; - }) - ); + return JSON.parse(JSON.stringify(value.asJS())); }; type SquiggleOutput = { diff --git a/packages/hub/src/public/openapi-schema.json b/packages/hub/src/public/openapi-schema.json new file mode 100644 index 0000000000..b61b963d94 --- /dev/null +++ b/packages/hub/src/public/openapi-schema.json @@ -0,0 +1,75 @@ +{ + "openapi": "3.0.0", + "info": { + "title": "Squiggle Code Processing API", + "version": "0.0.1" + }, + "servers": [ + { + "url": "https://squigglehub.org/api" + } + ], + "paths": { + "/runSquiggle": { + "post": { + "summary": "Run the given Squiggle code string.", + "operationId": "runSquiggle", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "code": { + "type": "string", + "description": "The code to be processed." + } + }, + "required": ["code"] + } + } + } + }, + "responses": { + "200": { + "description": "Code processed successfully.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "result": { + "type": "string", + "description": "The result of the code processing." + }, + "bindings": { + "type": "string", + "description": "Additional bindings from the code processing." + } + } + } + } + } + }, + "400": { + "description": "Error in processing or no code provided.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "description": "Error message explaining the failure." + } + } + } + } + } + } + } + } + } + } +} From 300f6a34a58930e6b8ec15183c82fea427cbe913 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 30 Dec 2023 21:21:15 -0800 Subject: [PATCH 6/9] Minor fixes --- .../hub/src/graphql/queries/runSquiggle.ts | 4 +- .../squiggle-lang/src/public/SqValue/index.ts | 61 ++++++++++--------- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/packages/hub/src/graphql/queries/runSquiggle.ts b/packages/hub/src/graphql/queries/runSquiggle.ts index 3cd92165ba..8742e54b2e 100644 --- a/packages/hub/src/graphql/queries/runSquiggle.ts +++ b/packages/hub/src/graphql/queries/runSquiggle.ts @@ -10,8 +10,8 @@ function getKey(code: string): string { return crypto.createHash("md5").update(code).digest("base64"); } -export const squiggleValueToJSON = (value: SqValue) => { - return JSON.parse(JSON.stringify(value.asJS())); +export const squiggleValueToJSON = (value: SqValue): any => { + return value.asJS(); }; type SquiggleOutput = { diff --git a/packages/squiggle-lang/src/public/SqValue/index.ts b/packages/squiggle-lang/src/public/SqValue/index.ts index 33684533c2..1629114c07 100644 --- a/packages/squiggle-lang/src/public/SqValue/index.ts +++ b/packages/squiggle-lang/src/public/SqValue/index.ts @@ -8,7 +8,11 @@ import { vNumber, vString, } from "../../value/index.js"; -import { SimpleValue, simpleValueFromValue } from "../../value/simpleValue.js"; +import { + removeLambdas, + simpleValueFromValue, + simpleValueToJson, +} from "../../value/simpleValue.js"; import { SqError } from "../SqError.js"; import { SqValueContext } from "../SqValueContext.js"; import { SqArray } from "./SqArray.js"; @@ -23,6 +27,10 @@ import { wrapScale } from "./SqScale.js"; import { SqTableChart } from "./SqTableChart.js"; import { SqTags } from "./SqTags.js"; +function valueToJSON(value: Value): unknown { + return simpleValueToJson(removeLambdas(simpleValueFromValue(value))); +} + export function wrapValue(value: Value, context?: SqValueContext) { switch (value.type) { case "Array": @@ -89,15 +97,15 @@ export abstract class SqAbstractValue { abstract asJS(): JSType; } -export class SqArrayValue extends SqAbstractValue<"Array", SimpleValue[]> { +export class SqArrayValue extends SqAbstractValue<"Array", unknown[]> { tag = "Array" as const; get value() { return new SqArray(this._value.value, this.context); } - asJS(): SimpleValue[] { - return this._value.value.map(simpleValueFromValue); + asJS(): unknown[] { + return this._value.value.map(valueToJSON); } } @@ -113,7 +121,7 @@ export class SqBoolValue extends SqAbstractValue<"Bool", boolean> { } } -export class SqDateValue extends SqAbstractValue<"Date", Date> { +export class SqDateValue extends SqAbstractValue<"Date", unknown> { tag = "Date" as const; static create(value: SDate) { @@ -128,13 +136,12 @@ export class SqDateValue extends SqAbstractValue<"Date", Date> { return this._value.value; } - //Note: This reveals the underlying Date object, but we might prefer to keep it hidden asJS() { - return this.value.toDate(); + return valueToJSON(this._value); } } -export class SqDistributionValue extends SqAbstractValue<"Dist", SimpleValue> { +export class SqDistributionValue extends SqAbstractValue<"Dist", unknown> { tag = "Dist" as const; get value() { @@ -151,11 +158,11 @@ export class SqDistributionValue extends SqAbstractValue<"Dist", SimpleValue> { } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } -export class SqLambdaValue extends SqAbstractValue<"Lambda", SimpleValue> { +export class SqLambdaValue extends SqAbstractValue<"Lambda", unknown> { tag = "Lambda" as const; static create(value: SqLambda) { @@ -194,15 +201,15 @@ export class SqNumberValue extends SqAbstractValue<"Number", number> { } } -export class SqDictValue extends SqAbstractValue<"Dict", SimpleValue> { +export class SqDictValue extends SqAbstractValue<"Dict", unknown> { tag = "Dict" as const; get value() { return new SqDict(this._value.value, this.context); } - asJS(): SimpleValue { - return simpleValueFromValue(this._value); + asJS() { + return valueToJSON(this._value); } } @@ -238,7 +245,7 @@ export class SqDurationValue extends SqAbstractValue<"Duration", number> { } } -export class SqPlotValue extends SqAbstractValue<"Plot", SimpleValue> { +export class SqPlotValue extends SqAbstractValue<"Plot", unknown> { tag = "Plot" as const; get value() { @@ -250,13 +257,10 @@ export class SqPlotValue extends SqAbstractValue<"Plot", SimpleValue> { } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } -export class SqTableChartValue extends SqAbstractValue< - "TableChart", - SimpleValue -> { +export class SqTableChartValue extends SqAbstractValue<"TableChart", unknown> { tag = "TableChart" as const; get value() { @@ -264,13 +268,10 @@ export class SqTableChartValue extends SqAbstractValue< } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } -export class SqCalculatorValue extends SqAbstractValue< - "Calculator", - SimpleValue -> { +export class SqCalculatorValue extends SqAbstractValue<"Calculator", unknown> { tag = "Calculator" as const; get value() { @@ -286,7 +287,7 @@ export class SqCalculatorValue extends SqAbstractValue< } } -export class SqScaleValue extends SqAbstractValue<"Scale", SimpleValue> { +export class SqScaleValue extends SqAbstractValue<"Scale", unknown> { tag = "Scale" as const; get value() { @@ -294,11 +295,11 @@ export class SqScaleValue extends SqAbstractValue<"Scale", SimpleValue> { } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } -export class SqInputValue extends SqAbstractValue<"Input", SimpleValue> { +export class SqInputValue extends SqAbstractValue<"Input", unknown> { tag = "Input" as const; get value() { @@ -306,7 +307,7 @@ export class SqInputValue extends SqAbstractValue<"Input", SimpleValue> { } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } @@ -322,7 +323,7 @@ export class SqVoidValue extends SqAbstractValue<"Void", null> { } } -export class SqDomainValue extends SqAbstractValue<"Domain", SimpleValue> { +export class SqDomainValue extends SqAbstractValue<"Domain", unknown> { tag = "Domain" as const; get value() { @@ -330,7 +331,7 @@ export class SqDomainValue extends SqAbstractValue<"Domain", SimpleValue> { } asJS() { - return simpleValueFromValue(this._value); + return valueToJSON(this._value); } } From 0dd04ee3cdf54233a50b342951b4fcc4635db515 Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 30 Dec 2023 21:24:31 -0800 Subject: [PATCH 7/9] Removing select tests for now --- .../__tests__/public/SqValue_test.ts | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/packages/squiggle-lang/__tests__/public/SqValue_test.ts b/packages/squiggle-lang/__tests__/public/SqValue_test.ts index 68930a2880..f289ce1d11 100644 --- a/packages/squiggle-lang/__tests__/public/SqValue_test.ts +++ b/packages/squiggle-lang/__tests__/public/SqValue_test.ts @@ -1,33 +1,30 @@ -import { OrderedMap } from "immutable"; - import { run, sq } from "../../src/index.js"; -import { testRun } from "../helpers/helpers.js"; -describe("SqValue.asJS", () => { - test("SqDict -> Map", async () => { - const value = ( - await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') - ).asJS(); +// describe("SqValue.asJS", () => { +// test("SqDict -> Map", async () => { +// const value = ( +// await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') +// ).asJS(); - expect(value).toBeInstanceOf(OrderedMap); - }); +// expect(value).toBeInstanceOf(OrderedMap); +// }); - test("Dict fields", async () => { - const value = (await testRun("{ x: 5 }")).asJS(); +// test("Dict fields", async () => { +// const value = (await testRun("{ x: 5 }")).asJS(); - expect((value as any).get("value").get("x")).toBe(5); - }); +// expect((value as any).get("value").get("x")).toBe(5); +// }); - test("Deeply nested dist", async () => { - const value = ( - await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') - ).asJS(); +// test("Deeply nested dist", async () => { +// const value = ( +// await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') +// ).asJS(); - expect( - (value as any).get("value").get("y")[2].get("value").get("dist") - ).toBeInstanceOf(Array); - }); -}); +// expect( +// (value as any).get("value").get("y")[2].get("value").get("dist") +// ).toBeInstanceOf(Array); +// }); +// }); describe("docstrings", () => { const runToResult = async (code: string) => { From aefb4490d4e636ee0e10397ed699f9c3df94287a Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 30 Dec 2023 21:45:11 -0800 Subject: [PATCH 8/9] Catch all errors in runSquiggle, give 500 if server error --- packages/hub/src/app/api/runSquiggle/route.ts | 64 ++++++++++++------- packages/hub/src/public/openapi-schema.json | 16 +++++ 2 files changed, 57 insertions(+), 23 deletions(-) diff --git a/packages/hub/src/app/api/runSquiggle/route.ts b/packages/hub/src/app/api/runSquiggle/route.ts index 195130a863..54280244e3 100644 --- a/packages/hub/src/app/api/runSquiggle/route.ts +++ b/packages/hub/src/app/api/runSquiggle/route.ts @@ -1,35 +1,53 @@ -import { runSquiggle } from "@/graphql/queries/runSquiggle"; import { NextRequest, NextResponse } from "next/server"; +import { runSquiggle } from "@/graphql/queries/runSquiggle"; + export async function POST(req: NextRequest) { // Assuming 'code' is sent in the request body and is a string - const body = await req.json(); - if (body.code) { - let response = await runSquiggle(body.code); - if (response.isOk) { - return new NextResponse( - JSON.stringify({ - result: response.resultJSON, - bindings: response.bindingsJSON, - }), - { - status: 200, - statusText: "OK", - headers: { "Content-Type": "application/json" }, - } - ); + try { + const body = await req.json(); + if (body.code) { + let response = await runSquiggle(body.code); + if (response.isOk) { + return new NextResponse( + JSON.stringify({ + result: response.resultJSON, + bindings: response.bindingsJSON, + }), + { + status: 200, + statusText: "OK", + headers: { "Content-Type": "application/json" }, + } + ); + } else { + return new NextResponse( + JSON.stringify({ error: response.errorString }), + { + status: 400, + statusText: "ERROR", + headers: { "Content-Type": "application/json" }, + } + ); + } } else { - return new NextResponse(JSON.stringify({ error: response.errorString }), { + return new NextResponse(JSON.stringify({ error: "No code provided" }), { status: 400, statusText: "ERROR", headers: { "Content-Type": "application/json" }, }); } - } else { - return new NextResponse(JSON.stringify({ error: "No code provided" }), { - status: 400, - statusText: "ERROR", - headers: { "Content-Type": "application/json" }, - }); + } catch (error) { + // Catch any errors, including JSON parsing errors + console.error("Error in POST request:", error); + return new NextResponse( + //We could give more information here, but we don't want to leak any information + JSON.stringify({ error: "An internal error occurred" }), + { + status: 500, + statusText: "Internal Server Error", + headers: { "Content-Type": "application/json" }, + } + ); } } diff --git a/packages/hub/src/public/openapi-schema.json b/packages/hub/src/public/openapi-schema.json index b61b963d94..924befdc5b 100644 --- a/packages/hub/src/public/openapi-schema.json +++ b/packages/hub/src/public/openapi-schema.json @@ -67,6 +67,22 @@ } } } + }, + "500": { + "description": "Internal Server Error.", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "error": { + "type": "string", + "description": "A generic error message indicating a server problem." + } + } + } + } + } } } } From 5c1f6241244426c64fc175f98e56b342b35d2b9b Mon Sep 17 00:00:00 2001 From: Ozzie Gooen Date: Sat, 30 Dec 2023 22:05:14 -0800 Subject: [PATCH 9/9] Fixed SqValue --- .../__tests__/public/SqValue_test.ts | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/packages/squiggle-lang/__tests__/public/SqValue_test.ts b/packages/squiggle-lang/__tests__/public/SqValue_test.ts index f289ce1d11..b9f1f553fa 100644 --- a/packages/squiggle-lang/__tests__/public/SqValue_test.ts +++ b/packages/squiggle-lang/__tests__/public/SqValue_test.ts @@ -1,30 +1,29 @@ import { run, sq } from "../../src/index.js"; +import { testRun } from "../helpers/helpers.js"; -// describe("SqValue.asJS", () => { -// test("SqDict -> Map", async () => { -// const value = ( -// await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') -// ).asJS(); +describe("SqValue.asJS", () => { + test("SqDict -> Map", async () => { + const value = ( + await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') + ).asJS(); -// expect(value).toBeInstanceOf(OrderedMap); -// }); + expect(value).toBeInstanceOf(Object); + }); -// test("Dict fields", async () => { -// const value = (await testRun("{ x: 5 }")).asJS(); + test("Dict fields", async () => { + const value = (await testRun("{ x: 5 }")).asJS(); -// expect((value as any).get("value").get("x")).toBe(5); -// }); + expect((value as any).value.x).toBe(5); + }); -// test("Deeply nested dist", async () => { -// const value = ( -// await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') -// ).asJS(); + test("Deeply nested dist", async () => { + const value = ( + await testRun('{ x: 5, y: [3, "foo", { dist: normal(5,2) } ] }') + ).asJS(); -// expect( -// (value as any).get("value").get("y")[2].get("value").get("dist") -// ).toBeInstanceOf(Array); -// }); -// }); + expect((value as any).value.y[2].value.dist).toBeInstanceOf(Array); + }); +}); describe("docstrings", () => { const runToResult = async (code: string) => {