local createLibTest = require('/apis/libtest');

local testlib = createLibTest({ ... });

local DEFAULT_CLOUD_URL = 'wss://os.trapcloud.fr';

local function fakeSettings(values)
  return {
    values = values or {},
    get = function(self, key)
      return self.values[key];
    end,
  };
end

local function runPostinstall(settingsLib)
  local lines = {};
  local shellRuns = {};
  local env = setmetatable({
    require = require,
    settings = {
      get = function(key) return settingsLib:get(key); end,
    },
    shell = {
      run = function(...)
        local args = { ... };
        shellRuns[#shellRuns + 1] = args;
        if args[1] == 'set' and args[2] then
          settingsLib.values[args[2]] = args[3];
        end
        return true;
      end,
    },
    print = function(line)
      lines[#lines + 1] = line or '';
    end,
  }, { __index = _G });

  local chunk, loadErr = loadfile('/programs/trapos-postinstall.lua', 't', env);
  if not chunk then error(loadErr, 0); end
  local ok, err = pcall(chunk);
  if not ok then error(err, 0); end
  return lines, shellRuns;
end

local function containsLine(lines, expected)
  for _, line in ipairs(lines) do
    if line == expected then return true; end
  end
  return false;
end

testlib.test('postinstall defaults missing cloud url', function()
  local settingsLib = fakeSettings();
  local lines, shellRuns = runPostinstall(settingsLib);

  testlib.assertEquals(settingsLib.values['cloud.url'], DEFAULT_CLOUD_URL);
  testlib.assertEquals(#shellRuns, 1);
  testlib.assertEquals(shellRuns[1][1], 'set');
  testlib.assertEquals(shellRuns[1][2], 'cloud.url');
  testlib.assertEquals(shellRuns[1][3], DEFAULT_CLOUD_URL);
  testlib.assertTrue(containsLine(lines, '=> Configured cloud.url to ' .. DEFAULT_CLOUD_URL .. '.'));
  testlib.assertTrue(containsLine(lines, '=> Cloud password not set.'));
end);

testlib.test('postinstall treats empty cloud url as missing', function()
  local settingsLib = fakeSettings({ ['cloud.url'] = '' });
  local _, shellRuns = runPostinstall(settingsLib);

  testlib.assertEquals(settingsLib.values['cloud.url'], DEFAULT_CLOUD_URL);
  testlib.assertEquals(#shellRuns, 1);
end);

testlib.test('postinstall preserves existing cloud url and password', function()
  local settingsLib = fakeSettings({
    ['cloud.url'] = 'wss://custom.example',
    ['cloud.password'] = 'secret',
  });
  local lines, shellRuns = runPostinstall(settingsLib);

  testlib.assertEquals(settingsLib.values['cloud.url'], 'wss://custom.example');
  testlib.assertEquals(#shellRuns, 0);
  testlib.assertTrue(containsLine(lines, '=> Keeping existing cloud.url.'));
  testlib.assertTrue(containsLine(lines, '=> Cloud password already configured.'));
end);

testlib.test('postinstall treats empty cloud password as missing', function()
  local settingsLib = fakeSettings({
    ['cloud.url'] = 'wss://custom.example',
    ['cloud.password'] = '',
  });
  local lines = runPostinstall(settingsLib);

  testlib.assertTrue(containsLine(lines, '=> Cloud password not set.'));
end);

testlib.run();
