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

require "test/unit"
require "pathname"
require "pp"

$LOAD_PATH << "../../lib" unless $LOAD_PATH.include?("../../lib")
require "qwik/test-module-session"

$quickml_debug = true

module QuickML
  class QuickML
    def setup_test_config
      # setup config
      raise "config not defined" if !defined?($test_config)
      @qwikconfig = $test_config

      # setup memory
      raise "memory not defined" if !defined?($test_memory)
      @qwikmemory = $test_memory
    end
  end
end

module TestQuickMLModule
  include TestSession

  def setup
    web_setup
    ml_setup
  end

  def ml_setup
    # setup quickml config
    if !defined?($quickml_config) || $quickml_config.nil?

      config = {
	:user => "qwik",
	:group => "qwik",
	:port => 9195,
	:bind_address => "0.0.0.0",

	:smtp_host => '127.0.0.1',
	:smtp_port => 25,
	:domain => 'qwik.jp',
	:postmaster => "info@qwik.jp",
	:info_url => "http://qwik.jp/",

	:data_dir => $qwik_dir+'/data',
	:pid_file => $qwik_dir+'/log/quickml.pid',
	:log_file => $qwik_dir+'/log/quickml-log',

	:verbose_mode => false,
	:max_members => 100,
	:max_mail_length => 2 * 1024 * 1024,
	:ml_life_time  => 86400 * 31,
	:ml_alert_time => 86400 * 24,
	:auto_unsubscribe_count => 5,

	:sweep_interval => 3600,
	:max_threads => 10,
	:timeout => 120,
	:use_qmail_verp => false,

	:backup_dir => nil,   # for synchronous backup (experimental)
	:confirm_ml_creation => true, # for confirming ML creation.

	:message_catalog => $qwik_dir+'/lib/qwik/quickml-messages.ja',
      }

      config[:data_dir] = "."
      $quickml_config  = QuickML::Config.new(config)
    end
    @ml_config = $quickml_config

    @ml_dir = @ml_config.data_dir.path+"test"
    @ml_dir.teardown

    @ml_catalog = @ml_config.catalog
    @ml_message_charset = "iso-2022-jp"

    t_make_public(QuickML::QuickML, "qwik_post")
  end

  def teardown
    ml_teardown
    web_teardown
  end

  def ml_teardown
    @ml_dir.teardown
  end

  def t_make_public(klass, method_name)
    klass.instance_eval {
      public method_name.intern
    }
  end

  def t_make_readable(klass, variable_name)
    klass.instance_eval {
      attr_reader variable_name.intern
    }
  end

  def ml_mail(mail_from, add_recipient, body)
    mail = QuickML::Mail.new
    mail.mail_from = mail_from
    mail.add_recipient(add_recipient)
    mail.read(body.sjistojis)
    return mail
  end
end
