From 5d019e9d2e01943cbde3775b912764d84a04a02e Mon Sep 17 00:00:00 2001 From: Duane Nykamp Date: Sat, 19 Oct 2024 18:56:33 -0500 Subject: [PATCH] convert samplePrimeNumbers tests to vitest --- .../src/test/tagSpecific/copy.test.ts | 4 +- .../tagSpecific/sampleprimenumbers.test.ts | 628 ++++++ .../e2e/tagSpecific/sampleprimenumbers.cy.js | 1919 ----------------- 3 files changed, 630 insertions(+), 1921 deletions(-) create mode 100644 packages/doenetml-worker/src/test/tagSpecific/sampleprimenumbers.test.ts diff --git a/packages/doenetml-worker/src/test/tagSpecific/copy.test.ts b/packages/doenetml-worker/src/test/tagSpecific/copy.test.ts index 689aec71a..8bc6da005 100644 --- a/packages/doenetml-worker/src/test/tagSpecific/copy.test.ts +++ b/packages/doenetml-worker/src/test/tagSpecific/copy.test.ts @@ -7669,14 +7669,14 @@ describe("Copy tag tests", async () => { expect(cleanLatex(stateVariables["/p31"].stateValues.text)).eq( `${P12latex || P12},${P22latex || P22}`, ); - expect(cleanLatex(stateVariables["/p32"].stateValues.text)).eq(`,`); + expect(cleanLatex(stateVariables["/p32"].stateValues.text)).eq(``); expect(cleanLatex(stateVariables["/p33"].stateValues.text)).eq( `${P11latex || P11},${P21latex || P21}`, ); expect(cleanLatex(stateVariables["/p34"].stateValues.text)).eq( `${P12latex || P12},${P22latex || P22}`, ); - expect(cleanLatex(stateVariables["/p35"].stateValues.text)).eq(`,`); + expect(cleanLatex(stateVariables["/p35"].stateValues.text)).eq(``); expect(cleanLatex(stateVariables["/p36"].stateValues.text)).eq( `(${P11latex || P11},${P12latex || P12})`, diff --git a/packages/doenetml-worker/src/test/tagSpecific/sampleprimenumbers.test.ts b/packages/doenetml-worker/src/test/tagSpecific/sampleprimenumbers.test.ts new file mode 100644 index 000000000..c9fe4da70 --- /dev/null +++ b/packages/doenetml-worker/src/test/tagSpecific/sampleprimenumbers.test.ts @@ -0,0 +1,628 @@ +import { describe, expect, it, vi } from "vitest"; +import { createTestCore, returnAllStateVariables } from "../utils/test-core"; +import { + updateBooleanInputValue, + updateMathInputValue, +} from "../utils/actions"; + +const Mock = vi.fn(); +vi.stubGlobal("postMessage", Mock); + +describe("SamplePrimeNumbers tag tests", async () => { + async function test_values_separately({ + doenetML, + componentNames, + compositeName, + valid_values, + num_samples, + must_be_distinct = false, + is_math = false, + }: { + doenetML: string; + componentNames: string[]; + compositeName?: string; + valid_values: any[][]; + num_samples: number; + must_be_distinct?: boolean; + is_math?: boolean; + }) { + for (let i = 0; i < num_samples; i++) { + let core = await createTestCore({ + doenetML, + requestedVariantIndex: i, + }); + const stateVariables = await returnAllStateVariables(core); + const s = stateVariables[compositeName ?? ""]; + for (let [ind, name] of componentNames.entries()) { + let value = name + ? stateVariables[name].stateValues.value + : stateVariables[s.replacements![ind].componentName] + .stateValues.value; + expect( + is_math + ? valid_values[ind].some((v) => v.equals(value)) + : valid_values[ind].includes(value), + ).eq(true, `Expected ${value} to be in ${valid_values[ind]}`); + } + + if (must_be_distinct) { + for (let name1 of componentNames) { + let val1 = stateVariables[name1].stateValues.value; + for (let name2 of componentNames) { + if (name2 !== name1) { + let val2 = stateVariables[name2].stateValues.value; + if (is_math) { + expect(val2.equals(val1)).eq(false); + } else { + expect(val2).not.eq(val1); + } + } + } + } + } + } + } + + async function test_combined_values({ + doenetML, + componentNames, + valid_combinations, + num_samples, + is_math = false, + }: { + doenetML: string; + componentNames: string[]; + valid_combinations: any[][]; + num_samples: number; + is_math?: boolean; + }) { + for (let i = 0; i < num_samples; i++) { + let core = await createTestCore({ + doenetML, + requestedVariantIndex: i, + }); + const stateVariables = await returnAllStateVariables(core); + let values = componentNames.map( + (name) => stateVariables[name].stateValues.value, + ); + + expect( + valid_combinations.some((comb) => + comb.every((v, i) => + is_math ? v.equals(values[i]) : v === values[i], + ), + ), + ).eq( + true, + `Expected (${values}) to be in ${valid_combinations.map((comb) => `(${comb})`)}`, + ); + } + } + + it("no parameters, sample single prime number from 2 to 100", async () => { + const doenetML = ``; + const valid_values = [ + [ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, + 61, 67, 71, 73, 79, 83, 89, 97, + ], + ]; + const componentNames = ["/res"]; + + await test_values_separately({ + doenetML, + valid_values, + componentNames, + num_samples: 30, + }); + }); + + it("sample five prime numbers up to 20, only maxValue specified", async () => { + const doenetML = ``; + const values = [2, 3, 5, 7, 11, 13, 17, 19]; + const valid_values = Array(5).fill(values); + const componentNames = ["/res1", "/res2", "/res3", "/res4", "/res5"]; + + await test_values_separately({ + doenetML, + valid_values, + componentNames, + num_samples: 10, + }); + }); + + it("sample five prime numbers between 50 and 100, only minValue specified", async () => { + const doenetML = ``; + const values = [53, 59, 61, 67, 71, 73, 79, 83, 89, 97]; + const valid_values = Array(5).fill(values); + const componentNames = ["/res1", "/res2", "/res3", "/res4", "/res5"]; + + await test_values_separately({ + doenetML, + valid_values, + componentNames, + num_samples: 10, + }); + }); + + it("sample fifty prime numbers between 10,000 and 100,0000", async () => { + let core = await createTestCore({ + doenetML: ``, + }); + + let stateVariables = await returnAllStateVariables(core); + + let samples = stateVariables["/s"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + expect(samples.length).eq(50); + + for (let sample of samples) { + expect( + Number.isInteger(sample) && sample >= 10000 && sample <= 100000, + ).eq(true); + + let isPrime = true; + let sqrtSample = Math.sqrt(sample); + for (let i = 2; i <= sqrtSample; i++) { + if (sample % i === 0) { + isPrime = false; + break; + } + } + + expect(isPrime).eq(true); + } + }); + + it("sample fifty prime numbers between 1900 and 2000, excluding 1931, 1979, and 1997", async () => { + const doenetML = ``; + const values = [ + 1901, 1907, 1913, 1933, 1949, 1951, 1973, 1987, 1993, 1999, + ]; + const valid_values = Array(50).fill(values); + const componentNames = [ + "/res1", + "/res2", + "/res3", + "/res4", + "/res5", + ...Array(45).fill(""), + ]; + + await test_values_separately({ + doenetML, + valid_values, + componentNames, + compositeName: "/s", + num_samples: 1, + }); + }); + + it("sampled numbers do change dynamically", async () => { + let core = await createTestCore({ + doenetML: ` + + +

+ +

+ + + + $exclude +

+ +

+ `, + }); + + let stateVariables = await returnAllStateVariables(core); + let sample1replacements = stateVariables["/sample1"].replacements!; + let sample2replacements = stateVariables["/sample2"].replacements!; + expect(sample1replacements.length).eq(50); + expect(sample2replacements.length).eq(180); + let sample1numbers = sample1replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let sample2numbers = sample2replacements.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + for (let num of sample1numbers) { + expect([2, 3, 5, 7].includes(num)).eq(true); + } + for (let num of sample2numbers) { + expect([2, 3, 5, 11, 13, 17, 23, 31, 41, 43].includes(num)).eq( + true, + ); + } + + // Get new samples when change number of samples + await updateMathInputValue({ + latex: "70", + componentName: "/numSamples", + core, + }); + await updateMathInputValue({ + latex: "160", + componentName: "/numSamples2", + core, + }); + + stateVariables = await returnAllStateVariables(core); + let sample1numbersb = stateVariables["/sample1"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let sample2numbersb = stateVariables["/sample2"] + .replacements!.slice( + 0, + stateVariables["/sample2"].replacements!.length - + (stateVariables["/sample2"].replacementsToWithhold ?? 0), + ) + .map((x) => stateVariables[x.componentName].stateValues.value); + expect(sample1numbersb.length).eq(70); + expect(sample2numbersb.length).eq(160); + + for (let num of sample1numbersb) { + expect([2, 3, 5, 7].includes(num)).eq(true); + } + + for (let num of sample2numbersb) { + expect([2, 3, 5, 11, 13, 17, 23, 31, 41, 43].includes(num)).eq( + true, + ); + } + + expect(sample1numbersb.slice(0, 10)).not.eqls( + sample1numbers.slice(0, 10), + ); + expect(sample2numbersb.slice(0, 10)).not.eqls( + sample2numbers.slice(0, 10), + ); + + // Get new samples when change parameters + await updateMathInputValue({ + latex: "20", + componentName: "/maxNum", + core, + }); + await updateMathInputValue({ + latex: "7, 19, 29, 37, 47, 2, 11, 23, 31, 41", + componentName: "/exclude", + core, + }); + + stateVariables = await returnAllStateVariables(core); + let sample1numbersc = stateVariables["/sample1"].replacements!.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + let sample2numbersc = stateVariables["/sample2"] + .replacements!.slice( + 0, + stateVariables["/sample2"].replacements!.length - + (stateVariables["/sample2"].replacementsToWithhold ?? 0), + ) + .map((x) => stateVariables[x.componentName].stateValues.value); + expect(sample1numbersc.length).eq(70); + expect(sample2numbersc.length).eq(160); + + for (let num of sample1numbersc) { + expect([2, 3, 5, 7, 11, 13, 17, 19].includes(num)).eq(true); + } + + for (let num of sample2numbersc) { + expect([3, 5, 13, 17, 43].includes(num)).eq(true); + } + + expect(sample1numbersc.slice(0, 10)).not.eqls( + sample1numbersb.slice(0, 10), + ); + expect(sample2numbersc.slice(0, 10)).not.eqls( + sample2numbersb.slice(0, 10), + ); + }); + + it("sampled numbers don't resample in dynamic map", async () => { + let core = await createTestCore({ + doenetML: ` + How many numbers do you want? +

+ + + + +

+ +

$map1

+ + $p1{name="p3"} + $p2{name="p4"} + + $p3{name="p5"} + $p4{name="p6"} + `, + }); + + async function check_sampled_numbers(sampledNumbers: number[]) { + const stateVariables = await returnAllStateVariables(core); + + expect( + stateVariables["/p1"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p2"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p3"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p4"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p5"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + + expect( + stateVariables["/p6"].activeChildren.map( + (child) => + stateVariables[child.componentName].stateValues.value, + ), + ).eqls(sampledNumbers); + } + + let sampledNumbers: number[] = []; + + // initially nothing + await check_sampled_numbers([]); + + // sample one variable + await updateMathInputValue({ latex: "1", componentName: "/mi1", core }); + + let stateVariables = await returnAllStateVariables(core); + sampledNumbers.push(stateVariables["/a/n"].stateValues.value); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get same number back + await updateMathInputValue({ latex: "1", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers); + + // get two more samples + await updateMathInputValue({ latex: "3", componentName: "/mi1", core }); + + stateVariables = await returnAllStateVariables(core); + let n1 = stateVariables["/a/n"].stateValues.value; + let n2 = stateVariables["/b/n"].stateValues.value; + let n3 = stateVariables["/c/n"].stateValues.value; + expect(n1).eq(sampledNumbers[0]); + sampledNumbers.push(n2); + sampledNumbers.push(n3); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get first two numbers back + await updateMathInputValue({ latex: "2", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers.slice(0, 2)); + + // get six total samples + await updateMathInputValue({ latex: "6", componentName: "/mi1", core }); + + stateVariables = await returnAllStateVariables(core); + n1 = stateVariables["/a/n"].stateValues.value; + n2 = stateVariables["/b/n"].stateValues.value; + n3 = stateVariables["/c/n"].stateValues.value; + let n4 = stateVariables["/d/n"].stateValues.value; + let n5 = stateVariables["/e/n"].stateValues.value; + let n6 = stateVariables["/f/n"].stateValues.value; + expect(n1).eq(sampledNumbers[0]); + expect(n2).eq(sampledNumbers[1]); + expect(n3).eq(sampledNumbers[2]); + sampledNumbers.push(n4); + sampledNumbers.push(n5); + sampledNumbers.push(n6); + await check_sampled_numbers(sampledNumbers); + + // go back to nothing + await updateMathInputValue({ latex: "0", componentName: "/mi1", core }); + await check_sampled_numbers([]); + + // get all six back + await updateMathInputValue({ latex: "6", componentName: "/mi1", core }); + await check_sampled_numbers(sampledNumbers); + }); + + it("asList", async () => { + let core = await createTestCore({ + doenetML: ` +

+

+ `, + }); + + let results: number[] = []; + + let stateVariables = await returnAllStateVariables(core); + + results.push(stateVariables["/u"].stateValues.value); + results.push(stateVariables["/v"].stateValues.value); + results.push(stateVariables["/w"].stateValues.value); + results.push(stateVariables["/x"].stateValues.value); + results.push(stateVariables["/y"].stateValues.value); + + for (let num of results) { + expect([179, 181, 191, 193, 197, 199].includes(num)); + } + expect(stateVariables["/p1"].stateValues.text).eq(results.join(", ")); + expect(stateVariables["/p2"].stateValues.text).eq(results.join("")); + }); + + it("same numbers for given variant if variantDeterminesSeed", async () => { + let doenetML = ` +

+ + +

+ + `; + + let core = await createTestCore({ + doenetML, + requestedVariantIndex: 1, + }); + + let stateVariables = await returnAllStateVariables(core); + + let samples = stateVariables["/p1"].activeChildren.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + expect(samples.length).eq(100); + + for (let sample of samples) { + expect( + [ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, + 59, 61, 67, 71, 73, 79, 83, 89, 97, + ].includes(sample), + ).eq(true); + } + + core = await createTestCore({ + doenetML, + requestedVariantIndex: 1, + }); + + stateVariables = await returnAllStateVariables(core); + + let samples2 = stateVariables["/p1"].activeChildren.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + expect(samples2).eqls(samples); + + core = await createTestCore({ + doenetML, + requestedVariantIndex: 2, + }); + + stateVariables = await returnAllStateVariables(core); + + samples2 = stateVariables["/p1"].activeChildren.map( + (x) => stateVariables[x.componentName].stateValues.value, + ); + + expect(samples2.length).eq(100); + + for (let sample of samples2) { + expect( + [ + 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, + 59, 61, 67, 71, 73, 79, 83, 89, 97, + ].includes(sample), + ).eq(true); + } + + expect(samples2).not.eqls(samples); + }); + + it.only(`resample prime numbers`, async () => { + let core = await createTestCore({ + doenetML: ` +

, + +

+ +

+ + +

+ + `, + }); + + let pn1, pn2, pn3; + let pn1b, pn2b, pn3b; + + let stateVariables = await returnAllStateVariables(core); + + pn1 = stateVariables["/pn1"].stateValues.value; + pn2 = stateVariables["/pn2"].stateValues.value; + pn3 = stateVariables["/pn3"].stateValues.value; + + expect(pn1).gt(1).lt(1000); + expect(pn2).gt(1).lt(1000); + expect(pn3).gt(1000).lt(10000); + + expect(stateVariables["/pn1"].stateValues.text).eq(pn1.toString()); + + await core.requestAction({ + componentName: "/resamp1", + actionName: "callAction", + args: {}, + event: null, + }); + + stateVariables = await returnAllStateVariables(core); + + pn1b = stateVariables["/pn1"].stateValues.value; + pn2b = stateVariables["/pn2"].stateValues.value; + pn3b = stateVariables["/pn3"].stateValues.value; + + expect(pn1b).gt(1).lt(1000); + expect(pn2b).gt(1).lt(1000); + expect(pn3b).gt(1000).lt(10000); + + expect(pn1b).not.eq(pn1); + expect(pn2b).not.eq(pn2); + expect(pn3b).eq(pn3); + + expect(stateVariables["/pn3"].stateValues.text).eq(pn3.toString()); + + await core.requestAction({ + componentName: "/resamp2", + actionName: "callAction", + args: {}, + event: null, + }); + + stateVariables = await returnAllStateVariables(core); + + let pn1c = stateVariables["/pn1"].stateValues.value; + let pn2c = stateVariables["/pn2"].stateValues.value; + let pn3c = stateVariables["/pn3"].stateValues.value; + + expect(pn1c).gt(1).lt(1000); + expect(pn2c).gt(1).lt(1000); + expect(pn3c).gt(1000).lt(10000); + + expect(pn1c).eq(pn1b); + expect(pn2c).eq(pn2b); + expect(pn3c).not.eq(pn3); + }); +}); diff --git a/packages/test-cypress/cypress/e2e/tagSpecific/sampleprimenumbers.cy.js b/packages/test-cypress/cypress/e2e/tagSpecific/sampleprimenumbers.cy.js index 6629c0674..e47584d25 100644 --- a/packages/test-cypress/cypress/e2e/tagSpecific/sampleprimenumbers.cy.js +++ b/packages/test-cypress/cypress/e2e/tagSpecific/sampleprimenumbers.cy.js @@ -6,1731 +6,6 @@ describe("SamplePrimeNumbers Tag Tests", function () { cy.visit("/"); }); - it("no parameters, sample random prime number up to 100", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1 -

- - $p2{name="p3"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - expect(samples.length).eq(50); - - for (let sample of samples) { - expect( - [ - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, - 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, - ].includes(sample), - ).eq(true); - } - - let copiedSamples = stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("sample five prime numbers up to 20, only maxValue specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1 -

- - $p2{name="p3"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(samples.length).eq(50); - - for (let sample of samples) { - expect([2, 3, 5, 7, 11, 13, 17, 19].includes(sample)).eq(true); - } - - let copiedSamples = stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("sample five prime numbers between 50 and 100, only minValue specified", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1 -

- - $p2{name="p3"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(samples.length).eq(50); - - for (let sample of samples) { - expect( - [53, 59, 61, 67, 71, 73, 79, 83, 89, 97].includes(sample), - ).eq(true); - } - - let copiedSamples = stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("sample ten prime numbers betweeen 10,000 and 100,0000", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- - - - -

- -

- $_map1 -

- - $p2{name="p3"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(samples.length).eq(50); - - for (let sample of samples) { - expect( - Number.isInteger(sample) && - sample >= 10000 && - sample <= 100000, - ).eq(true); - - let isprime = true; - let sqrtsample = Math.sqrt(sample); - for (let i = 2; i <= sqrtsample; i++) { - if (sample % i === 0) { - isprime = false; - break; - } - } - - expect(isprime).eq(true); - } - - let copiedSamples = stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("sample fifty prime numbers betweeen 1900 and 2000, excluding 1931, 1979, and 1997", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- -

- -

- $samples{name="samples2"} -

- - $p2{name="p3"} - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples = stateVariables["/samples"].replacements.map( - (y) => stateVariables[y.componentName].stateValues.value, - ); - expect(samples.length).eq(50); - - for (let sample of samples) { - expect( - [ - 1901, 1907, 1913, 1933, 1949, 1951, 1973, 1987, 1993, - 1999, - ].includes(sample), - ).eq(true); - } - - let copiedSamples = stateVariables["/samples2"].replacements.map( - (y) => stateVariables[y.componentName].stateValues.value, - ); - expect(copiedSamples).eqls(samples); - - let copiedCopiedSamples = stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(copiedCopiedSamples).eqls(samples); - }); - }); - - it("sampled number does change dynamically", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a - - -

- -

- - - - $exclude -

- -

-

- $numSamples2.value{assignNames="numSamples2a"} -

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let sample1numbers, sample2numbers; - let sample1numbersb, sample2numbersb; - let sample1numbersc, sample2numbersc; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - sample1numbers = stateVariables["/sample1"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - sample2numbers = stateVariables["/sample2"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - expect(sample1numbers.length).eq(50); - expect(sample2numbers.length).eq(180); - - for (let num of sample1numbers) { - expect([2, 3, 5, 7].includes(num)).eq(true); - } - - for (let num of sample2numbers) { - expect([2, 3, 5, 11, 13, 17, 23, 31, 41, 43].includes(num)).eq( - true, - ); - } - }); - - cy.log("Get new samples when change number of samples"); - cy.get(cesc("#\\/numSamples") + " textarea").type( - `{end}{backspace}{backspace}70{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/numSamples2") + " textarea").type( - `{ctrl+home}{shift+end}{backspace}160{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/numSamples2a")).should("contain.text", "160"); - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - sample1numbersb = stateVariables["/sample1"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - sample2numbersb = stateVariables["/sample2"].replacements - .slice( - 0, - stateVariables["/sample2"].replacements.length - - stateVariables["/sample2"].replacementsToWithhold, - ) - .map((x) => stateVariables[x.componentName].stateValues.value); - expect(sample1numbersb.length).eq(70); - expect(sample2numbersb.length).eq(160); - - for (let num of sample1numbersb) { - expect([2, 3, 5, 7].includes(num)).eq(true); - } - - for (let num of sample2numbersb) { - expect([2, 3, 5, 11, 13, 17, 23, 31, 41, 43].includes(num)).eq( - true, - ); - } - - expect(sample1numbersb.slice(0, 10)).not.eqls( - sample1numbers.slice(0, 10), - ); - expect(sample2numbersb.slice(0, 10)).not.eqls( - sample2numbers.slice(0, 10), - ); - }); - - cy.log("Get new samples when change parameters"); - cy.get(cesc("#\\/maxnum") + " textarea").type( - `{end}{backspace}{backspace}20{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/exclude") + " textarea").type( - `{end}, 2, 11, 23, 31, 41{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/ml_exclude")).should("contain.text", "41"); - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - sample1numbersc = stateVariables["/sample1"].replacements.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - sample2numbersc = stateVariables["/sample2"].replacements - .slice( - 0, - stateVariables["/sample2"].replacements.length - - stateVariables["/sample2"].replacementsToWithhold, - ) - .map((x) => stateVariables[x.componentName].stateValues.value); - expect(sample1numbersc.length).eq(70); - expect(sample2numbersc.length).eq(160); - - for (let num of sample1numbersc) { - expect([2, 3, 5, 7, 11, 13, 17, 19].includes(num)).eq(true); - } - - for (let num of sample2numbersc) { - expect([3, 5, 13, 17, 43].includes(num)).eq(true); - } - - expect(sample1numbersc.slice(0, 10)).not.eqls( - sample1numbersb.slice(0, 10), - ); - expect(sample2numbersc.slice(0, 10)).not.eqls( - sample2numbersb.slice(0, 10), - ); - }); - }); - - it("sampled number doesn't resample in dynamic map", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a - How many numbers do you want? -

- - - - - - -

- -

$_map1

-

$_aslist1

- - $p1{name="p4"} - $p2{name="p5"} - $p3{name="p6"} - - $p6{name="p7"} - $p7{name="p8"} - $p8{name="p9"} -

$_mathinput1.value{assignNames="m1"}

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let samplednumbers = []; - - cy.log("initially nothing"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("sample one number"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}1{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "1"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - samplednumbers.push(n1); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - for (let ind = 0; ind < 1; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get same number back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}1{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "1"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(1); - - for (let ind = 0; ind < 1; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("get two more samples"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}3{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "3"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - samplednumbers.push(n2); - samplednumbers.push(n3); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(3); - for (let ind = 0; ind < 3; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get first two numbers back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}2{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "2"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(2); - - for (let ind = 0; ind < 2; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("get six total samples"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}6{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "6"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - let n4 = stateVariables["/d/n"].stateValues.value; - let n5 = stateVariables["/e/n"].stateValues.value; - let n6 = stateVariables["/f/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect(n3).eq(samplednumbers[2]); - samplednumbers.push(n4); - samplednumbers.push(n5); - samplednumbers.push(n6); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - for (let ind = 0; ind < 6; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - - cy.log("go back to nothing"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}0{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "0"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(0); - }); - - cy.log("get all six back"); - cy.get(cesc("#\\/_mathinput1") + " textarea").type( - `{end}{backspace}6{enter}`, - { force: true }, - ); - cy.get(cesc("#\\/m1")).should("contain.text", "6"); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - let n1 = stateVariables["/a/n"].stateValues.value; - let n2 = stateVariables["/b/n"].stateValues.value; - let n3 = stateVariables["/c/n"].stateValues.value; - let n4 = stateVariables["/d/n"].stateValues.value; - let n5 = stateVariables["/e/n"].stateValues.value; - let n6 = stateVariables["/f/n"].stateValues.value; - expect(n1).eq(samplednumbers[0]); - expect(n2).eq(samplednumbers[1]); - expect(n3).eq(samplednumbers[2]); - expect(n4).eq(samplednumbers[3]); - expect(n5).eq(samplednumbers[4]); - expect(n6).eq(samplednumbers[5]); - expect( - stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p2"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p3"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p4"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p5"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p6"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p7"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p8"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - expect( - stateVariables[ - stateVariables["/p9"].activeChildren[0].componentName - ].activeChildren.length, - ).eq(6); - for (let ind = 0; ind < 6; ind++) { - expect( - stateVariables[ - stateVariables[ - stateVariables["/p1"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p2"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p3"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p4"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p5"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p6"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p7"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p8"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - expect( - stateVariables[ - stateVariables[ - stateVariables["/p9"].activeChildren[0] - .componentName - ].activeChildren[ind].componentName - ].stateValues.value, - ).eq(samplednumbers[ind]); - } - }); - }); - - it("sample single prime number, assign name", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

-

-

-

$u{name="u2"}

-

$v{name="v2"}

-

$w{name="w2"}

- `, - }, - "*", - ); - }); - - let options = [83, 89]; - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/u"]; - let u2 = stateVariables["/u2"]; - - expect(options.includes(u.stateValues.value)).eq(true); - expect(u.stateValues.value).eq(u2.stateValues.value); - - let v = stateVariables["/v"]; - let v2 = stateVariables["/v2"]; - expect(options.includes(v.stateValues.value)).eq(true); - expect(v.stateValues.value).eq(v2.stateValues.value); - - let w = stateVariables["/w"]; - let w2 = stateVariables["/w2"]; - expect(options.includes(w.stateValues.value)).eq(true); - expect(w.stateValues.value).eq(w2.stateValues.value); - }); - }); - - it("sample multiple prime numbers, assign names", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- -

-

$u{name="u2"}

-

$v{name="v2"}

-

$w{name="w2"}

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - for (let ind = 0; ind < 6; ind++) { - cy.get(cesc("#\\/_p1") + " > :nth-child(" + (2 * ind + 4) + ")") - .invoke("text") - .then((text) => { - let num = Number(text); - results[ind] = num; - expect([179, 181, 191, 193, 197, 199].includes(num)); - }); - } - - cy.log("check by name").then(() => { - cy.get(cesc("#\\/u")).should("have.text", results[0]); - cy.get(cesc("#\\/u2")).should("have.text", results[0]); - cy.get(cesc("#\\/v")).should("have.text", results[1]); - cy.get(cesc("#\\/v2")).should("have.text", results[1]); - cy.get(cesc("#\\/w")).should("have.text", results[2]); - cy.get(cesc("#\\/w2")).should("have.text", results[2]); - }); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/u"]; - let u2 = stateVariables["/u2"]; - expect(u.stateValues.value).eq(results[0]); - expect(u2.stateValues.value).eq(results[0]); - - let v = stateVariables["/v"]; - let v2 = stateVariables["/v2"]; - expect(v.stateValues.value).eq(results[1]); - expect(v2.stateValues.value).eq(results[1]); - - let w = stateVariables["/w"]; - let w2 = stateVariables["/w2"]; - expect(w.stateValues.value).eq(results[2]); - expect(w2.stateValues.value).eq(results[2]); - - let s = stateVariables["/s"]; - expect(s.replacements.length).eq(6); - for (let ind = 0; ind < 6; ind++) { - let r = stateVariables[s.replacements[ind].componentName]; - expect(r.stateValues.value).eq(results[ind]); - } - }); - }); - - it("sample multiple prime numbers, assign names, newNamespace", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

- -

-

$(s/u{name="u2"})

-

$(s/v{name="v2"})

-

$(s/w{name="w2"})

- `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - for (let ind = 0; ind < 6; ind++) { - cy.get(cesc("#\\/_p1") + " > :nth-child(" + (2 * ind + 4) + ")") - .invoke("text") - .then((text) => { - let num = Number(text); - results[ind] = num; - expect([179, 181, 191, 193, 197, 199].includes(num)); - }); - } - - cy.log("check by name").then(() => { - cy.get(cesc("#\\/s\\/u")).should("have.text", results[0]); - cy.get(cesc("#\\/u2")).should("have.text", results[0]); - cy.get(cesc("#\\/s\\/v")).should("have.text", results[1]); - cy.get(cesc("#\\/v2")).should("have.text", results[1]); - cy.get(cesc("#\\/s\\/w")).should("have.text", results[2]); - cy.get(cesc("#\\/w2")).should("have.text", results[2]); - }); - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let u = stateVariables["/s/u"]; - let u2 = stateVariables["/u2"]; - expect(u.stateValues.value).eq(results[0]); - expect(u2.stateValues.value).eq(results[0]); - - let v = stateVariables["/s/v"]; - let v2 = stateVariables["/v2"]; - expect(v.stateValues.value).eq(results[1]); - expect(v2.stateValues.value).eq(results[1]); - - let w = stateVariables["/s/w"]; - let w2 = stateVariables["/w2"]; - expect(w.stateValues.value).eq(results[2]); - expect(w2.stateValues.value).eq(results[2]); - - let s = stateVariables["/s"]; - expect(s.replacements.length).eq(6); - for (let ind = 0; ind < 6; ind++) { - let r = stateVariables[s.replacements[ind].componentName]; - expect(r.stateValues.value).eq(results[ind]); - } - }); - }); - - it("asList", () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

-

- - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let results = []; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - results.push(stateVariables["/u"].stateValues.value); - results.push(stateVariables["/v"].stateValues.value); - results.push(stateVariables["/w"].stateValues.value); - results.push(stateVariables["/x"].stateValues.value); - results.push(stateVariables["/y"].stateValues.value); - - for (let num of results) { - expect([179, 181, 191, 193, 197, 199].includes(num)); - } - cy.get(cesc2("#/_p1")).should("have.text", results.join(", ")); - cy.get(cesc2("#/_p2")).should("have.text", results.join("")); - }); - }); - it(`different numbers when reload page if don't save state`, () => { let doenetML = ` a @@ -1901,198 +176,4 @@ describe("SamplePrimeNumbers Tag Tests", function () { expect(samples2).eqls(samples); }); }); - - it("same numbers for given variant if variantDeterminesSeed", () => { - let doenetML = ` - a -

- - - - -

- - `; - - cy.window().then(async (win) => { - win.postMessage( - { - doenetML, - requestedVariantIndex: 1, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let samples = []; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - samples = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - expect(samples.length).eq(100); - - for (let sample of samples) { - expect( - [ - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, - 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, - ].includes(sample), - ).eq(true); - } - }); - - cy.reload(); - - cy.window().then(async (win) => { - win.postMessage( - { - doenetML, - requestedVariantIndex: 1, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples2 = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - expect(samples2).eqls(samples); - }); - - cy.reload(); - - cy.window().then(async (win) => { - win.postMessage( - { - doenetML, - requestedVariantIndex: 2, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let samples2 = stateVariables[ - stateVariables["/p1"].activeChildren[0].componentName - ].activeChildren.map( - (x) => stateVariables[x.componentName].stateValues.value, - ); - - expect(samples2.length).eq(100); - - for (let sample of samples2) { - expect( - [ - 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, - 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, - ].includes(sample), - ).eq(true); - } - - expect(samples2).not.eqls(samples); - }); - }); - - it(`resample prime numbers`, () => { - cy.window().then(async (win) => { - win.postMessage( - { - doenetML: ` - a -

, - -

- -

- - -

- - `, - }, - "*", - ); - }); - - cy.get(cesc("#\\/_text1")).should("have.text", "a"); //wait for page to load - - let pn1, pn2, pn3; - let pn1b, pn2b, pn3b; - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - pn1 = stateVariables["/pn1"].stateValues.value; - pn2 = stateVariables["/pn2"].stateValues.value; - pn3 = stateVariables["/pn3"].stateValues.value; - - expect(pn1).gt(1).lt(1000); - expect(pn2).gt(1).lt(1000); - expect(pn3).gt(1000).lt(10000); - - cy.get(cesc2("#/pn1")).should("have.text", pn1.toString()); - - cy.get(cesc2("#/resamp1")).click(); - - cy.get(cesc2("#/pn1")).should("not.have.text", pn1.toString()); - }); - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - pn1b = stateVariables["/pn1"].stateValues.value; - pn2b = stateVariables["/pn2"].stateValues.value; - pn3b = stateVariables["/pn3"].stateValues.value; - - expect(pn1b).gt(1).lt(1000); - expect(pn2b).gt(1).lt(1000); - expect(pn3b).gt(1000).lt(10000); - - expect(pn1b).not.eq(pn1); - expect(pn2b).not.eq(pn2); - expect(pn3b).eq(pn3); - - cy.get(cesc2("#/pn3")).should("have.text", pn3.toString()); - - cy.get(cesc2("#/resamp2")).click(); - - cy.get(cesc2("#/pn3")).should("not.have.text", pn3.toString()); - }); - - cy.window().then(async (win) => { - let stateVariables = await win.returnAllStateVariables1(); - - let pn1c = stateVariables["/pn1"].stateValues.value; - let pn2c = stateVariables["/pn2"].stateValues.value; - let pn3c = stateVariables["/pn3"].stateValues.value; - - expect(pn1c).gt(1).lt(1000); - expect(pn2c).gt(1).lt(1000); - expect(pn3c).gt(1000).lt(10000); - - expect(pn1c).eq(pn1b); - expect(pn2c).eq(pn2b); - expect(pn3c).not.eq(pn3); - }); - }); });