#!/usr/bin/env ruby
# Copyright (C) 2003-2005 Kouichirou Eto, All rights reserved.

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/test-common"
require "qwik/act-archive"
require "qwik/act-presen"
require "qwik/act-theme"

class TC_ActArchive < Test::Unit::TestCase
  include TestSession

  def assert_next(zis, f)
    e = zis.get_next_entry
    assert_equal_or_match(f, e.name)
  end

  def test_act_zip
    t_add_user

    page = @site["_SiteConfig"]
    page.store(":theme:qwikborder")

    page = @site.create_new
    page.store("* ‚ ")

    session("/test/test.zip")
    assert_equal("application/zip", @res["Content-Type"])
    zip = @res.body
    assert_instance_of(File, zip)
    str = zip.read
    zip.close
    assert_match(/\APK/, str)

    "testtemp.zip".path.open("wb") {|f| f.print str }

    Zip::ZipInputStream.open("testtemp.zip") {|zis|
      assert_next(zis, "test/1.txt")
      assert_equal("* ‚ ", zis.read)
      assert_next(zis, "test/1.html")
      assert_next(zis, "test/1-presen.html")
      assert_next(zis, "test/_SiteConfig.txt")
      assert_next(zis, "test/_SiteConfig.html")
      assert_next(zis, "test/_SiteConfig-presen.html")
      assert_next(zis, "test/_SiteMember.txt")
      assert_equal(",user@e.com,\n", zis.read)
      assert_next(zis, "test/_SiteMember.html")
      assert_next(zis, "test/_SiteMember-presen.html")
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
      assert_next(zis, /\Atest\/.theme/)
#      e = zis.get_next_entry
#      assert_equal(nil, e)
    }

    "testtemp.zip".path.unlink
  end
end

class TC_ZipGenerator < Test::Unit::TestCase
  include TestSession

  def test_zip
    session("/")

    page = @site.create_new
    page.store("* ‚ ")

    gen = Qwik::ZipGenerator.new(@config, @site, @action)
    zip = gen.generate
    assert_match(/test.zip\Z/, zip.to_s)
  end
end
