const fs = require('fs');
const path = require('path');

describe('node data resolution', () => {
  const relativeDataPath = './data/data.json';

  it('should be able to resolve data files through relative paths', () => {
    const resolvedRelativeDataPath = require.resolve(relativeDataPath);
    const dataContent = fs.readFileSync(resolvedRelativeDataPath);
    expect(JSON.parse(dataContent)).toEqual({ "value": 42 });
  });
  it('should be able to resolve data files through absolute paths', () => {
    const resolvedAbsoluteDataPath = require.resolve(path.join(__dirname, relativeDataPath));
    const dataContent = fs.readFileSync(resolvedAbsoluteDataPath);
    expect(JSON.parse(dataContent)).toEqual({ "value": 42 });
  });
  it('should throw when resolving files that are outside the sandbox', () => {
    if (process.platform.startsWith('win')) {
      // On Windows, file location is the original one and not sandboxed.
      // This means we cannot correctly exclude relative paths that aren't part of the runfiles.
      return;
    }
    // This file exists in the source folder but is not in the data array.
    expect(() => require.resolve('./data/missing-data.json')).toThrow();
  });
  it('should be able to resolve paths relative to data files', () => {
    if (process.platform.startsWith('win')) {
      // On Windows, file location is the original one and not sandboxed.
      // This means we cannot resolve paths relative to data files back to built files.
      return;
    }

    const resolvedRelativeDataPath = require.resolve(relativeDataPath);
    const thisFilePath = __filename;
    const relativePathFromDataToThisFile = path.join('../', path.basename(thisFilePath));
    const resolvedPathFromDataToThisFile = require.resolve(path.join(
      path.dirname(resolvedRelativeDataPath), relativePathFromDataToThisFile));

    expect(resolvedPathFromDataToThisFile).toEqual(thisFilePath);
  });
});
const fs = require('fs');
const path = require('path');

describe('node data resolution', () => {
  const relativeDataPath = './data/data.json';
  const isBuiltFile = __filename.endsWith('_built.spec.js');

  it('should be able to resolve data files through relative paths', () => {
    const resolvedRelativeDataPath = require.resolve(relativeDataPath);
    const dataContent = fs.readFileSync(resolvedRelativeDataPath);
    expect(JSON.parse(dataContent)).toEqual({ "value": 42 });
  });
  it('should be able to resolve data files through absolute paths', () => {
    const resolvedAbsoluteDataPath = require.resolve(path.join(__dirname, relativeDataPath));
    const dataContent = fs.readFileSync(resolvedAbsoluteDataPath);
    expect(JSON.parse(dataContent)).toEqual({ "value": 42 });
  });
  it('should throw when resolving files that are outside the sandbox', () => {
    if (process.platform.startsWith('win') && !isBuiltFile) {
      // On Windows, file location for source files is the original one and not sandboxed.
      // This means we cannot correctly exclude relative paths that aren't part of the runfiles.
      expect(() => require.resolve('./data/missing-data.json')).not.toThrow();
    } else {
      // This file exists in the source folder but is not in the data array.
      expect(() => require.resolve('./data/missing-data.json')).toThrow();
    }
  });
  it('should be able to resolve paths relative to data files', () => {
    const resolvedRelativeDataPath = require.resolve(relativeDataPath);
    const thisFilePath = __filename;
    const relativePathFromDataToThisFile = path.join('../', path.basename(thisFilePath));
    const joinedPathFromDataToThisFile = path.join(path.dirname(resolvedRelativeDataPath), 
      relativePathFromDataToThisFile)

    if (process.platform.startsWith('win') && isBuiltFile) {
      // On Windows, file location for data files is the original one and not sandboxed.
      // This means we cannot resolve paths relative to data files back to built files.
      expect(() => require.resolve(joinedPathFromDataToThisFile)).toThrow();
    } else {
      const resolvedPathFromDataToThisFile = require.resolve(joinedPathFromDataToThisFile);

      expect(resolvedPathFromDataToThisFile).toEqual(thisFilePath);
    }
  });
});
Add expectations for failing data resolution tests