Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

2.1 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// Tests for windows/fetchfile.js: driver downloads from the app (#348).
// Run with: node --test windows/test/fetchfile.test.js
'use strict';
const assert = require('node:assert/strict');
const { test } = require('node:test');
const crypto = require('node:crypto');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { fetchFile, readResult } = require('../fetchfile');

const body = Buffer.from('driver bytes');
const sha = crypto.createHash('sha256').update(body).digest('hex').toUpperCase();
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'td-fetch-'));
const ok = { fetch: async () => ({ ok: true, status: 200, arrayBuffer: async () => body }) };

test('writes the file when the checksum matches (any case)', async () => {
  const dest = path.join(tmp, 'a.zip');
  await fetchFile('https://github.com/x.zip', sha, dest, { net: ok });
  assert.deepEqual(fs.readFileSync(dest), body);
});

test('a wrong checksum writes nothing', async () => {
  const dest = path.join(tmp, 'b.zip');
  await assert.rejects(fetchFile('https://github.com/x.zip', '00', dest, { net: ok }), /wrong checksum/);
  assert.equal(fs.existsSync(dest), false);
});

test('a name that does not resolve reads in English, naming the host', async () => {
  const net = { fetch: async () => { throw new Error('net::ERR_NAME_NOT_RESOLVED'); } };
  await assert.rejects(fetchFile('https://github.com/x.zip', sha, path.join(tmp, 'c'), { net }), /couldn’t reach github\.com/);
});

test('an HTTP error says its status', async () => {
  const net = { fetch: async () => ({ ok: false, status: 404 }) };
  await assert.rejects(fetchFile('https://github.com/x.zip', sha, path.join(tmp, 'd'), { net }), /HTTP 404/);
});

test('readResult drops the UTF-8 BOM PowerShell 5 writes and keeps non-English text', () => {
  const f = path.join(tmp, 'r.txt');
  fs.writeFileSync(f, 'error: 未能解析此远程名称\r\n', 'utf8');
  assert.equal(readResult(f, 'x'), 'error: 未能解析此远程名称');
  assert.equal(readResult(path.join(tmp, 'none'), 'missing'), 'missing');
});