|
| 1 | +const assert = require("assert"); |
| 2 | +const { existsSync, writeFileSync } = require("fs"); |
| 3 | +const { join } = require("path"); |
| 4 | +const vscode = require("vscode"); |
| 5 | + |
| 6 | +const projectPath1 = join(workspaceDir, "project-1"); |
| 7 | +/** @type {Project} */ |
| 8 | +let project; |
| 9 | +/** @type {Device} */ |
| 10 | +let device; |
| 11 | + |
| 12 | +beforeAll(async () => { |
| 13 | + // Create a project and add a device |
| 14 | + // todo should createProject resolve promise only once project is added to projectsStore? |
| 15 | + await pymakr.commands.createProject(vscode.Uri.parse(projectPath1), { name: "my project" }); |
| 16 | + await new Promise((resolve) => pymakr.projectsStore.next(resolve)); |
| 17 | + project = pymakr.projectsStore.get()[0]; |
| 18 | + device = pymakr.devicesStore.get()[0]; |
| 19 | + project.setDevices([device]); |
| 20 | + assert(existsSync(projectPath1)); |
| 21 | + assert.equal(project.name, "my project"); |
| 22 | + assert(device); |
| 23 | +}); |
| 24 | + |
| 25 | +test("can use devmode", async () => { |
| 26 | + test("can put project in devmode", async () => { |
| 27 | + await pymakr.commands.startDevMode({ project }); |
| 28 | + assert(project.watcher.active); |
| 29 | + assert.equal(project.watcher.deviceManagers.length, 1); |
| 30 | + }); |
| 31 | + test("connected out of sync devices are updated when devmode is started", async () => { |
| 32 | + await pymakr.commands.stopDevMode({ project }); |
| 33 | + await device.connect(); |
| 34 | + writeFileSync(projectPath1 + "/main.py", 'print("hello world1")\n'); |
| 35 | + pymakr.commands.startDevMode({ project }); |
| 36 | + await new Promise((resolve) => device.readUntil("hello world1", resolve)); |
| 37 | + }); |
| 38 | + test("saving a file, uploads it and restarts device", async () => { |
| 39 | + writeFileSync(projectPath1 + "/main.py", 'print("hello world2")\n'); |
| 40 | + await new Promise((resolve) => device.readUntil("hello world2", resolve)); |
| 41 | + }); |
| 42 | + test("devices without looping scripts show idle terminal", async () => { |
| 43 | + writeFileSync(projectPath1 + "/main.py", 'print("hello world3")\n'); |
| 44 | + await new Promise((resolve) => device.readUntil("hello world3", resolve)); |
| 45 | + if (device.busy.get()) await new Promise((resolve) => device.busy.subscribe((val) => !val && resolve())); |
| 46 | + }); |
| 47 | + test("devices with looping scripts show as running user scripts", async () => { |
| 48 | + writeFileSync(projectPath1 + "/main.py", 'import time\nwhile True:\n print("waiting...")\n time.sleep(0.5)\n'); |
| 49 | + await new Promise((resolve) => device.readUntil("waiting...", resolve)); |
| 50 | + await new Promise((resolve) => device.readUntil("waiting...", resolve)); |
| 51 | + assert.equal(device.state.main.get(), "script"); |
| 52 | + }); |
| 53 | + test("devices with looping scripts will be stopped and restarted on file changes", async () => { |
| 54 | + writeFileSync(projectPath1 + "/main.py", 'print("hello world again")'); |
| 55 | + await new Promise((resolve) => device.readUntil("hello world again", resolve)); |
| 56 | + }); |
| 57 | +}); |
0 commit comments